-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
167 lines (146 loc) · 5.82 KB
/
Copy pathapp.py
File metadata and controls
167 lines (146 loc) · 5.82 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
161
162
163
164
165
166
167
from flask import Flask, render_template, request, redirect, url_for
from defines import getCreds
from instagramGraphAPI_test import getUserPages, getBusinessAccount, getUserMedia, getCarouselAlbum, businessDiscovery, getURLById, createMediaObject, publishMedia, getMediaObjectStatus, getLocationId, getMediaInsights, getUserInsights, getLongLiveToken
import json
import time
app=Flask(__name__)
@app.route("/")
def index():
params = getCreds()
response = getUserPages(params)
data=dict()
data['json_data'] = json.loads(response.content)
return render_template("index.html", response=data['json_data']['data'], length=len(data['json_data']['data']))
@app.route("/getLongLiveToken")
def longLiveToken():
params = getCreds()
response = getLongLiveToken(params)
data=dict()
data['json_data'] = json.loads(response.content)
return render_template("longLiveToken.html", response=data['json_data'])
@app.route("/getPages/")
def getPages():
params = getCreds()
response = getUserPages(params)
data=dict()
data['json_data'] = json.loads(response.content)
return render_template("userPages.html", response=data['json_data']['data'], length=len(data['json_data']['data']))
@app.route("/getBusiness/")
def getAccount():
params = getCreds()
response = getBusinessAccount(params)
data=dict()
data['json_data'] = json.loads(response.content)
return render_template("getBusinessAccount.html", response=data['json_data'])
@app.route("/media/")
def getMedia():
params = getCreds()
response = getUserMedia(params, params['instagram_account_id'])
data=dict()
data['json_data'] = json.loads(response.content)
return render_template("getMedia.html", response=data['json_data']['data'], length=len(data['json_data']['data']))
@app.route("/businessDiscovery/")
def getUsername():
return render_template("getUsername.html")
@app.route("/businessAccountData/", methods = ['GET', 'POST'])
def business_discovery():
if request.method=='POST':
username = request.form.get("username")
else:
return redirect(url_for("getUsername"))
params = getCreds()
response = businessDiscovery(params, username)
data=dict()
data['business_discovery'] = json.loads(response.content)
length=len(data['business_discovery']['business_discovery']['media']['data'])
for media in data['business_discovery']['business_discovery']['media']['data']:
try:
if media['media_type']=='CAROUSEL_ALBUM':
for child in media['children']['data']:
child['media_url']=getURLById(params, child['id'])
elif media['media_type']=='VIDEO':
media['thumbnail_url']=getURLById(params, media['id'])
except:
media['thumbnail_url']='#'
return render_template("businessDiscovery.html", response=data, length=length)
@app.route("/getPostData/")
def getPostData():
return render_template("getPostData.html")
@app.route("/postImage/", methods = ['GET', 'POST'])
def postPhotos():
if request.method=='POST':
media_url = str(request.form.get("post_url"))
usernames = str(request.form.get("user_tags"))
location = str(request.form.get("location"))
caption = str(request.form.get("caption"))
else:
return redirect(url_for('getPostData'))
params = getCreds()
response=list()
locationResponse = getLocationId(params, location)
locationData = json.loads(locationResponse.content)
response.append(locationData)
try:
location_id = locationData['data'][0]['id']
params['location_id']=location_id
except:
params['location_id'] = '7640348500'
params['media_type'] = 'IMAGE'
params['media_url'] = media_url
params['caption'] = caption
usernames = list(usernames.split())
inc=1/len(usernames)
x=inc-0.01
y=inc-0.01
user_tags=[]
for username in usernames:
user=dict()
user['username']=username
user['x']=round(x,1)
user['y']=round(y,1)
x+=inc
y+=inc
user_tags.append(user)
user_tags = json.dumps(user_tags)
params['user_tags']=user_tags
imageMediaObjectResponse = createMediaObject( params )
IG_Container = json.loads(imageMediaObjectResponse.content)
response.append(IG_Container)
creation_id=IG_Container['id']
imageMediaStatus='IN_PROGRESS'
while imageMediaStatus!='FINISHED':
imageMediaStatusResponse=getMediaObjectStatus(creation_id, params)
data = json.loads(imageMediaStatusResponse.content)
imageMediaStatus = data['status_code']
response.append(data)
time.sleep(5)
publishMediaResponse=publishMedia(params, creation_id)
data=json.loads(publishMediaResponse.content)
response.append(data)
# return render_template("postImage.html", response=response)
return redirect(url_for('getMedia'))
@app.route("/getMediaId/")
def getMediaId():
return render_template("getMediaId.html")
@app.route("/getMediaInsights/", methods = ['GET', 'POST'])
def mediaInsights():
'''
if 'VIDEO' == response['json_data']['data'][0]['media_type'] : # media is a video
params['metric'] = 'engagement,impressions,reach,saved,video_views'
else : # media is an image
params['metric'] = 'engagement,impressions,reach,saved'
'''
if request.method=='POST':
media_id = request.form.get("media_id")
else:
return redirect(url_for('getMediaId'))
params = getCreds()
response = getMediaInsights(params, media_id)
data=json.loads(response.content)
return render_template("getMediaInsights.html", response=data)
@app.route("/getUserInsights/")
def userInsights():
params = getCreds()
response = getUserInsights(params)
data=json.loads(response.content)
return render_template("getUserInsights.html", response=data)