-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.php
225 lines (203 loc) · 5.81 KB
/
helper.php
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
// Helper functions for validating fields to insert to database
// Checks whether the values are exists in the JSON query
function GetTweet(&$item)
{
// Performs a case insensitive string replacement of tweet
// Removes any variance of "DevDemoHi"
if(isset($item->text))
return str_ireplace("@DevDemoHi", " ", $item->text);
else
return null;
}
function GetLat(&$item)
{
if(isset($item->geo))
return $item->geo->coordinates[0];
else
return null;
}
function GetLong(&$item)
{
if(isset($item->geo))
return $item->geo->coordinates[1];
else
return null;
}
function GetHash(&$item)
{
if(isset($item->entities->hashtags[0]))
return $item->entities->hashtags[0]->text;
else
return null;
}
function GetProfImg(&$item)
{
if(isset($item->user['profile_image_url']))
return $item->user['profile_image_url'];
else
return null;
}
/* Function that checks the entities array parsed from twitter feed.
** This entities array contains the media url when users upload images.
** This url obtained is then returned to the function call to be stored in mongo
** If there are no images uploaded, it scans for 'http' string for instagram uploaded images
*/
function GetSpecImg(&$item)
{
if(isset($item->entities->media[0]))
{
return $item->entities->media[0]->media_url;
}
else if(isset($item->entities->urls))
{
for($i = 0; $i < count($item->entities->urls); $i++)
{
// Checks if string 'instagr.am' exists in the current url index
if(strpos($item->entities->urls[$i]->expanded_url, 'instagr.am'))
{
return $item->entities->urls[$i]->expanded_url;
}
}
}
else
return null;
}
/* Function that loops throught the URL array parsed from the twitter feed.
** It then scans for a specific tinyvox method for uploading sound/mp3 files.
** This sound (tinyvox) url is then returned to the function call to be stored in mongo
*/
function GetSpecSound(&$item)
{
$urls = $item->entities->urls;
if(isset($urls))
{
for($i = 0; $i < count($urls); $i++)
{
// Checks if string 'tinyvox' exists in the current url index
if(strpos($urls[$i]->expanded_url, 'tinyvox'))
{
return $urls[$i]->expanded_url;
}
}
}
else
return null;
}
/* Function that loops through the URL array parsed from the twitter feed
** and then scans for a specific yfrog method for uploading videos.
** This video (yfrog) url is then returned to the function call to be stored in mongo.
*/
function GetSpecVid(&$item)
{
$urls = $item->entities->urls;
if(isset($urls))
{
for($i = 0; $i < count($urls); $i++)
{
// Checks if string 'yfrog' exists in the current url index
if(strpos($urls[$i]->expanded_url, 'yfrog'))
return $urls[$i]->expanded_url;
}
}
else
return null;
}
/* Function that queries google api for reverse geocoding
** Parameters, $lat and $long
*/
function GetLocationInfo($lat, $long)
{
if(isset($lat) && isset($long))
{
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$long."&sensor=true";
$geocode = file_get_contents($url);
$geocode = json_decode($geocode);
//var_dump($geocode);
$addComp = $geocode->results[2]->address_components;
//Declare variables to store in array
$interLocation = $geocode->results[0]->formatted_address;
// Geocode JSON Types
$zipCode = null; // postal_code
$city = null; // locality
$county = null; // administrative_area_level_2
$state = null; // administrative_area_level_1
$country = null; // country
// Due to array order variation
// Loops Through address_components array
// Set the variables to correct instances
for($i = 0; $i < count($addComp); $i++)
{
$type = $addComp[$i]->types[0];
if($type == "postal_code")
$zipCode = $addComp[$i]->long_name;
else if($type == "locality")
$city = $addComp[$i]->long_name;
else if($type == "administrative_area_level_2")
$county = $addComp[$i]->long_name;
else if($type == "administrative_area_level_1")
$state = $addComp[$i]->long_name;
else if($type == "country")
$country = $addComp[$i]->long_name;
}
$locArray = array (
'interLocation' => $interLocation,
'zipCode' => $zipCode,
'city' => $city,
'county' => $county,
'state' => $state ,
'country' => $country
);
//var_dump($locArray);
return $locArray;
}
else
{
return null;
}
}
/* Function that determines ID type of id field
** Two types of ID: tweetID(18 long) && ObjectId (24 long)
** TweetID: Generated from twitter JSON
** ObjectID: Automatically generated from Manual Species Insertion
*/
function IsObjectId($id)
{
if(strlen($id) == 18)
return false;
else if(strlen($id) == 24)
return true;
else
return -1;
}
/* Function that gets which id to filter queries by
** Used for updating documents
*/
function GetFilter($id)
{
if(IsObjectId($id))
return array("_id" => new MongoId($id));
else if(!IsObjectId($id))
return array("_id" => $id);
else
return null;
}
function GetGeocodeArray($value)
{
// Gets the array containing geocoded data given lat long parameters
$locInfo = GetLocationInfo($value['geo']['lat'], $value['geo']['long']);
// Declare and set geocoded information
$interLocation = $locInfo['interLocation'];
$zipCode = $locInfo['zipCode'];
$city = $locInfo['city'];
$county = $locInfo['county'];
$state = $locInfo['state'];
$country = $locInfo['country'];
// This insert query only update fields assigned not replace the whole geo array
return array("geo.interLocation" => $interLocation, "geo.zipCode" => $zipCode, "geo.city" => $city,
"geo.county" => $county, "geo.state" => $state, "geo.country" => $country);
}
function GetNewInsertQuery()
{
}
?>