forked from mpawlak2/instalike-instagram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.py
79 lines (60 loc) · 2.34 KB
/
content.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
import random
import json
import spam
import model
class ContentManager:
def __init__(self, operation, repository):
self.operation = operation
self.repository = repository
self.spam_validator = spam.SpamDetector(self.operation, self.repository)
self.tags = ['l4l', 'f4f']
self.photos = []
self.photos_from_model = []
self.users_from_model = []
self.activities = []
def get_photos(self):
self.photos_from_model = []
self.users_from_model = []
no_of_photos = random.randint(2,10)
for tag in self.tags:
try:
print('getting photos from tag {0}...'.format(tag))
self.photos = self.operation.get_photos_by_tag(tag)
except TypeError:
print('oops! someting went wrong while fetching photos')
# get details for each photo
for photo in self.photos:
photo_details = self.operation.get_photo_details(photo['code'])
if (photo_details):
photo_instance = model.Photo().from_json(photo_details)
self.repository.merge_photo(photo_instance)
self.photos_from_model.append(photo_instance)
user_details = self.operation.get_user_details(photo_instance.owner_username.replace('\'',''))
if (user_details):
user_instance = model.User().from_json(user_details)
self.repository.merge_user(user_instance)
self.users_from_model.append(user_instance)
self.filter_photos()
if (len(self.photos_from_model) < no_of_photos):
no_of_photos = len(self.photos_from_model)
self.photos_from_model = random.sample(self.photos_from_model, no_of_photos)
self.users_from_model = self.spam_validator.validate_users(self.users_from_model)
return self.photos_from_model
def get_users(self):
return self.users_from_model
def get_users_to_unfollow(self):
response = self.repository.get_users_to_unfollow()
if(response):
return json.loads(response)
return []
def get_activity(self):
self.activities = []
activities_json = self.operation.get_activity()
if (activities_json):
for json_node in activities_json:
activity_model = model.Activity().from_json(json_node)
self.activities.append(activity_model)
return self.activities
def filter_photos(self):
self.photos_from_model = self.spam_validator.validate_photos(self.photos_from_model)
print('downloaded {0} valid photos'.format(len(self.photos_from_model)))