-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsta.py
66 lines (60 loc) · 1.54 KB
/
insta.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
from flask import Response
import json
import requests
from instaloader import Instaloader, Profile
from helpers import default_json_response
# TO DO:
# 1. Add async job schedule in app.py // modify it
# 2. Add security in case when the data hasn't been succesfully donwloaded
# 3. Randomize photo selection
# 4. Check what happends with videos
class InstaScraper:
def __init__(self):
self.gotInitData = False
self.jsonData = json.dumps(default_json_response)
self.loaded = False
self.posts = []
def response(self):
if self.gotInitData:
return Response(
response=self.jsonData,
status=200,
mimetype='application/json'
)
else:
return Response(
response=self.jsonData,
status=400,
mimetype='application/json'
)
def start(self):
data = self.getInstagramData()
self.gotInitData = True
self.jsonData = json.dumps(data)
def getInstagramData(self):
self.posts.clear()
if not self.loaded:
instaLoader = Instaloader()
PROFILENAME = 'umcs_lublin'
profile = Profile.from_username(instaLoader.context, PROFILENAME)
iter = 0
for post in profile.get_posts():
iter += 1
self.posts.append({
'photo_url': post.url,
'caption': post.caption,
'likes': post.likes
})
if iter == 5:
break
request = {
'success': True,
'info': {
'followers': profile.followers
},
'payload': self.posts
}
return request
if __name__ == "__main__":
insta = InstaScraper()
data = json.load(insta.getInstagramData())