Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
taux1c
GitHub Repository: taux1c/onlyfans-scraper
Path: blob/main/onlyfans_scraper/api/profile.py
961 views
1
r"""
2
_ __
3
___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __
4
/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|
5
| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |
6
\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|
7
|___/ |_|
8
"""
9
10
from datetime import datetime
11
from itertools import zip_longest
12
13
import httpx
14
15
from ..constants import profileEP
16
from ..utils import auth, dates, encoding
17
18
19
def scrape_profile(headers, username) -> dict:
20
with httpx.Client(http2=True, headers=headers) as c:
21
url = profileEP.format(username)
22
23
auth.add_cookies(c)
24
c.headers.update(auth.create_sign(url, headers))
25
26
r = c.get(profileEP.format(username), timeout=None)
27
if not r.is_error:
28
return r.json()
29
r.raise_for_status()
30
31
32
def parse_profile(profile: dict) -> tuple:
33
media = []
34
if (avatar := profile['avatar']):
35
media.append((avatar,))
36
if (header := profile['header']):
37
media.append((header,))
38
# media_urls = list(zip_longest(media, [], fillvalue=None))
39
40
name = encoding.encode_utf_16(profile['name'])
41
username = profile['username']
42
id_ = profile['id']
43
join_date = dates.convert_date_to_mdy(profile['joinDate'])
44
posts_count = profile['postsCount']
45
photos_count = profile['photosCount']
46
videos_count = profile['videosCount']
47
audios_count = profile['audiosCount']
48
archived_posts_count = profile['archivedPostsCount']
49
info = (
50
name, username, id_, join_date,
51
posts_count, photos_count, videos_count, audios_count, archived_posts_count)
52
53
return (media, info)
54
55
56
def print_profile_info(info):
57
header_fmt = 'Name: {} | Username: {} | ID: {} | Joined: {}\n'
58
info_fmt = '- {} posts\n -- {} photos\n -- {} videos\n -- {} audios\n- {} archived posts'
59
final_fmt = header_fmt + info_fmt
60
print(final_fmt.format(*info))
61
62
63
def get_id(headers, username):
64
with httpx.Client(http2=True, headers=headers) as c:
65
url = profileEP.format(username)
66
67
auth.add_cookies(c)
68
c.headers.update(auth.create_sign(url, headers))
69
70
r = c.get(url, timeout=None)
71
if not r.is_error:
72
return r.json()['id']
73
r.raise_for_status()
74
75