forked from NillaMouse/OnlyFans-AutoLiker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautoliker.py
245 lines (225 loc) · 8.67 KB
/
autoliker.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
import os
import sys
import json
import argparse
import logging
import time
import random
import math
from threading import Thread
import requests
from autoliker.constants import (
PROFILE_URL,
POSTS_URL,
POSTS_100_URL,
ARCHIVED_POSTS_URL,
ARCHIVED_POSTS_100_URL,
FAVORITE_URL,
HEADERS,
ICONS
)
class Logger:
FORMAT = '%(levelname)s: %(message)s'
def __init__(self):
self.log = logging.getLogger(name=__name__)
self.log.setLevel(level=logging.DEBUG)
if not self.log.handlers:
_formatter = logging.Formatter(fmt=self.FORMAT)
_sh = logging.StreamHandler()
_sh.setLevel(logging.INFO)
_sh.setFormatter(fmt=_formatter)
self.log.addHandler(hdlr=_sh)
def debug(self, message):
self.log.debug(message)
def info(self, message):
self.log.info(message)
def error(self, message):
self.log.error(message)
class OnlyFans(Logger):
def __init__(self, args):
super().__init__()
# Parse args:
self.timeline = args.timeline
self.archived = args.archived
# Create authentication:
auth_path = os.path.join(sys.path[0], 'auth.json')
with open(auth_path) as f:
auth = json.load(f)['auth']
auth_id = auth['auth_id']
auth_uid_ = auth['auth_uniq_']
if not auth_uid_:
del auth['auth_uniq_']
else:
auth[f"auth_uid_{auth_id}"] = auth.pop('auth_uniq_')
_cookies = [f'{k}={v}' for k, v in auth.items() if k !=
'user_agent' and k != 'app_token']
headers = {
"user-agent": auth['user_agent'], 'cookie': ";".join(_cookies)}
for k, v in HEADERS.items():
headers[k] = v
self.headers = headers
self.app_token = auth['app_token']
# Other constructors:
self.username = args.username
self.unlike = args.unlike
self.id = None
self.has_pinned_posts = None
self.posts_count = None
self.archived_posts_count = None
self.ids = None
self.archived_ids = None
self.stop = False
def scrape_user(self):
with requests.Session() as s:
r = s.get(PROFILE_URL.format(
self.username, self.app_token), headers=self.headers)
self.log.debug(r.status_code)
if r.ok:
user = r.json()
self.id = user['id']
self.has_pinned_posts = user['hasPinnedPosts']
self.posts_count = user['postsCount']
self.archived_posts_count = user['archivedPostsCount']
else:
self.set_stop_true()
self.log.error(
f"Unable to scrape user profile -- Received {r.status_code} STATUS CODE")
def scrape_posts(self, pinned=0, array=[], count=0, cycles=0, time=0):
if not array:
if self.archived:
return None
if self.has_pinned_posts:
with requests.Session() as s:
r = s.get(
POSTS_URL.format(
self.id, self.posts_count, 1, self.app_token), headers=self.headers)
if r.ok:
array = r.json()
else:
self.set_stop_true()
self.log.error(
f'Unable to scrape pinned posts -- Received {r.status_code} STATUS CODE')
if self.posts_count > 100:
cycles = math.floor(self.posts_count / 100)
url = POSTS_URL if not time else POSTS_100_URL
slot = pinned if not time else time
with requests.Session() as s:
r = s.get(url.format(
self.id, self.posts_count, slot, self.app_token),
headers=self.headers)
if r.ok:
posts = r.json()
if cycles:
if count < cycles:
count += 1
list_posts = array + posts
posted_at_precise = posts[-1]['postedAtPrecise']
posts = self.scrape_posts(
array=list_posts, count=count, cycles=cycles, time=posted_at_precise)
if time:
return posts
else:
return array + posts
else:
posts += array
if self.unlike:
posts = [post for post in posts if post['isFavorite']]
else:
posts = [post for post in posts if not post['isFavorite']]
self.ids = [post['id'] for post in posts if post['isOpened']]
else:
self.set_stop_true()
self.log.error(
f'Unable to scrape posts -- Received {r.status_code} STATUS CODE')
def scrape_archived_posts(self, array=[], count=0, cycles=0, time=0):
if not array:
if self.timeline:
return None
if self.archived_posts_count > 100:
cycles = math.floor(self.archived_posts_count / 100)
url = ARCHIVED_POSTS_URL if not time else ARCHIVED_POSTS_100_URL
with requests.Session() as s:
if time:
r = s.get(url.format(
self.id, self.archived_posts_count, time, self.app_token), headers=self.headers)
else:
r = s.get(url.format(
self.id, self.archived_posts_count, self.app_token), headers=self.headers)
if r.ok:
posts = r.json()
if cycles:
if count < cycles:
count += 1
list_posts = array + posts
posted_at_precise = posts[-1]['postedAtPrecise']
posts = self.scrape_archived_posts(
array=list_posts, count=count, cycles=cycles, time=posted_at_precise)
if time:
return posts
else:
return array + posts
else:
posts += array
if self.unlike:
posts = [post for post in posts if post['isFavorite']]
else:
posts = [post for post in posts if not post['isFavorite']]
self.archived_ids = [post['id']
for post in posts if post['isOpened']]
else:
self.set_stop_true()
self.log.error(
f'Unable to scrape posts -- Received {r.status_code} STATUS CODE')
def handle_posts(self, array, message='post'):
if not array:
return None
length = len(array)
enum = enumerate(array, 1)
for c, post_id in enum:
time.sleep(random.uniform(0.8, 1))
with requests.Session() as s:
r = s.post(FAVORITE_URL.format(
post_id, self.id, self.app_token), headers=self.headers)
if r.ok:
if self.unlike:
print(
f'Successfully unliked {message} ({c}/{length})', end='\r', flush=True)
else:
print(
f'Successfully liked {message} ({c}/{length})', end='\r', flush=True)
else:
self.log.error(
f"Unable to like post at 'onlyfans.com/{post_id}/{self.username}' -- Received {r.status_code} STATUS CODE")
def set_stop_true(self):
self.stop = True
def spinner(self):
while True:
for icon in ICONS:
print(icon, end='\r', flush=True)
if self.stop:
return None
time.sleep(0.1)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('username', type=str,
help="username of an OnlyFans content creator (that you're subscribed to)")
parser.add_argument('-t', '--timeline',
help='only like timeline posts', action='store_true')
parser.add_argument('-a', '--archived',
help='only like archived posts', action='store_true')
parser.add_argument('-u', '--unlike',
help='removes your likes from posts', action='store_true')
args = parser.parse_args()
onlyfans = OnlyFans(args)
t1 = Thread(target=onlyfans.spinner)
t1.start()
onlyfans.scrape_user()
onlyfans.scrape_posts()
if onlyfans.archived_posts_count:
onlyfans.scrape_archived_posts()
onlyfans.set_stop_true()
onlyfans.handle_posts(onlyfans.ids)
if onlyfans.archived_posts_count:
onlyfans.handle_posts(onlyfans.archived_ids, 'archived post')
if __name__ == '__main__':
main()