-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
103 lines (66 loc) · 2.76 KB
/
app.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
import flask
import os
import random
import tweepy
import simplejson as json
import requests
idUniques = []
imgUniques = []
#git push heroku master
count = 0
tweets = dict()
tweetsInfo = []
app = flask.Flask(__name__)
@app.route('/') #python decorator
#gets all IDs from json, caps at 20
def index(gettyImg = None):
accessToken = os.getenv("accessToken")
accessTokSec = os.getenv("accessTokSec")
consumer_token = os.getenv("consumer_token")
consumer_secret = os.getenv("consumer_secret")
max_tweets = 30
auth = tweepy.OAuthHandler(consumer_token, consumer_secret)
auth.set_access_token(accessToken, accessTokSec)
api = tweepy.API(auth)
getty_api_key = os.getenv("getty_api_key")
unique = False #boolean used for checking uniques
query = ['ramen', 'college','travel','life'] #terms to query twitter
word = random.choice(query)
url_test = 'https://api.gettyimages.com/v3/search/images?fields=id,title,thumb,referral_destinations&sort_order=best&phrase='+word
headers = {'Api-Key' : getty_api_key}
gettyList = []
count = 0
res = requests.get(url_test, headers=headers)
j=0
data = res.json()
for term in query:
searched_tweets = [status for status in tweepy.Cursor(api.search, q=term, lang="en").items(max_tweets)]
for tweet in searched_tweets:
if (not tweet.retweeted) and (tweet not in tweets) and ('RT @' not in tweet.text) and ('media' not in tweet.entities):
#'https:' not in tweet.text:
tweets[j] = [tweet.text,tweet.id_str,tweet.user.name]
j+=1
# clickPick = tweet.author.profile_image_url_https
for element in data['images']:
print element['id']
global count
count = random.randrange(0, len(tweets))
tString = tweets[count][0]
uStringID = tweets[count][1]
uStringU = tweets[count][2]
#count = count + 1 #Could use for linear iteration through twitter list
for element in data['images']:
gettyList.append(element['id'])
print element['id']
for ids in gettyList:
gettyImg = gettyList[random.randrange(0, len(gettyList))]
gettyImg = random.choice(gettyList)
print "getty img var: "+gettyImg
return flask.render_template("index.html",
tString = tString,
gettyImg= "http://media.gettyimages.com/photos/-id"+gettyImg,uStringID=uStringID,uStringU = uStringU)
#user=user,user_id=user_id
app.run(
port=int(os.getenv('PORT',8080)),
host=os.getenv('IP','0.0.0.0')
)