pyrsmk / streams
统一社交流
Requires
- facebook/graph-sdk: ^5.4
- guzzlehttp/guzzle: ^6.2
- james-heinrich/getid3: ^1.9
- symfony/css-selector: ^3.2
- symfony/dom-crawler: ^3.2
README
Streams旨在通过相同的API统一多个社交源。它主要基于Guzzle和异步请求。
这个库是Myriade 2项目的一部分(链接即将提供)。
目前支持
- DeviantArt
- Dribbble
- 500px
- Flickr
- Google Plus
- Vimeo
- Youtube
安装
composer require pyrsmk/streams
快速示例
让我们看看如何从Facebook上国家地理专辑获取50张照片
$stream = new Streams\Facebook\Album('10150205173893951', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50 ]); $stream->get()->then(function($elements) { // Print all retrieved images foreach($elements as $element) { echo "<img src=\"{$element['source']}\" alt=\"{$element['title']}\">"; } // calling wait() is needed only if you want to wait // for the request to complete before continue the script })->wait();
备注
支持的多个API不支持类似的分页。虽然在Streams中分页完全透明,但如果我们在流参数中不设置限制,流将尝试获取所有可能的元素。通常,流无法获取所有现有元素,因为相应的API禁止这样做(分页不完整)。但有些流有无限分页(它们获取所有现有元素),请求可能需要非常长的时间,甚至可能抛出PHP内存超限异常(我们的测试显示PHP在检索大约10000个元素时崩溃)。
类型
每个元素可以是以下4种类型之一:text
、image
、video
和embed
。嵌入可以是嵌入的视频或API可以返回的任何其他HTML代码。
以下是每种类型元素返回的数据。
文本
[ 'type' => 'text', 'date' => integer, 'author' => string, 'avatar' => string, 'title' => string, 'description' => string, 'permalink' => string ]
图片
[ 'type' => 'image', 'date' => integer, 'author' => string, 'avatar' => string, 'title' => string, 'description' => string, 'permalink' => string, 'source' => string, 'width' => integer, 'height' => integer, 'mimetype' => string ]
视频
[ 'type' => 'video', 'date' => integer, 'author' => string, 'avatar' => string, 'title' => string, 'description' => string, 'permalink' => string, 'source' => string, 'width' => integer, 'height' => integer, 'mimetype' => string, 'preview' => [ 'source' => string, 'width' => integer, 'height' => integer ] ]
嵌入
[ 'type' => 'embed', 'date' => integer, 'author' => string, 'avatar' => string, 'title' => string, 'description' => string, 'permalink' => string, 'html' => string, 'width' => integer, 'height' => integer, 'preview' => [ 'source' => string, 'width' => integer, 'height' => integer ] ]
备注
- Streams会自动检索MIME类型;它创建了大量的额外请求,但由于它们都是一次性异步运行的,因此影响微乎其微。
Streams
以下是可用流及其相应选项的列表。
选项
Streams有一些基本选项
nsfw
:如果获取NSFW元素则设置为true
(默认:false
)limit
:要获取的元素数量(默认:false
)select
:获取元素类型的列表(默认:['text', 'image', 'video', 'embed']
)
Streams可以根据相应的API有多个附加选项。它们通常需要满足api
和secret
选项。这些选项通常是远程网站上创建应用程序后的client_id
和client_secret
。
文件系统
new Streams\FileSystem\Directory('some/path/', [ 'limit' => 50 ]);
备注
- 支持图片和视频类型
- 不支持视频预览
- 不支持永久链接
- 不支持NSFW
- 不支持作者和头像
- 不支持描述
DeviantArt
从分类中获取图片(分类ID在DeviantArt对应页面的URI中可见)
new Streams\DeviantArt\Category('photography/nature', [ 'api' => '*****', 'secret' => '*****', 'nsfw' => false, 'limit' => 50, // either 'newest', 'hot', 'undiscovered', 'popular', // 'popular8h' (default), 'popular24h', 'popular3d', 'popular1w' or 'popular1m' 'type' => 'newest' ]);
获取用户画廊的根图片(不支持其他特定画廊)
new Streams\DeviantArt\User('numyumy', [ 'api' => '*****', 'secret' => '*****', 'nsfw' => false, 'limit' => 50 ]);
备注
DeviantArt API并不成熟。我们曾遇到过很多问题,据我们所知,由于API的设计问题(以及一些其他奇怪的事情),有很多东西不起作用。
- 支持文本和图片
- 无法获取图片描述
- 无法获取视频
- 无法获取特定画廊(https://stackoverflow.com/questions/28581350/obtain-deviantart-deviation-id-from-page-url)
- 有时请求可能非常慢
Dribbble
从存储桶中获取图片
new Streams\Dribbble\Bucket('476346-Usabilty-examples', [ 'token' => '*****', 'limit' => 50 ]);
从项目中获取图片
new Streams\Dribbble\Project('280804-Graphics', [ 'token' => '*****', 'limit' => 50 ]);
从团队获取图片
new Streams\Dribbble\Team('Creativedash', [ 'token' => '*****', 'limit' => 50 ]);
从用户获取图片
new Streams\Dribbble\User('BurntToast', [ 'token' => '*****', 'limit' => 50 ]);
备注
- 支持图片
- 不支持NSFW
从页面上的专辑获取图片
new Streams\Facebook\Album('1710763805841434', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50 ]);
从页面获取个人资料照片
new Streams\Facebook\Album('ChatNoirDesign', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50 ]);
从页面获取根(上传的)图片
new Streams\Facebook\Album('ChatNoirDesign', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50, 'type' => 'uploaded' ]);
从页面获取视频
new Streams\Facebook\Videos('ChatNoirDesign', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50 ]);
从页面获取笔记(文章)
new Streams\Facebook\Notes('289941984496813', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50 ]);
备注
- 支持图片和嵌入类型
- 图片上没有描述
- 笔记无法返回日期
- 不支持NSFW
500px
从用户获取图片
new Streams\FiveHundredPx\User('ademgider', [ 'api' => '*****', 'nsfw' => false, 'limit' => 50 ]);
从用户的相册中获取图片
new Streams\FiveHundredPx\Gallery('ademgider/city-map', [ 'api' => '*****', 'nsfw' => false, 'limit' => 50 ]);
备注
- 支持图片
Flickr
从用户获取媒体
new Streams\Flickr\User('cannon_s5_is', [ 'api' => '*****', 'limit' => 50 ]);
从用户的专辑中获取媒体
new Streams\Flickr\Album('cannon_s5_is/72157625103228853', [ 'api' => '*****', 'limit' => 50 ]);
- 支持图片和嵌入类型
备注
- 无法获取NSFW图片,因为它们不是公开的
Google Plus
从人们那里获取媒体
new Streams\GooglePlus\People('+frandroid', [ 'api' => '*****', 'limit' => 50 ]);
备注
- 支持文本、图片和嵌入类型
- 无限分页
- 不支持NSFW
获取用户的最后图片
new Streams\Instagram\User('ademgider/city-map', [ 'limit' => 20 ]);
备注
- 支持图片和视频类型
- 自2016年6月起,如果没有用户认证,就无法与API通信;由于Streams基于公开访问(因为我们不想向最终用户显示OAuth确认),我们只能获取Instagram账号的最后20条帖子
- 不支持NSFW
从subreddit获取媒体
new Streams\Reddit\Subreddit('earthporn', [ 'nsfw' => false, 'limit' => 50, // either 'popular', 'new' (default), 'rising', 'controversial', 'top' or 'gilded' 'type' => 'new' ]);
从用户的帖子中获取媒体
new Streams\Reddit\User('hansiphoto', [ 'nsfw' => false, 'limit' => 50 ]);
备注
- 支持文本、图片和嵌入类型
- API可能有点慢
Vimeo
从类别获取视频
new Streams\Vimeo\Category('food', [ 'api' => '*****', 'secret' => '*****', 'nsfw' => false, 'limit' => 50 ]);
从频道获取视频
new Streams\Vimeo\Channel('comedy', [ 'api' => '*****', 'secret' => '*****', 'nsfw' => false, 'limit' => 50 ]);
从群组获取视频
new Streams\Vimeo\Group('animation', [ 'api' => '*****', 'secret' => '*****', 'nsfw' => false, 'limit' => 50 ]);
从用户获取视频
new Streams\Vimeo\User('loispatino', [ 'api' => '*****', 'secret' => '*****', 'nsfw' => false, 'limit' => 50 ]);
备注
- 支持嵌入类型
Youtube
从频道获取视频
new Streams\Youtube\Channel('UCCMxHHciWRBBouzk-PGzmtQ', [ 'api' => '*****', 'limit' => 50 ]);
从播放列表获取视频
new Streams\Youtube\Playlist('PLWmL9Ldoef0sKB07aXA1ekfyIqu-47rV6', [ 'api' => '*****', 'limit' => 50 ]);
备注
- 支持嵌入类型
- 不支持NSFW
分组请求
如果您想一次性获取多个流(因为这样可以更有效),可以使用Guzzle Pool类
// Add Facebook $streams[] = function() { $stream = new Streams\Facebook\Album('10150205173893951', [ 'api' => '*****', 'secret' => '*****', 'limit' => 50 ]); return $stream->get()->then(function($elements) { // Print all retrieved images foreach($elements as $element) { echo "<img src=\"{$element['source']}\" alt=\"{$element['title']}\">"; } }); }; // Add Flickr $streams[] = function() { $stream = new Streams\Flickr\Album('cannon_s5_is/72157625103228853', [ 'api' => '*****', 'limit' => 50 ]); return $stream->get()->then(function($elements) { // Print all retrieved images foreach($elements as $element) { echo "<img src=\"{$element['source']}\" alt=\"{$element['title']}\">"; } }); }; // Add Instagram $streams[] = function() { $stream = new Streams\Instagram\User('ladylikelilymusic'); return $stream->get()->then(function($elements) { // Print all retrieved images foreach($elements as $element) { echo "<img src=\"{$element['source']}\" alt=\"{$element['title']}\">"; } }); }; // Run requests $guzzle = new GuzzleHttp\Client(['verify' => false]); $pool = new GuzzleHttp\Pool($guzzle, $streams); $pool->promise()->wait();
有关使用Guzzle进行并发请求的更多信息,请阅读文档。
许可证
MIT.