-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterApp.cgi
More file actions
executable file
·160 lines (122 loc) · 5.45 KB
/
twitterApp.cgi
File metadata and controls
executable file
·160 lines (122 loc) · 5.45 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
#!/apps/python/2.7.13/bin/python
##==============================================================================
# file: twitterApp.cgi
# date: Thu Jan 25 00:31:03 GMT 2018
# author(s): Thalita Coleman <thalitaneu@gmail.com>
# abstract: Obtains user twitter keys, calls functions tha retrieves
# tweets data from Twitter API, writes data to file,
# analyses results and dysplays on webpage.
#------------------------------------------------------------------------------
# requirements: python 2.7 with the following modules installed:
# requests, requests_oauthlib, pytz,
#------------------------------------------------------------------------------
##==============================================================================
import os
from os import listdir #only used for action=="testing"
from os.path import isfile, join #only used for action=="testing"
import cgi
import cgitb; cgitb.enable()
import requests
from requests_oauthlib import OAuth1Session
import io
import json
import gzip
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from generateGraphs import *
from generateStats import *
from collectTweets import *
print 'Content-type: text/html\n\n'
form = cgi.FieldStorage()
#hostname = 'ec2-54-146-3-85.compute-1.amazonaws.com'
hostname = 'www.codewithaheart.com'
#pathToAppDir = '/www/default/docs/'
pathToAppDir = '/www/codewithaheart.com/docs/'
client_key = '0XjfIpYCYq9Ve2FLaO5MOVoEv'
client_secret = '2ne1n8nqsCg6eN20RPg0Nbuyt83coyTSGYu6SahqKnPePd8XYF'
callback_uri = 'http://' + hostname + '/twitterApp/twitterApp.cgi?action=step3'
# Endpoints found in the OAuth provider API documentation
request_token_url = 'https://api.twitter.com/oauth/request_token'
authorization_url = 'https://api.twitter.com/oauth/authorize'
access_token_url = 'https://api.twitter.com/oauth/access_token'
# STEP 1: Obtain a request token which will identify you (the client)
# in the next step. At this stage you will only need your client key and secret.
oauth = OAuth1Session(client_key, client_secret=client_secret, callback_uri=callback_uri)
action = form["action"].value
if action=='step1':
fetch_response = oauth.fetch_request_token(request_token_url)
resource_owner_key = fetch_response.get('oauth_token')
resource_owner_secret = fetch_response.get('oauth_token_secret')
# STEP 2: Obtain authorization from the user (resource owner) to access their tweets.
authorize_url = oauth.authorization_url(authorization_url)
#print authorize_url
print '<META http-equiv="refresh" content="0;URL=' + authorize_url + '">'
# STEP 3: Obtain an access token from the OAuth provider. Save this token as it can be re-used later. In this step we will re-use most of the credentials obtained uptil this point.
if action=='step3':
oauth_token = form["oauth_token"].value
oauth_verifier = form["oauth_verifier"].value
#print oauth_token
#print oauth_verifier
oauth = OAuth1Session(client_key, client_secret=client_secret, resource_owner_key=oauth_token, verifier=oauth_verifier)
oauth_tokens = oauth.fetch_access_token(access_token_url)
resource_owner_key = oauth_tokens.get('oauth_token')
resource_owner_secret = oauth_tokens.get('oauth_token_secret')
# STEP 4: Access protected resources. OAuth1 access tokens typically do not expire and may be re-used until revoked by the user or yourself.
settings_url = 'https://api.twitter.com/1.1/account/settings.json'
# Using OAuth1Session
oauth = OAuth1Session(client_key,
client_secret=client_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret)
s = oauth.get(settings_url)
# retrieving screen name and timezone
settings = s.json()
screenName = settings["screen_name"]
if "time_zone" in settings:
tzinfo_name = settings["time_zone"]["tzinfo_name"]
else:
tzinfo_name = 'UTC'
# saving keys to file:
keyToFile = open(pathToAppDir + 'twitterApp/kdata/' + screenName + '.txt', 'w')
keyToFile.write(screenName + ',' + resource_owner_key + ',' + resource_owner_secret + '\n')
# STEP 5: Writes tweets to a json file:
jsonFileName = pathToAppDir + 'twitterApp/json/' + screenName + '.json.gz'
numberTweets = getTweets(client_key, client_secret, resource_owner_key, resource_owner_secret, screenName, jsonFileName)
if numberTweets == 0:
print '<META http-equiv="refresh" content="0;URL=noTweets.html">'
# STEP 6: Analyses results and dysplays on webpage:
generateGraphs(screenName, jsonFileName, tzinfo_name)
# ==================================================
# - TESTING -
# ==================================================
if action == "testing":
print """
<html>
<head>
</head>
<body>
<p> This is a test
<form action="twitterApp.cgi" method="POST">
Select a json file to process:
<br><br>
"""
jsonPath = pathToAppDir + "twitterApp/json"
jsonFiles = [f for f in listdir(jsonPath) if isfile(join(jsonPath, f))]
for file in jsonFiles:
print '<br><input type="radio" name="jsonFile" value="' + file + '">' + file
print """
<input type="hidden" name="action" value="processTestFile">
<br>
<input type="submit" value="process file">
</form>
</body>
</html>
"""
if action == "processTestFile":
jsonPath = pathToAppDir + "twitterApp/json"
screenName = str(form["jsonFile"].value).replace('.json.gz', '')
jsonFileName = jsonPath + "/" + str(form["jsonFile"].value)
tzinfo_name = 'Europe/Oslo'
# analysing user's data
generateGraphs(screenName, jsonFileName, tzinfo_name)