batopa/chirp

使用 MongoDB 缓存 Twitter

v1.1.0 2019-04-15 12:26 UTC

This package is auto-updated.

Last update: 2024-09-15 23:47:06 UTC


README

Software license Build Status codecov.io Scrutinizer Code Quality Code consistency Latest Stable Version

一个 PHP 库,用于将 MongoDB 用作 Twitter 缓存引擎。

要求

Chirp 需要 MongoDBMongoDB PHP 驱动

安装

使用已全局安装的 composer

composer require batopa/chirp

基本用法

require 'vendor/autoload.php';

use Bato\Chirp\Chirp;

// set your Twitter auth conf
$twitterAuth = [
    'oauth_access_token' => 'xxx',
    'oauth_access_token_secret' => 'yyy',
    'consumer_key' => 'www',
    'consumer_secret' => 'zzz'
];


$mongoDbAuth = [
    // the MongoDB database name to use
    'db'  => 'chirp',
    // the MongoDB connection uri. Do not set to use default
    // 'uri' => 'mongodb://user:password@localhost:27017'
];

// instantiate Chirp with Twitter auth and MongoDB conf
$chirp = new Chirp($twitterAuth, $mongoDbAuth);

// Perform Twitter API request GET statuses/user_timeline of @batopa user
// and save tweets in MongoDB
$result = $chirp->write('statuses/user_timeline', [
    // contains parameter used to build the query string
    'query' => [
        'screen_name' => 'batopa'
    ]
]);

// $result will be an array as
// [
//     'saved' => [array of tweets saved],
//     'read' => [array of tweets returned from Twitter API]
// ]

// read data saved previously
$tweets = $chirp->read('statuses/user_timeline');

所使用的 MongoDB 集合基于 Twitter 请求端点,将 / 替换为 - 字符,因此

  • statuses/user_timeline 请求变为 statuses-user_timeline 集合名称
  • /statuses///home_timeline// 请求变为 statuses-home_timeline 集合名称

高级用法

保存

您可以只保存返回的一些推文

// Save only tweets under some conditions
$chirp->write('statuses/user_timeline', [
    // contains parameter used for build the query string
    'query' => [
        'screen_name' => 'batopa'
    ],
    // save only if '#chirp' or 'cache' are in 'text' key
    'grep' => [
        'text' => ['#chirp', 'cache']
    ],
    // save only if 'entities' is not empty
    'require' => ['entities']
]);

可以使用 greprequire 来遍历结果集

$chirp->write('statuses/user_timeline', [
    // contains parameter used for build the query string
    'query' => [
        'screen_name' => 'batopa'
    ],
    // save only if 'user' has the key 'location'
    // populated with a string containing 'IT'
    'grep' => [
        'user.location' => 'IT'
    ],
    // save only if 'entities' has the key 'hashtags' valorized
    'require' => ['entities.hashtags']
]);

读取

您可以利用 MongoDB 过滤、排序和操作读取的推文,例如

// Read data from MongoDB
$chirp->read('statuses/user_timeline', [
    // filter
    [
        'user.screen_name' => 'batopa'
    ],
    // options
    [
        // order by id_str desc
        'sort' => ['id_str' => -1],
        // return only some fields
        'projection' => [
            'created_at' => true,
            'user.screen_name' => true,
            'text' => true,
            'id_str' => true,
            'media_url' => true,
            'entities' => true
        ]
    ]
]);

直接使用 MongoDB 和 Twitter API

如果需要,您可以获取数据库或集合,并用于您的目的

// get db
$db = $chirp->getDb();

// get collection
$collection = $chirp->getCollection('statuses/user_timeline');

您还可以使用 Chirp::request() 或获取 TwitterOAuth 的实例来向 Twitter API 发送请求

$twitter = $chirp->getTwitter();

并直接使用它。