-
Notifications
You must be signed in to change notification settings - Fork 1
/
importarchive.php
164 lines (126 loc) · 3.74 KB
/
importarchive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
chdir(dirname(__FILE__));
require_once "lib/ConfigHelper.php";
ConfigHelper::requireConfig("config.php");
$mysqli = ConfigHelper::getDatabaseConnection();
require_once('lib/TweetStorer.php');
require_once('lib/UserUtil.php');
$zip = openZip($argc, $argv);
$pathJson = decodeFile($zip, 'data/js/tweet_index.js');
$archivePaths = array();
$tweetsToProcess = 0;
foreach($pathJson as $path) {
$archivePaths[] = $path->file_name;
$tweetsToProcess += $path->tweet_count;
}
$tweetsAdded = 0;
$tweetsProcessed = 0;
$userIds = array();
foreach($archivePaths as $path) {
$tweets = decodeFile($zip, $path);
foreach($tweets as $tweet) {
if(TweetStorer::storeTweet($tweet, $mysqli)) {
$tweetsAdded++;
if(property_exists($tweet, "retweeted_status")) {
$userIds[] = $tweet->retweeted_status->user->id;
}
$userIds[] = $tweet->user->id;
}
$tweetsProcessed++;
}
output("\r\033[K"); //rewrite line
output("Processing tweets ($tweetsProcessed/$tweetsToProcess)");
}
output("\nAdded $tweetsAdded new tweet".(($tweetsAdded == 1) ? "" : "s")."\n");
if($tweetsAdded > 0)
$usersUpdated = lookupUsers(array_keys(array_flip($userIds)), $mysqli);
$zip->close();
if (!isCli()) {
echo json_encode(array("tweetsAdded" => $tweetsAdded, "tweetsProcessed" => $tweetsProcessed, "usersUpdated" => $usersUpdated));
}
function lookupUsers($ids, $mysqli) {
$twitterObj = ConfigHelper::getTwitterObject();
$lookupChunkSize = 100;
$chunks = array_chunk($ids, $lookupChunkSize);
$usersToUpdate = count($ids);
$usersUpdated = 0;
foreach($chunks as $chunk) {
foreach($twitterObj->post('/users/lookup.json', array("user_id" => implode(",", $chunk))) as $user) {
UserUtil::upsertUser($user, $mysqli);
}
$usersUpdated += count($chunk);
output("\r\033[K"); //rewrite line
output("Updating users ($usersUpdated/$usersToUpdate)");
}
output("\n");
return $usersUpdated;
}
function decodeFile($zip, $path) {
$file = $zip->getFromName($path);
if(!$file) {
header('http/1.1 400 Bad Request');
exit("ERROR: Couldn't find $path in zip.\n");;
}
$firstBrace = strpos($file, "{");
$firstBracket = strpos($file, "[");
if(!$firstBrace && !$firstBracket) {
header('HTTP/1.1 400 Bad Request');
exit("ERROR: Invalid JSON in $path within zip.\n");
}
if($firstBrace && $firstBracket) {
$strpos = $firstBrace < $firstBracket ? $firstBrace : $firstBracket;
} else if ($firstBrace) {
$strpos = $firstBrace;
} else {
$strpos = $firstBracket;
}
if(!$strpos || is_null($decoded = json_decode(substr($file, $strpos)))) {
header('HTTP/1.1 400 Bad Request');
exit("ERROR: Invalid JSON in $path within zip.\n");
}
return $decoded;
}
function openZip($argc, $argv) {
$fileName = determineFileName($argc, $argv);
if(!class_exists('ZipArchive')) {
header('HTTP/1.1 500 Internal Server Error.');
exit("ERROR: Zip extension not loaded. Install/include it.\n");
}
$zip = new ZipArchive;
$status = $zip->open($fileName);
if($status !== true) {
header('HTTP/1.1 400 Bad Request');
exit("ERROR: Invalid file. (Errno: $status)\n");
}
return $zip;
}
function determineFileName($argc, $argv) {
if(isCli()) {
if($argc > 1) {
$fileName = $argv[1];
if(!is_readable($fileName)) {
exit("ERROR: $fileName is not readable\n");
}
return $fileName;
} else {
exit("ERROR: Need to specify zip file to load.\nUsage: php {$argv[0]} <path to archive.zip>\n");
}
} else {
$fileInputName = "archiveFile";
if(empty($_FILES) || !array_key_exists($fileInputName, $_FILES) || $_FILES[$fileInputName]["size"] == 0) {
header('HTTP/1.1 400 Bad Request');
exit("Invalid file");
} else {
return $_FILES[$fileInputName]["tmp_name"];
}
}
}
function output($text) {
if(isCli()) {
echo $text;
}
}
function isCli() {
return PHP_SAPI == 'cli';
}
?>