|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/* |
|
3
|
|
|
Name: anomalyReader |
|
4
|
|
|
Version: 1.0 |
|
5
|
|
|
Author: Mert S. Kaplan |
|
6
|
|
|
Contact: [email protected] |
|
7
|
|
|
GitHub: https://github.com/mertskaplan/anomaly-reader |
|
8
|
|
|
Twitter: @anomalyReader |
|
9
|
|
|
License: GNU General Public License v3 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// anomaly read settings |
|
13
|
|
|
$word = 'patlama'; |
|
14
|
|
|
$cities = [ |
|
15
|
|
|
['plate' => 01, 'name' => 'Adana', 'key' => 'a', 'lat' => '36.991419', 'long' => '35.330829'], |
|
16
|
|
|
['plate' => 06, 'name' => 'Ankara', 'key' => 'a', 'lat' => '39.920951', 'long' => '32.854003'], |
|
17
|
|
|
['plate' => 16, 'name' => 'Bursa', 'key' => 'a', 'lat' => '40.188528', 'long' => '29.060964'], |
|
18
|
|
|
['plate' => 21, 'name' => 'Diyarbakır', 'key' => 'a', 'lat' => '37.924973', 'long' => '40.210983'], |
|
19
|
|
|
['plate' => 25, 'name' => 'Erzurum', 'key' => 'a', 'lat' => '39.905499', 'long' => '41.265824'], |
|
20
|
|
|
['plate' => 26, 'name' => 'Eskişehir', 'key' => 'e', 'lat' => '39.766706', 'long' => '30.525631'], |
|
21
|
|
|
['plate' => 34, 'name' => 'İstanbul', 'key' => 'a', 'lat' => '41.008238', 'long' => '28.978359'], |
|
22
|
|
|
['plate' => 35, 'name' => 'İzmir', 'key' => 'e', 'lat' => '38.423734', 'long' => '27.142826'], |
|
23
|
|
|
['plate' => 55, 'name' => 'Samsun', 'key' => 'a', 'lat' => '41.279703', 'long' => '36.336067'], |
|
24
|
|
|
['plate' => 61, 'name' => 'Trabzon', 'key' => 'a', 'lat' => '41.005915', 'long' => '39.718494'], |
|
25
|
|
|
['plate' => 63, 'name' => 'Şanlıurfa', 'key' => 'a', 'lat' => '37.167512', 'long' => '38.795578'], |
|
26
|
|
|
['plate' => 65, 'name' => 'Van', 'key' => 'a', 'lat' => '38.501215', 'long' => '43.372908'] |
|
27
|
|
|
]; |
|
28
|
|
|
$radius = '20km'; |
|
29
|
|
|
|
|
30
|
|
|
// app settings |
|
31
|
|
|
$consumerKey = ''; |
|
32
|
|
|
$consumerSecret = ''; |
|
33
|
|
|
$accessToken = ''; |
|
34
|
|
|
$accessTokenSecret = ''; |
|
35
|
|
|
|
|
36
|
|
|
// defaults |
|
37
|
|
|
// ini_set('display_errors', 'On'); // http://php.net/manual/en/ini.list.php |
|
38
|
|
|
// error_reporting(E_ALL | E_STRICT); |
|
39
|
|
|
$export['errors'] = $export['result'] = []; |
|
40
|
|
|
|
|
41
|
|
|
// functions |
|
42
|
|
|
function anomaly($array,$count) { |
|
43
|
|
|
return ($count > array_sum($array)/count($array) * 1.4 ? true : false); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// codes |
|
47
|
|
|
header('Content-Type: application/json; charset=utf-8'); |
|
48
|
|
|
require_once 'class/twitteroauth/autoload.php'; |
|
49
|
|
|
use Abraham\TwitterOAuth\TwitterOAuth; |
|
50
|
|
|
|
|
51
|
|
|
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret); |
|
52
|
|
|
$limits = $connection->get('application/rate_limit_status', array('resources' => 'search')); |
|
53
|
|
|
if ($connection->getLastHttpCode() == 200 && !isset($limits->errors[0]->code)) { // level 1 |
|
54
|
|
|
if ($limits->resources->search->{'/search/tweets'}->remaining >= 1) { // level 2 |
|
55
|
|
|
|
|
56
|
|
|
foreach ($cities as $city) { |
|
57
|
|
|
$array = $arrayWrite = $timeArray = $countArray = $sinceArray = $write = ''; |
|
58
|
|
|
|
|
59
|
|
|
$countFile = file('cities/'. $city['plate'] .'.txt', FILE_IGNORE_NEW_LINES); |
|
60
|
|
|
foreach ($countFile as $lines) { |
|
61
|
|
|
$line = explode(',', $lines); |
|
62
|
|
|
if (isset($line[0])) { |
|
63
|
|
|
$timeArray[] = $line[0]; |
|
64
|
|
|
$countArray[] = $line[1]; |
|
65
|
|
|
$sinceArray[] = $line[2]; |
|
66
|
|
|
$array[] = $line[0] .','. $line[1] .','. $line[2] ."\n"; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
array_shift($array); |
|
70
|
|
|
|
|
71
|
|
|
$q = $city['name'] .' '. $word .' OR '. $city['name'] .'d'. $city['key'] .' '. $word .' OR '. $city['name'] .'d'. $city['key'] .'n '. $word .' OR '. $city['name'] .'d'. $city['key'] .'ki '. $word .' OR "'. $word .' geocode:'. $city['lat'] .','. $city['long'] .' within:'. $radius .'" exclude:nativeretweets exclude:retweets'; |
|
72
|
|
|
$search = $connection->get('search/tweets', array('q' => $q, 'result_type' => 'recent', 'count' => '100', 'since_id' => end($sinceArray), 'include_entities' => false)); |
|
73
|
|
|
|
|
74
|
|
|
$count = count($search->statuses); |
|
75
|
|
|
$timeDifference = floor(time() / 60) - floor(end($timeArray) / 60); |
|
76
|
|
|
if ($timeDifference <= 0) { |
|
77
|
|
|
$timeDifference = 1; |
|
78
|
|
|
} |
|
79
|
|
|
if ($count > 0) { |
|
80
|
|
|
$avarageTweet = $count / $timeDifference; |
|
81
|
|
|
} else { |
|
82
|
|
|
$avarageTweet = 0; |
|
83
|
|
|
} |
|
84
|
|
|
$searchLink = 'https://twitter.com/search?f=tweets&q='. $city['name'] .'%20'. $word .'%20OR%20'. $city['name'] .'d'. $city['key'] .'%20'. $word .'%20OR%20'. $city['name'] .'d'. $city['key'] .'n%20'. $word .'%20OR%20'. $city['name'] .'d'. $city['key'] .'ki%20'. $word .'%20OR%20%22'. $word .'%20near%3A'. $city['lat'] .'%2C'. $city['long'] .'%20within%3A'. $radius .'%22%20exclude%3Anativeretweets%20exclude%3Aretweets%20&src=typd'; |
|
85
|
|
|
|
|
86
|
|
|
if (anomaly($countArray,$avarageTweet)) { |
|
87
|
|
|
// tweet |
|
88
|
|
|
$tweetText = '#'. $city['name'] .' civarından gönderilen veya '. $city['name'] .' ile ilgili olarak "'. $word .'" kelimesini içeren tweetlerin sayısında anomali tespit edildi. Bkz: '. $searchLink; |
|
89
|
|
|
$geo = $connection->get('geo/reverse_geocode', array('lat' => $city['lat'], 'long' => $city['long'])); |
|
90
|
|
|
$tweet = $connection->post('statuses/update', array('status' => $tweetText, 'lat' => $city['lat'], 'long' => $city['long'], 'place_id' => $geo->result->places[0]->id, 'display_coordinates ' => true)); |
|
91
|
|
|
if ($connection->getLastHttpCode() == 200 && !isset($tweet->errors[0]->code)) { |
|
92
|
|
|
array_push($export['result'], [ |
|
93
|
|
|
'anomaly' => true, |
|
94
|
|
|
'tweeted' => true, |
|
95
|
|
|
'city' => $city['name'], |
|
96
|
|
|
'tweetCount' => count($search->statuses), |
|
97
|
|
|
'passedTime' => floor(time() / 60) - floor(end($timeArray) / 60), |
|
98
|
|
|
'avarageTweet' => $avarageTweet, |
|
99
|
|
|
'allAvarageTweet' => array_sum($countArray)/count($countArray), |
|
100
|
|
|
'anomalyBorder' => array_sum($countArray)/count($countArray) * 1.4, |
|
101
|
|
|
'searchLink' => $searchLink |
|
102
|
|
|
]); |
|
103
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
104
|
|
|
array_push($export['result'], [ |
|
105
|
|
|
'anomaly' => true, |
|
106
|
|
|
'tweeted' => false, |
|
107
|
|
|
'city' => $city['name'], |
|
108
|
|
|
'tweetCount' => count($search->statuses), |
|
109
|
|
|
'passedTime' => floor(time() / 60) - floor(end($timeArray) / 60), |
|
110
|
|
|
'avarageTweet' => $avarageTweet, |
|
111
|
|
|
'allAvarageTweet' => array_sum($countArray)/count($countArray), |
|
112
|
|
|
'anomalyBorder' => array_sum($countArray)/count($countArray) * 1.4, |
|
113
|
|
|
'searchLink' => $searchLink |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
117
|
|
|
array_push($export['result'], [ |
|
118
|
|
|
'anomaly' => false, |
|
119
|
|
|
'tweeted' => null, |
|
120
|
|
|
'city' => $city['name'], |
|
121
|
|
|
'tweetCount' => count($search->statuses), |
|
122
|
|
|
'passedTime' => floor(time() / 60) - floor(end($timeArray) / 60), |
|
123
|
|
|
'avarageTweet' => $avarageTweet, |
|
124
|
|
|
'allAvarageTweet' => array_sum($countArray)/count($countArray), |
|
125
|
|
|
'anomalyBorder' => array_sum($countArray)/count($countArray) * 1.4, |
|
126
|
|
|
'searchLink' => $searchLink |
|
127
|
|
|
]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach ($array as $a) { |
|
131
|
|
|
$arrayWrite .= $a; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (isset($search->statuses[0]->id)) { |
|
135
|
|
|
$sinceId = $search->statuses[0]->id; |
|
136
|
|
|
} else { |
|
137
|
|
|
$sinceId = end($sinceArray); |
|
138
|
|
|
} |
|
139
|
|
|
$open = fopen('cities/'. $city['plate'] .'.txt', 'w'); |
|
140
|
|
|
$write = $arrayWrite; |
|
141
|
|
|
$write .= time() .','. $avarageTweet .','. $sinceId ."\n"; |
|
142
|
|
|
fwrite($open, $write); |
|
143
|
|
|
fclose($open); |
|
144
|
|
|
} |
|
145
|
|
|
} else { |
|
146
|
|
|
array_push($export['errors'], [ |
|
147
|
|
|
'errorCode' => '777', |
|
148
|
|
|
'errorMessage' => 'Rate limit exceeded', |
|
149
|
|
|
'errorLevel' => 2 |
|
150
|
|
|
]); |
|
151
|
|
|
} |
|
152
|
|
|
} else { |
|
153
|
|
|
$errors = $limits; |
|
154
|
|
|
$level = '1'; |
|
155
|
|
|
require_once 'errors.php'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
print_r(json_encode($export)); |
|
159
|
|
|
?> |
|
|
|
|
|
|
160
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.