-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
194 lines (149 loc) · 5.35 KB
/
index.php
File metadata and controls
194 lines (149 loc) · 5.35 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
require "vendor/autoload.php";
require_once "keys.php";
use Abraham\TwitterOAuth\TwitterOAuth;
# Imports the Google Cloud client library
use Google\Cloud\Core\Exception\BadRequestException;
use Google\Cloud\Language\LanguageClient;
sentimentAnalysis('data/tweets_missing.csv','data/tweets_analyzed.partial.csv');
/*
print_r("Connecting to twitter API ...\n");
$connection = new TwitterOAuth(API_CONSUMER_KEY, API_CONSUMER_SECRET,API_ACCESS_TOKEN,API_TOKEN_SECRET );
crawlTweets("data/users_tweets_missing.csv","data/tweets.partial.csv","data/mentions.partial.csv",$connection);
//crawlUsers("data/user_sources.partial.csv","data/users.csv",$connection);
*/
function sentimentAnalysis($inputCSV,$outputCSV) {
# Instantiates a client
$language = new LanguageClient([
'projectId' => PROJECT_ID
]);
$tweets = readCSV($inputCSV);
foreach($tweets as $tweet) {
$sentiment = analyzeText($tweet[3],$language);
if($sentiment) {
$result = array_merge($tweet,$sentiment);
writeToCSV($outputCSV,[$result]);
}
}
}
function analyzeText($text,$language){
# Detects the sentiment of the text
try {
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo "\nText: " . $text . 'Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
return $sentiment;
} catch (BadRequestException $e) {
# Language is not supported
echo $e->getMessage();
return null;
}
}
function crawlTweets($source_csv,$tweets_csv,$mentions_csv,$connection) {
$user_sources = readCSV($source_csv);
foreach($user_sources as $index=>$user_source) {
$tweets = getTweets($user_source[1],$connection);
if($tweets) {
writeToCSV($tweets_csv,$tweets[0]);
writeToCSV($mentions_csv,$tweets[1]);
}
}
}
function getTweets($screen_name,$connection) {
$screen_name = cleanTwitterName($screen_name);
print_r("Getting tweets from user $screen_name from Twitter ...");
$tweets = $connection->get("statuses/user_timeline", ["screen_name" => $screen_name,"trim_user" => true, "count" => 200, "include_rts" => 1]);
if($tweets) {
$csvData = [[],[]]; // tweets, user_mentions
foreach($tweets as $tweet) {
$csvData[0][] = [$tweet->id,$tweet->user->id,$tweet->created_at,$tweet->text,$tweet->source,$tweet->retweet_count,$tweet->favorite_count,$tweet->lang,getCurrentDate()];
foreach($tweet->entities->user_mentions as $user_mention) {
$csvData[1][] = [$tweet->id,$tweet->user->id,$user_mention->screen_name,$user_mention->name,$user_mention->id];
}
}
print_r("SUCCESS \n");
return $csvData;
} else {
print_r("FAIL \n");
return null;
}
}
function crawlUsers($source_csv,$target_csv,$connection) {
$user_sources = readCSV($source_csv);
$user_sources_length = count($user_sources);
foreach($user_sources as $index=>$user_source) {
print_r("Getting user data for:\n");
$userData = getUser($user_source[1],$connection);
if($userData) {
writeToCSV($target_csv,[$userData]);
}
}
}
function cleanTwitterName($screen_name) {
$screen_name = trim($screen_name);
$screen_name = preg_replace('/^[\pZ\pC]+|[\pZ\pC]+$/u', '', $screen_name);
$screen_name = ltrim($screen_name, '@');
return $screen_name;
}
function getUser($screen_name,$connection) {
$screen_name = cleanTwitterName($screen_name);
print_r("Getting user $screen_name from Twitter ...");
$user = $connection->get("users/show", ["screen_name" => $screen_name]);
if($user->id) {
print_r("SUCCESS \n");
return [$user->id,$user->screen_name,$user->name,$user->location, $user->description, $user->followers_count, $user->friends_count,$user->created_at, $user->verified, $user->lang,$user->profile_image_url,getCurrentDate()];
} elseif($user->error) {
print_r($user->error);
} else {
print_r("FAIL\n");
print_r($user);
return null;
}
}
// Creates the file if it doesn't exists and writes to the end of it.
function writeToCSV($url,$dataArray) {
print_r("Opening CSV file ... ");
$file = fopen($url,"a");
if($file) {
print_r("SUCCESS\n");
} else {
print_r("FAILED\n");
}
foreach($dataArray as $line) {
print_r("Writing line to CSV ... ");
if(fputcsv($file,$line)) {
print_r("SUCCESS\n");
} else {
print_r("FAIL\n");
}
}
fclose($file);
}
// Reads a CSV file and returns an array
function readCSV($url) {
$row = 0;
$readData = [];
print_r("Opening CSV File ... ");
if (($handle = fopen($url, "r")) !== FALSE) {
print_r("SUCCESS\n");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($row!=0) { // skip first row
$readData[$row] = $data;
}
$row++;
}
fclose($handle);
return $readData;
} else {
print_r("FAIL\n");
return null;
}
}
function getDateForDatabase(string $date) : string {
$timestamp = strtotime($date);
$date_formated = date('Y-m-d H:i:s', $timestamp);
return $date_formated;
}
function getCurrentDate(): string {
return date('Y-m-d H:i:s');
}