Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
taux1c
GitHub Repository: taux1c/onlyfans-scraper
Path: blob/main/onlyfans_scraper/api/paid.py
961 views
1
r"""
2
_ __
3
___ _ __ | | _ _ / _| __ _ _ __ ___ ___ ___ _ __ __ _ _ __ ___ _ __
4
/ _ \ | '_ \ | || | | || |_ / _` || '_ \ / __| _____ / __| / __|| '__| / _` || '_ \ / _ \| '__|
5
| (_) || | | || || |_| || _|| (_| || | | |\__ \|_____|\__ \| (__ | | | (_| || |_) || __/| |
6
\___/ |_| |_||_| \__, ||_| \__,_||_| |_||___/ |___/ \___||_| \__,_|| .__/ \___||_|
7
|___/ |_|
8
"""
9
from urllib.request import urlopen
10
11
from ..constants import purchased_contentEP
12
from ..utils import auth
13
import httpx
14
import pathlib
15
from ..utils.config import read_config
16
from hashlib import md5
17
import sqlite3 as sql
18
config = read_config()['config']
19
paid_content_list_name = 'list'
20
21
save_location = pathlib.Path(config.get('save_location'), 'Paid Content')
22
save_location.mkdir(parents=True,exist_ok=True)
23
# SQL SETUP
24
25
db = sql.connect(pathlib.Path(save_location, 'paid.db'))
26
cursor = db.cursor()
27
28
create_table_command = "CREATE TABLE IF NOT EXISTS hashes(id integer PRIMARY KEY, hash text, file_name text)"
29
30
31
32
def add_to_db(hash,file_name):
33
"""Returns True if hash was not in the database and file can continue."""
34
cursor.execute(create_table_command)
35
db.commit()
36
cursor.execute(f"SELECT * FROM hashes WHERE hash='{hash.hexdigest()}'")
37
results = cursor.fetchall()
38
if len(results) > 0:
39
print("Working in the background. Simmer down.")
40
return False
41
cursor.execute("""INSERT INTO hashes(hash,file_name) VALUES(?,?)""",(hash.hexdigest(),file_name))
42
db.commit()
43
return True
44
45
46
47
48
49
50
51
52
def scrape_paid():
53
"""Takes headers to access onlyfans as an argument and then checks the purchased content
54
url to look for any purchased content. If it finds some it will return it as a list."""
55
media_to_download = []
56
offset = 0
57
hasMore = True
58
headers = auth.make_headers(auth.read_auth())
59
with httpx.Client(http2=True, headers=headers, follow_redirects=True) as c:
60
while hasMore:
61
headers = auth.make_headers(auth.read_auth())
62
auth.add_cookies(c)
63
url = purchased_contentEP.format(offset)
64
offset += 10
65
c.headers.update(auth.create_sign(url, headers))
66
r = c.get(url, timeout=None)
67
if not r.is_error:
68
if "hasMore" in r.json():
69
hasMore = r.json()['hasMore']
70
# THIS NEEDS TO BE REWORKED TO WORK LIKE HIGHLIGHTS AND FIGURE OUT THE LIST NAME HAVEN'T HAD TIME.
71
for item in r.json()[paid_content_list_name]:
72
for i in item['media']:
73
if "source" in i:
74
media_to_download.append(i['source']['source'])
75
print("Scraping, it isn't frozen. It takes time.")
76
return media_to_download
77
78
79
def download_paid(media):
80
"""Takes a list of purchased content and downloads it."""
81
headers = auth.make_headers(auth.read_auth())
82
with httpx.Client(http2=True, headers=headers, follow_redirects=True) as c:
83
auth.add_cookies(c)
84
for item in media:
85
r = c.get(item)
86
rheaders = r.headers
87
last_modified = rheaders.get("last-modified")
88
file_name = item.split('.')[-2].split('/')[-1].strip("/,.;!_-@#$%^&*()+\\ ")
89
content_type = rheaders.get("content-type").split('/')[-1]
90
pathlib.Path.mkdir(pathlib.Path(save_location),parents=True,exist_ok=True)
91
file = "{}/{}-{}.{}".format(save_location,file_name,last_modified.replace(':','-'),content_type)
92
hash = md5(r.content)
93
if add_to_db(hash,file_name):
94
with open(file, 'wb') as f:
95
print("Downloading: {}".format(file))
96
f.write(r.content)
97
98
99
100
101
102
103
104
105
106