Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
taux1c
GitHub Repository: taux1c/onlyfans-scraper
Path: blob/main/onlyfans_scraper/interaction/like.py
961 views
1
r"""
2
_ __
3
___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __
4
/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|
5
| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |
6
\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|
7
|___/ |_|
8
"""
9
10
import random
11
import time
12
from typing import Union
13
14
import httpx
15
from revolution import Revolution
16
17
from ..api import posts
18
from ..constants import favoriteEP, postURL
19
from ..utils import auth
20
21
22
def get_posts(headers, model_id):
23
with Revolution(desc='Getting posts...') as _:
24
pinned_posts = posts.scrape_pinned_posts(headers, model_id)
25
timeline_posts = posts.scrape_timeline_posts(headers, model_id)
26
archived_posts = posts.scrape_archived_posts(headers, model_id)
27
28
return pinned_posts + timeline_posts + archived_posts
29
30
31
def filter_for_unfavorited(posts: list) -> list:
32
unfavorited_posts = [post for post in posts if 'isFavorite' in post and not post['isFavorite']]
33
return unfavorited_posts
34
35
36
def filter_for_favorited(posts: list) -> list:
37
favorited_posts = [post for post in posts if 'isFavorite' in post and post['isFavorite']]
38
return favorited_posts
39
40
41
def get_post_ids(posts: list) -> list:
42
ids = [post['id'] for post in posts if 'isOpened' in post and post['isOpened']]
43
return ids
44
45
46
def like(headers, model_id, username, ids: list):
47
_like(headers, model_id, username, ids, True)
48
49
50
def unlike(headers, model_id, username, ids: list):
51
_like(headers, model_id, username, ids, False)
52
53
54
def _like(headers, model_id, username, ids: list, like_action: bool):
55
title = "Liking" if like_action else "Unliking"
56
with Revolution(desc=f'{title} posts...', total=len(ids)) as rev:
57
for i in ids:
58
with httpx.Client(http2=True, headers=headers) as c:
59
url = favoriteEP.format(i, model_id)
60
61
auth.add_cookies(c)
62
c.headers.update(auth.create_sign(url, headers))
63
64
retries = 0
65
while retries <= 1:
66
time.sleep(random.uniform(0.8, 0.9))
67
retries += 1
68
try:
69
r = c.post(url)
70
if not r.is_error or r.status_code == 400:
71
break
72
else:
73
_handle_err(r, postURL.format(i, username))
74
except httpx.TransportError as e:
75
_handle_err(e, postURL.format(i, username))
76
rev.update()
77
78
79
def _handle_err(param: Union[httpx.Response, httpx.TransportError], url: str) -> str:
80
message = 'unable to execute action'
81
status = ''
82
try:
83
if isinstance(param, httpx.Response):
84
json = param.json()
85
if 'error' in json and 'message' in json['error']:
86
message = json['error']['message']
87
status = f'STATUS CODE {param.status_code}: '
88
else:
89
message = str(param)
90
except:
91
pass
92
print(f'{status}{message}, post at {url}')
93
94