-
Notifications
You must be signed in to change notification settings - Fork 43
/
datalayer.py
222 lines (179 loc) · 7.37 KB
/
datalayer.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import datetime
from peewee import *
import model
from abc import ABC, abstractmethod
# SQLite database connection
_sqlite_db = SqliteDatabase('instalike.db')
_sqlite_db.connect()
_recreate_tables = True
# Model Definitions
class BaseModel(Model):
class Meta:
database = _sqlite_db
class Photo(BaseModel):
id = IntegerField(primary_key=True)
width = IntegerField()
height = IntegerField()
code = CharField()
is_ad = BooleanField(null=True)
likes_count = IntegerField()
viewer_has_liked = BooleanField(null=True)
is_video = BooleanField(null=True)
display_src = CharField(null=True)
location = CharField(null=True)
owner_id = IntegerField(null=True)
caption = CharField(null=True)
owner_name = CharField(null=True)
creation_date = DateTimeField()
mod_date = DateTimeField()
class User(BaseModel):
id = IntegerField(primary_key=True)
name = CharField()
has_blocked_viewer = BooleanField(null=True)
follows_count = IntegerField()
followers_count = IntegerField()
external_url = CharField(null=True)
follows_viewer = BooleanField()
profile_pic_url = CharField(null=True)
is_private = BooleanField()
full_name = CharField(null=True)
posts_count = IntegerField()
blocked_by_viewer = BooleanField()
followed_by_viewer = BooleanField()
is_verified = BooleanField(null=True)
biography = CharField(null=True)
creation_date = DateTimeField()
mod_date = DateTimeField()
class Like(BaseModel):
id = PrimaryKeyField()
photo_id = IntegerField()
event_time = DateTimeField()
class Follow(BaseModel):
id = PrimaryKeyField()
user_id = IntegerField()
event_time = DateTimeField()
followed = BooleanField()
if _recreate_tables:
if Photo.table_exists():
Photo.drop_table()
if User.table_exists():
User.drop_table()
if Like.table_exists():
Like.drop_table()
if Follow.table_exists():
Follow.drop_table()
if not Photo.table_exists():
Photo.create_table()
if not User.table_exists():
User.create_table()
if not Like.table_exists():
Like.create_table()
if not Follow.table_exists():
Follow.create_table()
""" If you wish to have new persistence plugin derive from this class and implement these methods. """
class InstalikeDataLayer(ABC):
@abstractmethod
def persist_user(self, user: model.User):
pass
@abstractmethod
def persist_photo(self, photo: model.Photo):
pass
@abstractmethod
def persist_like(self, photo: model.Photo):
pass
@abstractmethod
def persist_follow(self, user: model.User):
pass
@abstractmethod
def persist_unfollow(self, user: model.User):
pass
@abstractmethod
def get_users_to_unfollow(self, day_range):
pass
class InstalikeSQLDAO(InstalikeDataLayer):
def persist_user(self, user: model.User):
update = False
if User.select().where(User.id == user.id).exists():
update = True
user_model = User.get(User.id == user.id)
user_model.name = user.username
user_model.has_blocked_viewer = user.has_blocked_viewer
user_model.follows_count = user.follows_count
user_model.followers_count = user.followed_by_count
user_model.external_url = user.external_url
user_model.follows_viewer = user.follows_viewer
user_model.profile_pic_url = user.profile_pic_url
user_model.is_private = user.is_private
user_model.full_name = user.full_name
user_model.posts_count = user.posts_count
user_model.blocked_by_viewer = user.blocked_by_viewer
user_model.followed_by_viewer = user.followed_by_viewer
user_model.is_verified = user.is_verified
user_model.biography = user.biography
else:
user_model = User(id=user.id,
name=user.username,
has_blocked_viewer=user.has_blocked_viewer,
follows_count=user.follows_count,
followers_count=user.followed_by_count,
external_url=user.external_url,
follows_viewer=user.follows_viewer,
profile_pic_url=user.profile_pic_url,
is_private=user.is_private,
full_name=user.full_name,
posts_count=user.posts_count,
blocked_by_viewer=user.blocked_by_viewer,
followed_by_viewer=user.followed_by_viewer,
is_verified=user.is_verified,
biography=user.biography,
creation_date=datetime.datetime.today())
user_model.mod_date = datetime.datetime.today()
return user_model.save() if update else user_model.save(force_insert=True)
def get_users_to_unfollow(self, day_range):
days_ago = datetime.datetime.today() - datetime.timedelta(days=day_range)
print(day_range)
print(days_ago)
users = Follow.select().where((Follow.event_time <= days_ago) & Follow.followed)
return [user.user_id for user in users]
def persist_like(self, photo: model.Photo):
like_model = Like(photo_id=photo.id, event_time=datetime.datetime.today())
return like_model.save()
def persist_follow(self, user: model.User):
follow_model = Follow(user_id=user.id, event_time=datetime.datetime.today(), followed=True)
return follow_model.save()
def persist_photo(self, photo: model.Photo):
update = False
if Photo.select().where(Photo.id == photo.id).exists():
update = True
photo_model = Photo.get(Photo.id == photo.id)
photo_model.width = photo.width
photo_model.height = photo.height
photo_model.is_ad = photo.is_ad
photo_model.likes_count = photo.likes_count
photo_model.viewer_has_like = photo.viewer_has_liked
photo_model.is_video = photo.is_video
photo_model.display_src = photo.display_src
photo_model.location = photo.location
else:
photo_model = Photo(id=photo.id,
width=photo.width,
height=photo.height,
code=photo.code,
is_ad=photo.is_ad,
likes_count=photo.likes_count,
viewer_has_liked=photo.viewer_has_liked,
is_video=photo.is_video,
display_src=photo.display_src,
location=photo.location,
creation_date=datetime.datetime.today())
photo_model.mod_date = datetime.datetime.today()
return photo_model.save() if update else photo_model.save(force_insert=True)
def persist_unfollow(self, user: model.User):
if Follow.select().where(Follow.user_id == user.id).exists():
follow_model = Follow.get(Follow.user_id == user.id)
if follow_model.followed:
follow_model.followed = False
else:
return 0
return follow_model.save()
return 0