-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.php
More file actions
168 lines (154 loc) · 4.98 KB
/
Copy pathchart.php
File metadata and controls
168 lines (154 loc) · 4.98 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
<?php
/**
* twitter-chart-demo : Just a demo of using twitter search api with php and Twitter-API-PHP
*
* PHP version 5.5.32
*
* @author Konstantinos Togias <info@ktogias.gr>
* @license MIT License
* @version 1.0.0
* @link https://github.com/ktogias/twitter-chart-demo
*/
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Twitter Chart Demo</title>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>Twiter chart demo!</h1>
<div class="container">
<?php
require_once('TwitterAPIExchange.php');
$settings = [
'oauth_access_token' => '',
'oauth_access_token_secret' => '',
'consumer_key' => "xxxxxxxxxx",
'consumer_secret' => "xxxxxxxxxxxx"
];
$twitter = new TwitterAPIExchange($settings);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$maxQueries = 100;
$search = [
'vodafone',
'eurobank'
];
$startDate = new DateTime(date('Y-m-d'));
$startDate->sub(new DateInterval('P7D'));
$dateRange = [
'from' => $startDate,
'to' => new DateTime(date('Y-m-d')),
];
$interval = new DateInterval('P1D');
$period = new DatePeriod($dateRange['from'], $interval, $dateRange['to']);
$days = [];
foreach ($period as $day){
$dayData = [
'since' => clone $day,
'until' => $day->add($interval),
];
foreach ($search as $term){
$dayData['data'][$term] = ['count' => 0];
$q = '?q='.$term.' since:'.$dayData['since']->format('Y-m-d').' until:'.$dayData['until']->format('Y-m-d').'&include_entities=0';
$res = $twitter->setGetfield($q)
->buildOauth($url, 'GET')
->performRequest();
$resObj = json_decode($res);
$qNum = 0;
if(empty($resObj->errors)){
$dayData['data'][$term]['count'] = count($resObj->statuses);
while (!empty($resObj->search_metadata->next_results) && $qNum < $maxQueries){
$q = urldecode($resObj->search_metadata->next_results);
$res = $twitter->setGetfield($q)
->buildOauth($url, 'GET')
->performRequest();
$resObj = json_decode($res);
$qNum++;
if(empty($resObj->errors)){
$dayData['data'][$term]['count'] += count($resObj->statuses);
}
else {
$dayData['data'][$term]['error'] = ['errors' => $resObj->errors, 'qNum' => $qNum, 'q' => $q];
}
}
}
else {
$dayData['data'][$term]['error'] = ['errors' => $resObj->errors, 'qNum' => $qNum, 'q' => $q];
}
}
$days[] = $dayData;
}
?>
<canvas id="chart"></canvas>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.0.2/Chart.bundle.min.js"></script>
<script>
var daysStr = '<?php echo json_encode($days)?>';
var days = JSON.parse(daysStr);
var labels = [];
var data = {};
var colors = [
{
bg: 'rgba(75,192,192,0.4)',
border: 'rgba(75,192,192,1)'
},
{
bg: 'rgba(255,205,86,0.4)',
border: 'rgba(255,205,86,1)'
}
];
var datasets = [];
for (di in days){
var day = days[di];
var fullDateTime = day['since']['date'];
//formatting date for labels
labels.push(fullDateTime.substring(0, fullDateTime.indexOf(' ')));
for(dti in day['data']){
if (!data[dti]){
data[dti] = [];
}
data[dti].push(day['data'][dti]['count']);
}
}
for (label in data){
var color = colors.pop();
datasets.push({
label: label,
fill: false,
backgroundColor: color.bg,
borderColor: color.border,
pointBorderColor: color.border,
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: color.border,
pointHoverBorderColor: color.border,
pointHoverBorderWidth: 2,
data: data[label]
});
}
var ctx = document.getElementById("chart");
var lineChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: datasets
}
});
</script>
</body>
</html>