-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
172 lines (154 loc) · 5.49 KB
/
main.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
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
168
169
170
171
172
#!/usr/bin/python
import change
from flask import render_template, Flask, request, json
from googletrans import Translator
import requests
translator = Translator()
import requests
from bs4 import BeautifulSoup
from igramscraper.instagram import Instagram
from flask_cors import CORS
from firebase import firebase
from datetime import date
from datetime import datetime
app = Flask(__name__)
CORS(app)
langs = ['gu','bn','hi','kn','ta','te','en']
@app.route('/')
def home():
return "hello"
# @app.route('/translate', methods=['POST', 'GET'])
# def translate():
# if request.method == 'POST':
# text = request.form['text']
# captions = {}
# for lan in langs:
# trans = []
# for word in text.split(' '):
# t = translator.translate(word, dest=lan)
# trans.append(t.text)
# translated = ' '.join(trans)
# captions[lan] = translated
# return render_template('trans.html', captions=captions)
# else:
# return render_template('trans.html')
@app.route('/insta/<username>')
def getigStats(username):
instagram = Instagram()
data = { 'account': {}}
account = instagram.get_account(username)
data['account']['id'] = account.identifier
data['account']['username'] = account.username
data['account']['Full name'] = account.full_name
data['account']['Biography'] = account.biography
data['account']['Profile pic url'] = account.get_profile_picture_url()
data['account']['Number of published posts'] = account.media_count
data['account']['Number of followers'] = account.followed_by_count
data['account']['Number of follows'] = account.follows_count
today = date.today().strftime("%Y-%m-%d")
firebse = firebase.FirebaseApplication('https://covidai-1dd78.firebaseio.com/', None)
previous = firebse.get('https://covidai-1dd78.firebaseio.com/covidai-1dd78/followcount/', '')
keys = previous.keys()
print(keys)
newEntry = 0
for key in keys:
if(username == key):
newEntry = 1
for sna in previous[key].keys():
stamp = sna
print(stamp)
Dataurl = 'https://covidai-1dd78.firebaseio.com/covidai-1dd78/followcount/'+username+'/'
flag = 0
previous = firebse.get(Dataurl+stamp, '')
print(previous)
da = previous[-1].get(today, '')
print(da)
if(da):
diff = account.followed_by_count - previous[-1][today]
data['account']['diff'] = diff
flag = 1
print(diff)
if(diff != 0):
previous[-1][today] = account.followed_by_count
snap = str(len(previous) - 1)
print('diff not')
result = firebse.put(Dataurl+stamp, snap, {today:previous[-1][today]})
break
if(flag == 0):
previous.append({ today: account.followed_by_count })
result = firebse.put(Dataurl, stamp, previous)
diff = account.followed_by_count - previous[-1][today]
print('update')
data['account']['diff'] = diff
break
if(newEntry == 0):
print('new entry')
Dataurl = 'https://covidai-1dd78.firebaseio.com/covidai-1dd78/followcount/'+username
firebse.post(Dataurl, [{today: account.followed_by_count}])
return data
@app.route('/insta/<username>/coms')
def getcomments(username):
instagram = Instagram()
data = {}
coms = []
medias = instagram.get_medias(username, 1000)
for x in medias:
comments = instagram.get_media_comments_by_id(x.identifier, 10000)
for comment in comments['comments']:
coms.append(comment.text)
data['comments'] = coms
return data
@app.route('/insta/<username>/likesncoms')
def getlikesncoms(username):
instagram = Instagram()
data = {'like_timeline': {},
'comment_timeline': {}}
total_likes = 0
total_comments = 0
medias = instagram.get_medias(username, 1000)
flag = 0
for x in medias:
timestamp = x.created_time
dh_object = datetime.fromtimestamp(timestamp).strftime("%H")
if(flag == 0):
data['like_timeline'][dh_object] = x.likes_count
data['comment_timeline'][dh_object] = x.comments_count
else:
data['like_timeline'][dh_object] += x.likes_count
data['comment_timeline'][dh_object] += x.comments_count
total_likes += x.likes_count
total_comments += x.comments_count
data['total_likes'] = total_likes
data['total_comments'] = total_comments
return data
@app.route('/insta/<username>/latest')
def getlatest(username):
data = {}
instagram = Instagram()
medias = instagram.get_medias(username, 1)
for x in medias:
data['created_time'] = x.created_time
data['caption'] = x.caption
data['likes_count'] = x.likes_count
data['comments_count'] = x.comments_count
data['image_high_resolution_url'] = x.image_high_resolution_url
data['link'] = x.link
return data
@app.route('/insta/<username>/followtimeline')
def getfollowTimeline(username):
firebse = firebase.FirebaseApplication('https://covidai-1dd78.firebaseio.com/', None)
previous = firebse.get('https://covidai-1dd78.firebaseio.com/covidai-1dd78/followcount/'+username, '')
keys = previous.keys()
for key in keys:
timeline = { 'Followtimeline': previous[key] }
return timeline
@app.route('/trending/<key>')
def getTrending(key):
text = key
URL_indian = "http://best-hashtags.com/hashtag/"+text
r_indian = requests.get(URL_indian)
soup_indian = BeautifulSoup(r_indian.content, 'html.parser')
hashtag_indian = soup_indian.find('p1').getText().split(" ")[1:11]
return {"hashtags": hashtag_indian}
if __name__ == '__main__':
app.run(debug=True)