-
Notifications
You must be signed in to change notification settings - Fork 1
/
items.py
508 lines (438 loc) · 23.2 KB
/
items.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
from fetch_data import fetch_username_by_id, fetch_profile_pic_by_id, fetch_img_from_url
from format_data import format_timestamp, format_reaction, format_duration, format_link
from rich.console import Console
import requests
class Text:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, text: str, reactions: dict) -> None:
"""
Constructor of the Text class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param text: Text of this item.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._text = text
self._reactions = reactions
def show(self):
# Fetch every data needed.
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
# Check if the item is sent by the owner of the account.
if self._issentbyviewer:
# display the item.
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'{self._text}'
f'{reactions}\n', soft_wrap=True)
else:
# display the item.
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'{self._text}'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class Link:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, text: str, reactions: dict) -> None:
"""
Constructor of the Link class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param text: Text of this item.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._text = text
self._reactions = reactions
def show(self):
# Fetch every data needed.
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
text = format_link(self._text)
reactions = format_reaction(self._session, self._reactions)
# Check if the item is sent by the owner of the account.
if self._issentbyviewer:
# display the item.
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'{text}'
f'{reactions}\n', soft_wrap=True)
else:
# display the item.
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'{text}'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class Clip:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: str,
timestamp: int, first_frame: str, src: str, duration: int, reactions: dict):
"""
Constructor of the Clip class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param src: URL of the clip.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._first_frame = first_frame
self._src = src
self._duration = duration
self._reactions = reactions
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
first_frame = fetch_img_from_url(self._session, self._console, self._first_frame)
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n\n'
f'[link={self._src}]{first_frame}[/]'
f'([#0373fc]{self._duration}[/]s)\n'
f'[bold]Crtl+Clic to open[/]'
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n\n'
f'[link={self._src}]{first_frame}[/]'
f'([#0373fc]{self._duration}[/]s)\n'
f'[bold]Crtl+Clic to open[/]'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class MediaShare:
URL = 'https://www.instagram.com/p/'
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: str,
timestamp: int, post_img: str, url_code: str, caption: str, reactions: dict):
"""
Constructor of the Clip class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param post_img: Image that the post contain.
:param url: URL of to the post.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._post_img = post_img
self._url_code = url_code
self._caption = caption
self._reactions = reactions
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
post_img = fetch_img_from_url(self._session, self._console, self._post_img)
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'[link={self.URL + self._url_code}]{post_img}[/]'
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'[link={self.URL + self._url_code}]{post_img}[/]'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class VoiceMedia:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, audio_src: str, audio_duration: int, reactions: dict):
"""
Constructor of the Clip class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param audio_src: Url of the audio.
:param audio_duration: Duration in millisecond of the audio.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._src = audio_src
self._duration = audio_duration
self._reactions = reactions
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'[link={self._src}]▶[/] 🔘─────────────────────({format_duration(self._duration)})'
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'[link={self._src}]▶[/] 🔘─────────────────────({format_duration(self._duration)})'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class RavenMedia:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
seen_count: int, timestamp: int, media_type: str, reactions: dict):
"""
Constructor of the RavenMedia class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param session: Session that contain cookies and headers.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param media_type: If the media is video or photo.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._seen_count = seen_count
self._timestamp = timestamp
self._media_type = media_type
self._reactions = reactions
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
seen_text = '[dark_orange](ouvert)[/]' if self._seen_count > 0 else '[blue](à ouvrir)[/]'
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n' +
f'{seen_text} Vous avez envoyé une ' + ('photo.' if self._media_type == 1 else 'vidéo.') +
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n' +
f'{seen_text} {username} a envoyé une ' + ('photo.' if self._media_type == 1 else 'vidéo.') +
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class Media:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, media_type: str, src: str, reactions: dict):
"""
Constructor of the RavenMedia class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param media_type: If the media is video or photo.
:param src: Url that lead to the media.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._media_type = media_type
self._src = src
self._reactions = reactions
def show(self):
if self._media_type == 1:
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
media = fetch_img_from_url(self._session, self._console, self._src)
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'{media}'
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'{media}'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class StoryShare:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, first_frame: str, url: str, reactions: dict):
"""
Constructor of the StoryShare class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param first_frame: First frame of the story (image if the story is an image).
:param url: Url that lead to the story media.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._first_frame = first_frame
self._url = url
self._reactions = reactions
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
# set the text to display as unavailable and check after.
display_text = '[red italic]Cette story n\'est plus disponible...\n[/]'
# Check if the story is available.
if self._first_frame is not None and self._url is not None:
first_frame = fetch_img_from_url(self._session, self._console, self._first_frame)
display_text = f'[link={self._url}]{first_frame}[/]'
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
'Vous avez partagé une story.\n'
f'{display_text}'
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
'A partagé une story.\n'
f'{display_text}'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class ReelShare:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, first_frame: str, text: str, url: str, story_owner_id: str, reactions: dict):
"""
Constructor of the ReelShare class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param issentbyviewer: Boolean that is true if the item is sent by the user, false otherwise.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param first_frame: First frame of the story (image if the story is an image).
:param text: Text that come with the story share.
:param url: Url that lead to the story media.
:param reactions: Contain reactions of every user.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._first_frame = first_frame
self._text = text
self._url = url
self._story_owner_id = story_owner_id
self._reactions = reactions
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
reactions = format_reaction(self._session, self._reactions)
first_frame = fetch_img_from_url(self._session, self._console, self._first_frame)
# set the text to display as unavailable and check after.
display_text = '[red italic]Cette story n\'est plus disponible...\n[/]'
# Check if the story is available.
if self._first_frame is not None and self._url is not None:
first_frame = fetch_img_from_url(self._session, self._console, self._first_frame)
display_text = f'[link={self._url}]{first_frame}[/]'
if self._issentbyviewer:
story_owner = fetch_username_by_id(self._session, str(self._story_owner_id))
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'Vous avez réagi a la story de {story_owner}.\n'
f'{display_text}'
f'{self._text}'
f'{reactions}\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'A réagi a votre story.\n'
f'{display_text}'
f'{self._text}'
f'{reactions}\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
class Placeholder:
def __init__(self, session: requests.Session, console: Console, issentbyviewer: bool, sender_id: int,
timestamp: int, message: str):
"""
Constructor of the Placeholder class.
:param session: Session that contain cookies and headers.
:param console: Console object that contain every info on the current console.
:param sender_id: Instagram ID of the user that send this item.
:param timestamp: Timestamp of this item.
:param message: The message inside the placeholder.
"""
self._session = session
self._console = console
self._issentbyviewer = issentbyviewer
self._sender_id = sender_id
self._timestamp = timestamp
self._message = message
def show(self):
username = fetch_username_by_id(self._session, str(self._sender_id))
profile_pic = fetch_profile_pic_by_id(self._session, str(self._sender_id))
# Display the placeholder message.
if self._issentbyviewer:
self._console.print(f'{profile_pic} ({format_timestamp(self._timestamp)}) {username}\n'
f'[red italic]{self._message}[/]\n', soft_wrap=True)
else:
self._console.print(f'{username} ({format_timestamp(self._timestamp)}) {profile_pic}\n'
f'[red italic]{self._message}[/]\n', soft_wrap=True, justify='right')
def get_timestamp(self):
"""
Function that return the timestamp of the item. Useful to sort items of a thread in the right order.
:return: Timestamp of the item.
"""
return self._timestamp
if __name__ == '__main__':
print('This code is intended to be imported...')