-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
71 lines (54 loc) · 1.82 KB
/
run.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
from pymongo import MongoClient
import db, os, json, requests, urllib
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import GoogleCredentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "coins.json"
sep = ";"
print "year" + sep + "name" + sep + "boxOfficeId" + sep + "won" + sep + "twitter_mentions"
# connect to mognodb
client = MongoClient(db.conn_string)
db = client.oscar
# grab the application's default credentials from the environment.
credentials = GoogleCredentials.get_application_default()
# construct the service object for interacting with the BigQuery API.
bigquery_service = build("bigquery", "v2", credentials=credentials)
# find all nominees
for data in db.oscar_nominations_extended.find():
if data["film"]:
# fetch boxOfficeId
try:
url_params = urllib.urlencode({"movie": data["film"], "year": str(data["year"])})
resp = requests.get(url="http://boxofficeid.thomasbrueggemann.com/?" + url_params)
boxData = json.loads(resp.text)
boxId = boxData[0]["boxOfficeId"]
except:
boxId = ""
try:
# prepare movie for twitter quers
twit_movie = data["film"].split(" - ")[0].split(":")[0]
# build big query
sql = "SELECT COUNT(user.id_str) FROM [coins_twitter.movie_actor_director] WHERE text LIKE \'%"
sql += twit_movie
sql += "%\';"
# run query
query_request = bigquery_service.jobs()
query_data = {
"query": (sql)
}
query_response = query_request.query(
projectId="coins-1128",
body=query_data
).execute()
# [{u'f': [{u'v': u'51369'}]}]
result = str(data["year"]) + sep
result += data["film"] + sep
result += boxId + sep
if data["won"] == True:
result += "1" + sep
else:
result += "0" + sep
result += query_response["rows"][0]["f"][0]["v"]
print result
except:
pass