macfja/book-retriever

用于检索书籍数据的库


README

用于检索书籍数据的库

安装

composer require macfja/book-retriever

使用

简单提供者

获取特定提供者上的书籍信息

$htmlGetter = new \MacFJA\BookRetriever\Helper\HtmlGetter();
$isbnTool = new \Isbn\Isbn();

$antoineOnline = new \MacFJA\BookRetriever\Provider\AntoineOnline($htmlGetter, $isbnTool);
$books = $antoineOnline->searchIsbn('9782253006329');
// $books contains a list of \MacFJA\BookRetriever\SearchResultInterface

可配置提供者

使用可配置提供者获取书籍信息

$providerConfiguration = ...; // A class that implement \MacFJA\BookRetriever\ProviderConfigurationInterface
$configurator = new \MacFJA\BookRetriever\ProviderConfigurator($providerConfiguration);
$amazon = new \MacFJA\BookRetriever\Provider\Amazon();
$configurator->configure($amazon);
$books = $amazon->searchIsbn('9782253006329');
// $books contains a list of \MacFJA\BookRetriever\SearchResultInterface

多个提供者

使用 Pool 提供者请求多个提供者

$providerConfiguration = ...; // A class that implement \MacFJA\BookRetriever\ProviderConfigurationInterface
$configurator = new \MacFJA\BookRetriever\ProviderConfigurator($providerConfiguration);

$htmlGetter = new \MacFJA\BookRetriever\Helper\HtmlGetter();
$isbn = new \Isbn\Isbn();
$opds = new \MacFJA\BookRetriever\Helper\OPDSParser();
$sru = new \MacFJA\BookRetriever\Helper\SRUParser();

$providers = [
    new \MacFJA\BookRetriever\Provider\AbeBooks($htmlGetter),
    new \MacFJA\BookRetriever\Provider\Amazon(),
    new \MacFJA\BookRetriever\Provider\AntoineOnline($htmlGetter, $isbn),
    new \MacFJA\BookRetriever\Provider\ArchiveOrg($opds),
    new \MacFJA\BookRetriever\Provider\LibraryHub($sru),
    new \MacFJA\BookRetriever\Provider\DigitEyes(),
    new \MacFJA\BookRetriever\Provider\Ebay()
];
array_walk($providers, [$configurator, 'configure']);

$pool = new \MacFJA\BookRetriever\Pool($providers, $providerConfiguration);

$books = $pool->searchIsbn('9782253006329');
// $books contains a list of \MacFJA\BookRetriever\SearchResultInterface

如果你使用依赖注入库,可以移除大量代码。(以下为 Symfony 示例)

使用 Symfony(和 Doctrine)

使用 Doctrine 作为 ORM 存储配置的示例

config/services.yaml

services:
    _instanceof:
        # services whose classes are instances of ProviderInterface will be tagged automatically
        MacFJA\BookRetriever\ProviderInterface:
            tags: ['app.provider']
    MacFJA\BookRetriever\:
        resource: '../vendor/macfja/book-retriever/lib/'
    MacFJA\BookRetriever\Pool:
        arguments:
            $providers: !tagged_iterator app.provider
    MacFJA\BookRetriever\ProviderConfigurationInterface: '@App\Repository\ProviderConfigurationRepository'

src/Entity/ProviderConfiguration.php

<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/** @ORM\Entity(repositoryClass="App\Repository\ProviderConfigurationRepository") */
class ProviderConfiguration
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;
    /** @ORM\Column(type="string", length=50) */
    private $provider;
    /** @ORM\Column(type="boolean") */
    private $active;
    /** @ORM\Column(type="json") */
    private $parameters = [];

    // All Getters/Setters
    // Removed in this example for readability
}

src/Repository/ProviderConfigurationRepository.php

<?php
namespace App\Repository;
use App\Entity\ProviderConfiguration;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use MacFJA\BookRetriever\ProviderConfigurationInterface;
use MacFJA\BookRetriever\ProviderInterface;

class ProviderConfigurationRepository extends ServiceEntityRepository implements ProviderConfigurationInterface
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, ProviderConfiguration::class);
    }

    public function getParameters(ProviderInterface $provider): array
    {
        $configuration = $this->findOneBy(['provider' => $provider->getCode()]);
        
        return $configuration !== null ? $configuration->getParameters() : [];
    }

    public function isActive(ProviderInterface $provider): bool
    {
        $configuration = $this->findOneBy(['provider' => $provider->getCode()]);
        // not active by default
        return $configuration !== null ? $configuration->getActive() : false;
    }
}

src/Controller/SomeController.php

<?php
namespace App\Controller;
use MacFJA\BookRetriever\Pool;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class SomeController extends AbstractController
{
    /** @Route("/test") */
    public function index(Pool $pool)
    {
        return new JsonResponse($pool->searchIsbn('9782253006329'));
    }
}

提供者

目前有 20 个内置提供者。更多详情请见 这里

贡献

你可以为库做出贡献。为此,你可以在 Github 问题上

  • 提出你的问题
  • 通知提供者的任何变化
  • 建议新的提供者
  • 请求任何更改(拼写错误、糟糕的代码等)
  • 等等...

你也可以通过 PR 来

  • 添加新的提供者
  • 建议更正
  • 等等...

本地安装

首先克隆项目(无论是这个仓库还是你的分支),然后运行

make install # Install project vendor
make all # Run QA tools + tests suites + generate docs

验证你的代码

当你完成代码编写后,运行以下命令检查代码质量是否符合定义的规则,并对其进行格式化

make analyze # Run QA tools + tests suites

如果你添加了单元测试,运行以下命令在测试代码套件上执行相同的操作

make analyze-tests # Run QA tools on tests suites

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件