Last active
September 16, 2019 19:12
-
-
Save perforb/4207205 to your computer and use it in GitHub Desktop.
Tweets Importer - Imports your tweets to MySQL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create database tweets_importer character set utf8; | |
create table user_timeline( | |
id int(10) auto_increment | |
,uid int(10) not null | |
,name varchar(255) not null | |
,text varchar(255) not null | |
,source varchar(255) not null | |
,retweet_count int(10) not null | |
,favorited int(1) not null | |
,retweeted int(1) not null | |
,tweeted_at datetime not null | |
,created_at datetime not null | |
,primary key(id) | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Depends on: | |
git://github.com/abraham/twitteroauth.git | |
Usage: | |
php user_timeline.php -n <screen_name> | --screen_name <screen_name> [-c=<import_count> | --count=<import_count>] [-p=<save_path> | --save_path=<save_path>] | |
Example: | |
php user_timeline.php -n pamyurin -c=15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Depends on: | |
git://github.com/abraham/twitteroauth.git | |
*/ | |
require_once(__DIR__ . '/twitteroauth/twitteroauth/twitteroauth.php'); | |
$shortopts = "n:c::p::"; | |
$longopts = array( | |
"screen_name:", | |
"count::", | |
"save_path::", | |
); | |
$options = getopt($shortopts, $longopts); | |
$options['screen_name'] = isset($options['screen_name']) ? $options['screen_name'] : (isset($options['n']) ? $options['n'] : exit('screen_name is required.')); | |
$options['count'] = isset($options['count']) ? $options['count'] : (isset($options['c']) ? $options['c'] : 200); | |
$options['save_path'] = isset($options['save_path']) ? $options['save_path'] : (isset($options['p']) ? $options['p'] : './'); | |
$settings = array( | |
'consumer_key' => '**********', | |
'consumer_secret' => '**********', | |
'access_token' => '**********', | |
'access_token_secret' => '**********', | |
); | |
$db_settings = array( | |
'dsn' => 'mysql:host=localhost;dbname=tweets_importer;unix_socket=/tmp/mysql.sock', | |
'user' => '**********', | |
'password' => '**********', | |
); | |
$urls = array( | |
'user_timeline' => 'https://api.twitter.com/1.1/statuses/user_timeline.json', | |
); | |
$to = new TwitterOAuth( | |
$settings['consumer_key'], | |
$settings['consumer_secret'], | |
$settings['access_token'], | |
$settings['access_token_secret'] | |
); | |
$json = $to->oAuthRequest( | |
$urls['user_timeline'], | |
'GET', | |
array( | |
'screen_name' => $options['screen_name'], | |
'count' => $options['count'], | |
) | |
); | |
$result = json_decode($json); | |
if (!is_array($result)) exit($result->error . "\n"); | |
$file_name = $options['save_path'] . $options['screen_name'] . '-' . date("YmdHis") . '.json'; | |
file_put_contents($file_name, $json); | |
echo "Saved a file as {$file_name}", "\n"; | |
$dbh = new PDO( | |
$db_settings['dsn'], | |
$db_settings['user'], | |
$db_settings['password'] | |
); | |
$stmt = $dbh->prepare(<<<SQL | |
insert into user_timeline( | |
uid | |
,name | |
,text | |
,source | |
,retweet_count | |
,favorited | |
,retweeted | |
,tweeted_at | |
,created_at | |
) values | |
( | |
:uid | |
,:name | |
,:text | |
,:source | |
,:retweet_count | |
,:favorited | |
,:retweeted | |
,:tweeted_at | |
,now() | |
) | |
SQL | |
); | |
while(($status = array_pop($result)) != null) { | |
$stmt->bindParam(':uid', $status->user->id_str); | |
$stmt->bindParam(':name', $status->user->name); | |
$stmt->bindParam(':text', $status->text); | |
$stmt->bindParam(':source', $status->source); | |
$stmt->bindParam(':retweet_count', $status->retweet_count); | |
$stmt->bindParam(':favorited', $status->favorited); | |
$stmt->bindParam(':retweeted', $status->retweeted); | |
$tweeted_at = date('Y-m-d H:i:s', strtotime($status->created_at)); | |
$stmt->bindParam(':tweeted_at', $tweeted_at); | |
$stmt->execute(); | |
} | |
$dbh = null; | |
echo "Saved user_timeline to MySQL.", "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment