Skip to content

Instantly share code, notes, and snippets.

@perforb
Last active September 16, 2019 19:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perforb/4207205 to your computer and use it in GitHub Desktop.
Save perforb/4207205 to your computer and use it in GitHub Desktop.
Tweets Importer - Imports your tweets to MySQL.
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)
);
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
<?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