This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream-sqlite.php
executable file
·155 lines (123 loc) · 4.29 KB
/
stream-sqlite.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
#!/usr/bin/env php
<?php
require_once __DIR__.'/vendor/autoload.php';
$GLOBALS['-cache-media'] = [];
$GLOBALS['-cache-users'] = [];
$GLOBALS['-cache-tweets'] = [];
function store_media($media) {
if (!empty($GLOBALS['-cache-media'][$media['id']])) {
return false;
}
$data = json_encode($media, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
$GLOBALS['media_ins']->execute([$media['id'], $data]);
$GLOBALS['-cache-media'][$media['id']] = true;
return true;
}
function store_user($user) {
if (!empty($GLOBALS['-cache-users'][$user['id']])) {
return false;
}
$data = json_encode($user, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
$GLOBALS['user_ins']->execute([$user['id'], $data, $data]);
$GLOBALS['-cache-users'][$user['id']] = true;
return true;
}
function store_tweet($tweet) {
if (!empty($GLOBALS['-cache-tweets'][$tweet['id']])) {
return false;
}
store_user($tweet['user']);
$tweet['user'] = $tweet['user']['id'];
if (!empty($tweet['retweeted_status']) ) {
store_tweet($tweet['retweeted_status']);
$tweet['retweeted_status'] = $tweet['retweeted_status']['id'];
}
if (!empty($tweet['quoted_status']) ) {
store_tweet($tweet['quoted_status']);
$tweet['quoted_status'] = $tweet['quoted_status']['id'];
}
if (!empty($tweet['entities']['media'])) {
foreach ($tweet['entities']['media'] as $k => $m) {
store_media($m);
$tweet['entities']['media'][$k] = $m['id'];
}
}
if (!empty($tweet['extended_entities']['media'])) {
foreach ($tweet['extended_entities']['media'] as $k => $m) {
store_media($m);
$tweet['extended_entities']['media'][$k] = $m['id'];
}
}
$data = json_encode($tweet, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
$GLOBALS['tweet_ins']->execute([$tweet['id'], $tweet['user'], $data]);
if (!empty($tweet['in_reply_to_status_id'])) {
$GLOBALS['tree_ins']->execute([$tweet['id'], $tweet['in_reply_to_status_id']]);
}
$GLOBALS['-cache-tweets'][$tweet['id']] = true;
return true;
}
$db = new \TDC\PDO\SQLite(__DIR__.'/db.sqlite');
$db->exec("PRAGMA auto_vacuum = INCREMENTAL");
$db->exec("PRAGMA case_sensitive_like = ON");
$db->exec("PRAGMA foreign_keys = ON");
$db->exec("PRAGMA journal_mode = WAL");
$db->exec("PRAGMA locking_mode = NORMAL");
$db->exec("PRAGMA synchronous = NORMAL");
$db->exec("PRAGMA threads = 4");
$db->exec("PRAGMA trusted_schema = OFF");
$media_ins = $db->prepare("INSERT INTO twitter_media (media_id, media_data) VALUES (?, ?) ON CONFLICT(media_id) DO NOTHING");
$user_ins = $db->prepare("INSERT INTO twitter_users (user_id, user_data) VALUES (?, ?) ON CONFLICT(user_id) DO UPDATE SET user_data = ?");
$tweet_ins = $db->prepare("INSERT INTO twitter_tweets (tweet_id, user_id, tweet_data) VALUES (?, ?, ?) ON CONFLICT(tweet_id) DO NOTHING");
$tree_ins = $db->prepare("INSERT INTO twitter_tree (tweet_id, tweet_parent) VALUES (?, ?) ON CONFLICT(tweet_id,tweet_parent) DO NOTHING");
require_once __DIR__.'/keys.php';
$GLOBALS['count'] = 0;
$langs = ['da' => 0, 'de' => 0];
$terms = [];
foreach (array_keys($langs) as $lang) {
$qs = explode("\n", trim(file_get_contents(__DIR__."/search-terms-{$lang}.txt")));
foreach ($qs as $q) {
$q = trim($q);
if (empty($q)) {
continue;
}
$terms[] = $q;
}
}
sort($terms);
$terms = array_unique($terms);
$stream = \Spatie\TwitterStreamingApi\PublicStream::create($keys['oauth_access_token'], $keys['oauth_access_token_secret'], $keys['consumer_key'], $keys['consumer_secret']);
$stream->setLocale(implode(',', array_keys($langs)));
$stream->whenHears($terms, function(array $t) {
if (empty($t['id'])) {
echo "Skip: ".var_export($t, true)."\n";
return;
}
if (empty($t['lang'])) {
echo "Skip {$t['id']}: ".var_export($t, true)."\n";
return;
}
/*
if (!isset($GLOBALS['langs'][$t['lang']])) {
echo "Skip {$t['lang']} {$t['id']}\n";
return;
}
//*/
foreach (['-cache-media', '-cache-users', '-cache-tweets'] as $i) {
if (count($GLOBALS[$i]) > 10000) {
echo "Clear cache $i\n";
$GLOBALS[$i] = [];
}
}
++$GLOBALS['langs'][$t['lang']];
echo "Store {$t['lang']} {$t['id']} {$GLOBALS['langs'][$t['lang']]}\n";
++$GLOBALS['count'];
if ($GLOBALS['count'] % 100 == 0) {
echo "Commit {$GLOBALS['count']}\n";
$GLOBALS['db']->commit();
$GLOBALS['db']->beginTransaction();
}
store_tweet($t);
});
$GLOBALS['db']->beginTransaction();
$stream->startListening();
$GLOBALS['db']->commit();