-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers_factory.php
More file actions
executable file
·186 lines (151 loc) · 5.4 KB
/
users_factory.php
File metadata and controls
executable file
·186 lines (151 loc) · 5.4 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
<?php
include("database.php");
include("connection_factory.php");
include("filters_city.php");
include("cities_factory.php");
class Users_Factory{
var $users_from_twitter;
var $users_with_conflict;
var $instance_conn;
var $city_factory;
var $filter_city;
var $zones;
function __construct() {
$this->users_new_from_twitter = array();
$this->users_with_conflict = array();
$this->instance_conn = Database::getInstance();
$this->city_factory = new Cities_Factory();
$this->filter_city = new Filters_City();
$this->zones = array(array(30, array(-21.0, -69.4, 450)),
array(30, array(-28.7, -70.4, 250)),
array(30, array(-30.4, -70.9, 220)),
array(40, array(-33.0, -71.0, 100)),
array(50, array(-35.1, -71.2, 120)),
array(40, array(-37.0, -72.1, 170)),
array(40, array(-39.7, -72.4, 190)),
array(40, array(-42.5, -72.9, 260)),
array(20, array(-48.1, -73.9, 260)),
array(20, array(-52.9, -71.2, 340))
);
}
public function iterate_zones() {
$connection = new Connection_Factory();
foreach($this->zones as $zone) {
$max_calls = $zone[0];
$dat_zone = $zone[1];
#$this->json = $connection->get_tweets_from_location("-35.5","-71.5","2300", "100");
$json = $connection->get_tweets_from_location($dat_zone[0], $dat_zone[1], $dat_zone[2], $max_calls);
if(isset($json->hash->error)) {
exit(0);
}
$this->build_users_from_twitter($json);
#exit(0);
}
}
public function build_users_from_twitter($json) {
if (isset($json->results)) {
$nresults = count($json->results);
}
else {
return 0;
}
$profile_image_url = "#";
#for ($i = 0; $i < $nresults; ++$i) {
foreach ($json->results as $user) {
//verificamos que no este guardado
if (!$this->user_exists($user->from_user_id)) {
if (isset($user->profile_image_url)) {
$profile_image_url = $user->profile_image_url;
}
$real_location = $this->real_location($user->from_user);
#aplicamos el primer filtro
if ($this->filter_city->is_prohibited_city($real_location)) {
continue;
}
//por si superamos la cuota de twitter aquí podemos guardar pendientes
if (!isset($real_location)) {
exit(0);
}
#echo "TUTO real location $real_location <br>";
$city = $this->city_factory->get_city($real_location);
#echo "TUTO $city <br>";
if (strtoupper($city) == "CONFLICTO") {
#echo "TUTO $real_location, $this->json->results[$i]->from_user <br>";
array_push($this->users_with_conflict, array(
'from_user_id' => $user->from_user_id,
'ciudad' => $real_location,
'pais' => 'chile',
'profile_image_url' => $profile_image_url,
'from_user' => $user->from_user,
'location' => $user->location)
);
}
else if ($city != 0 and strtoupper($city) != "CONFLICTO") {
//almacenamos en el arreglo con todos los datos
array_push($this->users_new_from_twitter, array(
'from_user_id' => $user->from_user_id,
'ciudad' => $city,
'pais' => 'chile',
'profile_image_url' => $profile_image_url,
'from_user' => $user->from_user,
'location' => $user->location)
);
}
$profile_image_url = '#';
}
}
}
private function real_location($user) {
$connection = new connection_factory();
$perfil = $connection->get_perfil_for_user($user);
if (isset($perfil->error) && ereg ("^Rate limit exceeded", $perfil->error)) {
return 0;
}
#echo "Buscando en la ciudad".$perfil->location."<br />";
return $perfil->location;
}
private function user_exists($str) {
$query = "SELECT from_user_id from users where from_user_id = $str";
$matrix = $this->instance_conn->get_connection()->query($query);
$total_results = $matrix->num_rows;
if ($total_results > 0) {
return 1;
}
return 0;
}
public function save_new_users() {
echo "Se agregaron los siguientes usuarios <br />";
print_r($this->users_new_from_twitter);
foreach ($this->users_new_from_twitter as $user) {
$from_user_id = $user['from_user_id'];
$ciudad = $user['ciudad'];
$pais = $user['pais'];
$profile_image_url = $user['profile_image_url'];
$from_user = $user['from_user'];
$location = $user['location'];
$query = "INSERT INTO users (id, from_user_id, ciudad, pais, profile_image_url, from_user, location, fecha_ingreso)
VALUES('', '$from_user_id', '$ciudad', '$pais', '$profile_image_url', '$from_user', '$location', now())";
if(!$this->instance_conn->get_connection()->query($query)) {
echo "error $query";
}
}
}
public function save_conflicted_users() {
echo "Se agregaron los siguientes usuarios conflictivos <br />";
print_r($this->users_with_conflict);
foreach ($this->users_with_conflict as $user) {
$from_user_id = $user['from_user_id'];
$ciudad = $user['ciudad'];
$pais = $user['pais'];
$profile_image_url = $user['profile_image_url'];
$from_user = $user['from_user'];
$location = $user['location'];
$query = "INSERT INTO pendientes (id, from_user_id, ciudad, pais, profile_image_url, from_user, location, fecha_ingreso)
VALUES('', '$from_user_id', '$ciudad', '$pais', '$profile_image_url', '$from_user', '$location', now())";
if(!$this->instance_conn->get_connection()->query($query)) {
echo "error $query";
}
}
}
}
?>