-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter_sync.php
101 lines (78 loc) · 2.98 KB
/
twitter_sync.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
<?php
/* Load and clear sessions */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once("./twitter_config.php");
require_once("connection.php");
/*If not logged in, go back.*/
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['user_id'])) {
header('Location: ./twitter_clearsessions.php');
}
/*Else, get the last sync time.*/
$stmt = $db->prepare("SELECT (SELECT LastSync FROM `Users` WHERE UserID= ?) <
( SELECT NOW() - INTERVAL 1 DAY) AS DayPassed;");
$stmt->execute(array($_SESSION['access_token']['user_id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$DayPassedAfterLastSync = $row['DayPassed'];
/*If last sync was in last 24 hrs, do not sync. Go back.*/
if($DayPassedAfterLastSync == 0){
//Maybe we can put this message in a POST, then show it on index.
//echo "Your last sync was today! Come again tomorrow.";
header('Location: ./index.php');
}else{
//Get the id of last synced tweet
$stmt=$db->prepare("SELECT LastSyncID FROM `Users` WHERE UserID=?");
$stmt->execute(array($_SESSION['access_token']['user_id']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$LastSyncID=$row['LastSyncID'];
/* Create a TwitterOauth object with consumer/user tokens. */
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
/*get timeline*/
if($LastSyncID) //Not the first sync, use last sync id.
$getarray=array('user_id' => $_SESSION['access_token']['user_id'],
'trim_user' => 'true',
'exclude_replies' => 'true',
'include_rts' => 'false',
'since_id' => $LastSyncID);
else //First sync ever!
//Since this is the first sync, we need to get the folllowers!
include("./twitter_get_followers.php");
//Then we continue with tweets.
$getarray=array('user_id' => $_SESSION['access_token']['user_id'],
'trim_user' => 'true',
'exclude_replies' => 'true',
'include_rts' => 'false');
$content=$connection->get('statuses/user_timeline',$getarray);
$LastSyncIDNotUpdated=1;
date_default_timezone_set('Europe/Istanbul');
foreach($content as $tweet)
{
if($LastSyncIDNotUpdated){
$LastSyncID=$tweet->id;
$LastSyncIDNotUpdated=0;
}
if($tweet->lang == "en"){ //Get only English tweets!
//Convert Twitter time to MySQL time:
$mysqlDate = date_format(date_create_from_format('D M d H:i:s T Y', $tweet->created_at), 'Y-m-d H:i:s');
//Insert into tweets table
$stmt=$db->prepare("INSERT INTO Tweets VALUES (?,?,?,?,NOW(),0,0,0)");
//TweetID, UserID, TweetText, TweetTime, CreatedOn
$stmt->execute(array(
$tweet->id,
$_SESSION['access_token']['user_id'],
$tweet->text,
$mysqlDate));
//echo "$newDate {$tweet->lang} {$tweet->id} {$tweet->text}\n";
}
}
//update user's last sync id & time
$stmt=$db->prepare("UPDATE `Users` SET `LastSync`=NOW(),`LastSyncID`=? WHERE `UserID`=?;");
$stmt->execute(array(
$LastSyncID,
$_SESSION['access_token']['user_id']));
/* Redirect */
//Maybe we can put this message in a POST, then show it on index.
//echo "Synced!";
header('Location: ./index.php');
}
?>