-
Notifications
You must be signed in to change notification settings - Fork 58
/
share_war.py
437 lines (351 loc) · 13.5 KB
/
share_war.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
import selenium, time, argparse, sys, os, textwrap
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def rt(d):
times = np.random.rand(1000)+np.random.rand(1000)+d
return np.random.choice(times, 1).tolist()[0]
def login(debugger=False):
if debugger is True:
import pdb; pdb.set_trace()
else:
pass
url = "https://poshmark.com/login"
driver.get(url)
time.sleep(rt(5))
try:
## Login
print(textwrap.dedent('''
[*] logging into Poshmark seller account: {}...
the share war will begin momentarily...
'''.format(poshmark_username)))
username = driver.find_element_by_name("login_form[username_email]")
username.send_keys(poshmark_username)
time.sleep(rt(5))
password = driver.find_element_by_name("login_form[password]")
password.send_keys(poshmark_password)
time.sleep(rt(5))
password.send_keys(Keys.RETURN)
time.sleep(rt(5))
## Check for Captcha
try:
captcha_pat = "//span[@class='base_error_message']"
captcha_fail = driver.find_element_by_xpath(captcha_pat)
if len(str(captcha_fail)) > 100:
print(textwrap.dedent('''
[*] caught by captchas...
[*] please complete captchas
robots game before proceeding...
[*] please proceed to terminal debugger
'''))
login(debugger=True)
return
else:
pass
except Exception as e:
pass
## Navigate to Seller Page
time.sleep(rt(10))
seller_page = get_seller_page_url(args.account)
driver.get(seller_page)
## Confirm Account to Share If Not Username
if args.bypass == True:
pass
else:
if args.account != poshmark_username:
confirm_account_sharing(args.account, poshmark_username)
if quit_input is True:
return False
else:
pass
else:
pass
return True
except:
## Captcha Catch
print(textwrap.dedent('''
[*] ERROR in Share War: Thrwarted by Captchas
you may now attempt to login with the python debugger
'''))
offer_user_quit()
login(debugger=True)
pass
def confirm_account_sharing(account, username):
## Get User Input
print(textwrap.dedent('''
[*] you have requested to share
the items in another poshmark closet:
------------------------------------
[*]: {}
------------------------------------
'''.format(account)))
confirm_mes = (textwrap.dedent('''
[*] to confirm this request, enter [y]
to cancel and share your closet items instead enter [n] :
'''))
confirm_selection = input(confirm_mes)
cs = str(confirm_selection).lower()
if cs == 'y':
pass
elif cs == 'n':
## Redirect to users's closet page
seller_page = get_seller_page_url(username)
driver.get(seller_page)
else:
print('[*] you have entered an invalid selection...')
offer_user_quit()
if quit_input is True:
pass
else:
confirm_account_sharing(account, username)
def offer_user_quit():
## Provide Option to Quit
quit_mes = textwrap.dedent('''
[*] if you would like to quit, enter [q]
otherwise, enter any other key to continue
''')
quit_selection = input(quit_mes)
qs = str(quit_selection).lower()
if qs == 'q':
global quit_input
quit_input = True
else:
pass
def get_seller_page_url(poshmark_account):
url_stem = 'https://poshmark.com/closet/'
available = '?availability=available'
url = '{}{}{}'.format(url_stem, poshmark_account, available)
return url
def scroll_page(n, delay=3):
scroll = 0
screen_heights = [0]
print("[*] scrolling through all items in closet...")
for i in range(1, n+1):
scroll +=1
scroll_script = "window.scrollTo(0, document.body.scrollHeight);"
driver.execute_script(scroll_script)
height = driver.execute_script("return document.documentElement.scrollHeight")
last_height = screen_heights[-1:][0]
if height == last_height:
return
else:
screen_heights.append(height)
time.sleep(rt(delay))
def get_closet_urls():
items = driver.find_elements_by_xpath("//div[@class='item-details']")
urls = [i.find_element_by_css_selector('a').get_attribute('href') for i in items]
return urls
def get_closet_share_icons():
item_pat = "//div[@class='social-info social-actions d-fl ai-c jc-c']"
items = driver.find_elements_by_xpath(item_pat)
share_icons = [i.find_element_by_css_selector("a[class='share']") for i in items]
return share_icons
def clicks_share_followers(share_icon, d=4.5):
## First share click
driver.execute_script("arguments[0].click();", share_icon); time.sleep(rt(d))
## Second share click
share_pat = "//a[@class='pm-followers-share-link grey']"
share_followers = driver.find_element_by_xpath(share_pat)
driver.execute_script("arguments[0].click();", share_followers); time.sleep(rt(d))
def open_closet_item_url(url):
print(url)
driver.get(url)
time.sleep(rt(5))
def deploy_share_war(n=3, order=True, random_subset=0):
print("[*] DEPLOYING SHARE WAR")
try:
if login() is True:
pass
else:
return
scroll_page(n)
## Share Icons and Order
share_icons = get_closet_share_icons()
if order is True:
share_icons.reverse()
else:
pass
## Share Random Subset of Items
if random_subset != 0:
try:
random_subset = int(random_subset)
print(textwrap.dedent('''
[*] you have selected to share a random subset of {} items
from all {} PoshMark listings in the closet...
please wait...
'''.format(random_subset, len(share_icons))))
share_icons = np.random.choice(share_icons, random_subset, replace=False).tolist()
except:
pass
else:
pass
## Share Message
print(textwrap.dedent('''
[*] sharing PoshMark listings for {} items in closet...
please wait...
'''.format(len(share_icons))))
## Share Listings
[clicks_share_followers(item) for item in share_icons]
print("[*] closet successfully shared...posh-on...")
pass
except:
print("[*] ERROR in Share War")
pass
## Closing Message
loop_delay = int(random_loop_time/60)
current_time = time.strftime("%I:%M%p on %b %d, %Y")
print(textwrap.dedent('''
[*] the share war will continue in {} minutes...
current time: {}
'''.format(loop_delay, current_time)))
if __name__=="__main__":
##################################
## Arguments for Script
##################################
## Default Arguments with RawTextHelpFormatter
class RawTextArgumentDefaultsHelpFormatter(
argparse.ArgumentDefaultsHelpFormatter,
argparse.RawTextHelpFormatter
):
pass
## Check to ensure user has created the credentials.py file
exists = os.path.isfile('./credentials.py')
if not exists:
print(textwrap.dedent('''
[*] ERROR: `credentials.py` file does not exist.
You may need to create the file, for example,
by copying `example_credentials.py`...
[*] In terminal, enter the following command:
cp example_credentials.py credentials.py
[*] Then edit credentials.py with your
poshmark closet and password.
'''))
sys.exit()
else:
import credentials
## Fail gracefully if the username or password not specified
try:
poshmark_username = credentials.poshmark_username
poshmark_password = credentials.poshmark_password
except AttributeError:
print(textwrap.dedent('''
[*] ERROR: Username and/or password not specified...
[*] You may need to uncomment poshmark_username and
poshmark_password in credentials.py
'''))
sys.exit()
## Poshmark closet URL only works with username, so verify
## that the user is not using their email address to log in.
if '@' in poshmark_username:
print(textwrap.dedent('''
[*] Do not your user email address to log in...
use your Poshmark username (closet) instead...
'''))
sys.exit()
parser = argparse.ArgumentParser(
description=textwrap.dedent('''
[*] Help file for share_war.py
from the poshmark_sharing repository:
https://github.com/jmausolf/poshmark_sharing
'''),
usage='use "python %(prog)s --help" for more information',
formatter_class=RawTextArgumentDefaultsHelpFormatter)
parser.add_argument("-t", "--time", default=7200, type=float,
help=textwrap.dedent('''\
loop time in seconds to repeat the code
:: example, repeat in two hours:
-t 7200
'''))
parser.add_argument("-n", "--number", default=1000, type=int,
help="number of closet scrolls")
parser.add_argument("-o", "--order", default=True, type=bool,
help="preserve closet order")
parser.add_argument("-r", "--random_subset", default=0, type=int,
help="select a random subset (number) of items to share")
parser.add_argument("-a", "--account", default=poshmark_username,
type=str,help=textwrap.dedent('''\
the poshmark closet account you want to share
(default is the login account in credentials.py)
:: example, share another user's closet items:
-a another_username
'''))
parser.add_argument("-b", "--bypass", default=False, type=bool,
help=textwrap.dedent('''\
option to bypass user confirmation
by default, if the account to be shared is not equal
to the poshmark username, the user will be prompted to
confirm this selection
:: example, bypass user confirmation
-b True
'''))
parser.add_argument("-d", "--driver", default='0', type=str,
help=textwrap.dedent('''\
selenium web driver selection
drivers may be called by either entering the name
of the driver or entering the numeric code
for that driver name as follows:
Firefox==0, Chrome==1, Edge==2, Safari==3
:: example, use Firefox:
-d Firefox
-d 0
:: example, use Chrome:
-d Chrome
-d 1
'''))
args = parser.parse_args()
##################################
## Run Script
##################################
## Start Share War Loop
starttime = time.time()
while True:
## Select and Start Webdriver
try:
## Try to start driver
if args.driver == '0' or args.driver == 'Firefox':
driver = webdriver.Firefox()
elif args.driver == '1' or args.driver == 'Chrome':
driver = webdriver.Chrome()
elif args.driver == '2' or args.driver == 'Edge':
driver = webdriver.Edge()
elif args.driver == '3' or args.driver == 'Safari':
driver = webdriver.Safari()
else:
print(textwrap.dedent('''
[*] ERROR Driver argument value not supported!
Check the help (-h) argument for supported values.
'''))
## Driver Implicit Wait
driver.implicitly_wait(0)
except NameError:
print(textwrap.dedent('''
[*] ERROR You don't have the web driver for argument
given ({}) you need to download it, go here for
installation info:
https://selenium-python.readthedocs.io/installation.html#drivers
'''.format(args.driver)))
sys.exit()
except Exception as e:
print(textwrap.dedent('''
[*] ERROR the selected driver may not be setup correctly.
Ensure you can access it from the command line and
try again.
{}
'''.format(e)))
sys.exit()
else:
pass
## Time Delay: While Loop
random_loop_time = rt(args.time)
## Run Main App
quit_input = False
deploy_share_war(args.number, args.order, args.random_subset)
if quit_input is False:
time.sleep(rt(10))
driver.close()
## Time Delay: While Loop
time.sleep(random_loop_time - ((time.time() - starttime) %
random_loop_time))
else:
driver.close()
sys.exit()