chrismou/echonest 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

chrismou/echonest

Composer 安装命令:

composer require chrismou/echonest

包简介

A simple but fully featured Echonest API wrapper, with auto rate limiting and Guzzle 6 support

README 文档

README

Build Status Test Coverage Code Climate Buy me a beer

A dead simple wrapper class for the echonest API.

Includes support for Guzzle 6, options for max number of attempts before giving up, and a auto rate limiter which spaces out requests based on the number of API requests remaining for that minute.

Install

For composer based projects:

composer require chrismou/echonest

Usage

First you need an Echonest API key. You can obtain one by signing up here: https://developer.echonest.com/account/register

To set up the echonest API client:

$lastfm = new \Chrismou\Echonest\Echonest(
    new GuzzleHttp\Client(),
    YOUR_API_KEY
);

(you can also pass a PSR-3 compliant logger as a third argument - see below for more details)

GET requests

In most cases, this is the only method you'll need to use.

Virtually all requests to Echonest are GET requests, so the simplest way to use this client is to use the get helper function. The format for this is:

$echonest->get($resource, $action, $urlParameters);
  • resource - the specific Echonest resource you're querying (ie, 'artist', 'genre', 'song')
  • action - the method specific to the resource you're calling (ie, 'search', 'profile', 'images')
  • urlParameters (optional) - an array of URL parameters, as specified in the API documentation for that endpoint.

So, if you wanted to get all images for Cher, you could run:

$echonest->get('artist', 'images', ['name' => 'cher']);

Or if you wanted Artist by a specific genre, you could run:

$echonest->get('genre', 'artists', ['name' => 'rock']);

You can also specify 'buckets' as a way of returning multiple sets of data within the same API query. To request them in the request, you can do the following:

$echonest->get(
    'artist',
    'search',
    [
        'name' => 'Arctic Monkeys',
        'bucket' => [
            'genre',
            'biographies',
            'familiarity',
            'images'
        ]
    ]
);

POST requests

A couple of the Echonest endpoints require a POST requests:

$echonest->post($resource, $action, $urlParameters, $formParameters);
  • resource - the specific Echonest resource you're querying (ie, 'artist', 'genre', 'song')
  • action - the method specific to the resource you're calling (ie, 'search', 'profile', 'images')
  • urlParameters (optional) - an array of URL parameters, as specified in the API documentation for that endpoint.
  • formParameters (optional) - an array of form parameters to be used in POST requests

Building a custom request

In some cases, you may want to take advantage of the extra options. The format for these requests is as follows:

$echonest->query($httpMethod, $resource, $action, $urlParameters, $formParameters, $autoRateLimit, $maxRetries);
  • httpMethod - the method to use, ie GET or POST
  • resource - the specific Echonest resource you're querying (ie, 'artist', 'genre', 'song')
  • action - the method specific to the resource you're calling (ie, 'search', 'profile', 'images')
  • urlParameters (optional) - an array of URL parameters, as specified in the API documentation for that endpoint.
  • formParameters (optional) - an array of form parameters to be used in POST requests
  • autoRateLimit (optional) - whether to let the wrapper manage rate limiting (see below)
  • maxRetries (optional) - how many times to attempt a request before giving up and moving on

Refer to the Echonest API documentation for a full list of available endpoints, parameters, buckets and example responses. This wrapper is designed to support virtually all endpoints out of the box, so you should be safe to use whichever ones you need.

Rate limiting

Echonest implements rate limiting, so if you make too many requests within a minute it'll stop you connecting until the minute is up and your limit is reset (the number of requests you get depends on your Echonest account type - if you're in need of more than the default, drop them an email).

This wrapper supports auto rate limiting at the client end, thanks to Echonest returning the number of requests left in the response headers. It does this by checking the number left after each request and the amount of time you have to make these requests, and calculates a suitable wait time between each request. In essence, it tries to space the available requests out over the minute, rather than pounding the API over 20 seconds and then sitting dormant for 40 seconds waiting for the reset.

In some cases, you may want to override this (say, if you know you're only making a total of 20 requests and would rather just run them ASAP rather than space them over 1 minute), which you can do by specifying false as the 4 parameter in the method call.

For example:

$echonest->query('artist', 'images', ['name' => 'cher'], false);

Logging

Optionally, you can pass a PSR-3 compliant logger as the third constructor argument to the client, as long as it implements the \Psr\Log interface (ie, Monolog). By passing this in, some basic logging will automatically be enabled, such a the reason for connection failures, etc.

The echonest client assumes the logger has already been properly configured, so you'll need to do this before passing it in. For more information on configuring Monolog for use with this class, see the usage documentation.

Tests

To run the unit test suite:

curl -s https://getcomposer.org/installer | php
php composer.phar install
./vendor/bin/phpunit

If you use docker, you can also run the test suite against all supported PHP versions:

./vendor/bin/dunit

License

Released under the MIT License. See LICENSE.

chrismou/echonest 适用场景与选型建议

chrismou/echonest 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 160 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 chrismou/echonest 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 chrismou/echonest 我们能提供哪些服务?
定制开发 / 二次开发

基于 chrismou/echonest 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 160
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-18