forked from TwitrTraffic/Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
132 lines (90 loc) · 3.71 KB
/
helpers.py
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
import tweepy
import json,pprint,re
from flask import jsonify,abort,g,flash
import sqlite3
from contextlib import closing
import requests
from urllib2 import urlopen
from keys import GoogleApiKey,twitter_consumer_key,twitter_consumer_secret,twitter_access_token,twitter_access_token_secret
YOUR_API_KEY = GoogleApiKey
consumer_key = twitter_consumer_key
consumer_secret = twitter_consumer_secret
access_token = twitter_access_token
access_token_secret = twitter_access_token_secret
def getTwitterFeed():
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
alltweets = []
new_tweets = api.user_timeline('blrcitytraffic',count=200)
alltweets.extend(new_tweets)
oldest = alltweets[-1].id - 1
t = 30
while len(new_tweets) > 0 and t > 0:
#while t > 0:
t=t-1
new_tweets = api.user_timeline('blrcitytraffic',count=200,max_id=oldest)
alltweets.extend(new_tweets)
oldest = alltweets[-1].id - 1
k=1
for tweet in alltweets:
if not tweet.retweeted and 'RT @' not in tweet.text:
if '@' not in tweet.text:
print "{}: {} -> {}".format(tweet.id,tweet.created_at,re.sub(r'[^\x00-\x7F]+',' ', tweet.text))
dateTime = str(tweet.created_at).split()
g.db.execute('insert into tweets(tweet,Ttime,Tdate) values (?, ?, ?)',[str(re.sub(r'[^\x00-\x7F]+',' ', tweet.text)), dateTime[1], dateTime[0]])
g.db.commit()
k=k+1
cur = g.db.execute('select tweet from tweets')
entries = cur.fetchall()
return entries
def retrieveAllblrTweets():
cur = g.db.execute('select * from tweets')
entries = [dict(id=row[0], tweet=row[1], time=row[2], date=row[3]) for row in cur.fetchall()]
return entries
def getCheckpoints(source,destination):
url = 'https://maps.googleapis.com/maps/api/directions/json?origin=12.9577133,77.6852271&destination=14.0187447,75.2582987&key=' + YOUR_API_KEY
response = urlopen(url)
json_obj = json.load(response)
json_routes = json_obj['routes']
for i in json_routes:
json_legs = i['legs']
for i in json_legs:
json_steps = i['steps']
json_checkpoints = []
for i in json_steps:
json_checkpoints.append(i['end_location'])
#print json.dumps(json_checkpoints,indent=2)
return json_checkpoints
def getCheckpointLocations(source,destination,day,time):
json_checkpoints = getCheckpoints(source,destination)
json_checkpoint_locations = []
for i in json_checkpoints:
url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + str(i['lat']) + ','+ str(i['lng']) + '&key=' + YOUR_API_KEY
response = urlopen(url)
json_obj = json.load(response)
results = json_obj['results']
addr_component = results[0]
address = addr_component['formatted_address']
json_checkpoint_locations.append(address)
return json_checkpoint_locations
def insertRouteIntoDb(source,destination,day,time):
location_addr = getCheckpointLocations(source,destination,day,time)
locations = []
loc_comp = []
for loc in location_addr:
loc_comp = loc.split(',')
locations.append(loc_comp[0])
delim = ","
locations_string = delim.join(locations)
g.db.execute('insert into places values (?, ?, ?)',[source[2], destination[2], locations_string])
g.db.commit()
return locations
def getTrafficTweetsForRoute(locations,day,time):
twittrafic = []
for loction in locations:
cmd = "select tweet from tweets where tweet like '%" + loction + "%' and Tdate='" + day + "'"
cur = g.db.execute(cmd)
for row in cur.fetchall():
twittrafic.append(row)
return twittrafic