-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitializeDB.php
More file actions
130 lines (99 loc) · 4.84 KB
/
initializeDB.php
File metadata and controls
130 lines (99 loc) · 4.84 KB
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
<?php
/**
* initialize.php
*
* Created by Pamela L. Gay
* Use: StarStryder
* Date: 7/10/17
* Time: 12:49 PM
*
* This file
* 0) Initializes things
* 1) Gets all of a seed accounts followers
*
* On completion, run main.php
*
*/
/***********************************************************************************************
* 0) Iniitalize everything
***********************************************************************************************/
// INCLUDES ------------------------------------------------------------------------------------
// Tests to make sure code is not doing evil
require_once('tests.php');
// Library for connecting to Twitter API
require_once('TwitterAPIExchange.php');
// Setup all need tokens and values
require_once('config.php'); // COPY config_SAMPLE.php to config.php and add in your values
echo "starting...\n";
// Initial variables ---------------------------------------------------------------------------
$ids_array = array(); // we'll store all user ids in this to reduce DB calls
/***********************************************************************************************
* 1) Get the seed user and all of the seed user's followers
***********************************************************************************************/
// GET INITIAL SEED USER -----------------------------------------------------------------------
// see documentation https://dev.twitter.com/rest/reference/get/users/show
$url = 'https://api.twitter.com/1.1/users/show.json';
$getfield = '?screen_name='.$seed;
$obj = json_decode($twitter->setGetfield($getfield)->buildOauth($url, 'GET')->performRequest(), true);
test_twshow($obj);
$user_id = $obj['id_str'];
$username = $obj['screen_name'];
$name = addslashes(substr($obj['name'], 0, 49));
$descrip = addslashes($obj['description']);
$followers = $obj['followers_count'];
$friends = $obj['friends_count'];
$tweets = $obj['statuses_count'];
$created = $obj['created_at'];
if ($obj['verified']) $verify = 1; else $verify = 0;
$status = $obj['status'];
$update = $status['created_at'];
$query = "INSERT INTO tweeps (tweep_id, tweep_username, tweep_name, description, followers, friends, tweets, verified, done, created_at, update_at)
VALUES ($user_id, '$username', '$name', '$descrip', $followers, $friends, $tweets, $verify, 1,'$created', '$update')";
test_mysql_q($query, $conn);
// GET THE NETWORK OF THE SEED USER'S FOLLOWERS ------------------------------------------------
// NOTES: If a user has a lot of followers, they won't all get returned in a single query
// To get around this, a "Cursor" variable is used. As long as this isn't zero, there are more
// pages of content. See full documentation: https://dev.twitter.com/rest/reference/get/followers/list
// Returns info for 20 users
// Limited to 15 calls per 15 minutes
echo "Getting $seed's followers ";
$cursor = 1; // To start the while loop
$call = 1;
$i = 0;
while ($cursor) {
if ($cursor == 1) $cursor = -1;
echo ".";
// This gets list of people who follow the seed user
$url = "https://api.twitter.com/1.1/followers/list.json";
$getfield = '?screen_name='.$seed.'&cursor='.$cursor;
$objs = json_decode($twitter->setGetfield($getfield)->buildOauth($url, 'GET')->performRequest(), true);
test_twlist($objs);
// this returns a whole set of users
$tweeps = $objs['users'];
foreach($tweeps as $tweep) {
$user_id = $tweep['id_str'];
$username = $tweep['screen_name'];
$name = addslashes(substr($tweep['name'], 0, 49));
$descrip = addslashes($tweep['description']);
$followers = $tweep['followers_count'];
$friends = $tweep['friends_count'];
$tweets = $tweep['statuses_count'];
$created = $tweep['created_at'];
if ($tweep['verified']) $verify = 1; else $verify = 0;
$status = $tweep['status'];
$update = $status['created_at'];
$query = "INSERT INTO tweeps (tweep_id, tweep_username, tweep_name, description, followers, friends, tweets, verified, done, created_at, update_at)
VALUES ($user_id, '$username', '$name', '$descrip', $followers, $friends, $tweets, $verify, 0,'$created', '$update')";
$i++;
test_mysql_q($query, $conn);
}
// Move to the next page of responses
$cursor = $objs['next_cursor'];
// Don't run into the rate limit
$call++;
if ($call > 15) {
$call = 1;
echo "PAUSE at $cursor and user $username\n";
sleep(15.1*60);
}
}