index.php ➔ anomaly()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 42 and the first side effect is on line 13.

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.

Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
160