forked from stevenmirabito/egc-extractor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
extractors.py
638 lines (542 loc) · 25.2 KB
/
extractors.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
from selenium.common.exceptions import NoSuchElementException
import re, json, time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
class Extractor:
@staticmethod
def complete_challenge(browser, email, phonenum):
return None
@staticmethod
def subject():
return ""
@staticmethod
def delay():
pass
@staticmethod
def find_element(browser, list_of_options, default_value):
return_value = default_value
for l in list_of_options:
locator = l.get('id')
func = l.get('method', browser.find_element_by_xpath)
postprocess = l.get('postprocess', lambda s: s.text.strip())
exception = l.get('exception', NoSuchElementException)
try:
return_value = postprocess(func(locator))
if return_value == '':
continue
break
except exception:
pass
return return_value
class StaplesExtractor(Extractor):
@staticmethod
def name():
return "Staples"
@staticmethod
def email():
return ['DoNotReply.Staples@blackhawk-net.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(1).get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.findAll("a", title=re.compile('View Gift'))
urls = []
if len(egc_link) > 0:
for u in egc_link:
urls.insert(0, u['href'])
return urls
@staticmethod
def fetch_codes(browser):
# Get the card amount
try:
card_amount = browser.find_element_by_xpath('//*[@id="main"]/div[1]/div[2]/h2').text.strip()
except NoSuchElementException:
card_amount = 'Unknown'
# card store
card_store = '{} {}'.format(card_amount, browser.find_element_by_id('productName').get_attribute('value'))
# Get the card number
card_code = browser.find_element_by_id('cardNumber').get_attribute('value')
card_pin = browser.find_element_by_id('pinNumber').get_attribute('value')
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class NeweggExtractor(Extractor):
@staticmethod
def name():
return "Newegg"
@staticmethod
def email():
return ['info@newegg.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.findAll("a", text=re.compile('View and Print the card'))
urls = []
if len(egc_link) > 0:
for u in egc_link:
urls.insert(0, u['href'])
return urls
@staticmethod
def fetch_codes(browser):
# card store
card_store = browser.title.strip()
# check if card is ready?
try:
browser.find_element_by_xpath('//*[contains(text(), "Your gift card number is currently being retrieved.")]')
return None
except NoSuchElementException:
pass
# Get the card amount
try:
card_amount = browser.find_element_by_xpath('//*[@id="lblCertAmount"]').text.strip()
except NoSuchElementException:
card_amount = browser.find_element_by_xpath('//*[@id="desktop"]/div[1]/div[2]/div[1]/div/div/div[1]/div[2]/div[1]').text.strip()
# Get the card number
try:
card_code = browser.find_element_by_xpath('//*[@id="imgCertBarCode"]').get_attribute('src').split('CBID=')[1].split('&')[0]
except NoSuchElementException:
try:
card_code = browser.find_element_by_xpath('//*[@id="desktop"]/div[1]/div[2]/div[1]/div/div/div[1]/div[2]/div[3]/div/div').text.strip()
except NoSuchElementException:
try:
datacert = json.loads(browser.find_element_by_id('ids-configuration').get_attribute('data-certificate'))
card_code = datacert['CardNumber']
except (NoSuchElementException, TypeError, KeyError) as e:
card_code = ''
try:
card_pin = browser.find_element_by_xpath('//*[@id="lblPin"]').text.strip()
except NoSuchElementException:
try:
datacert = json.loads(browser.find_element_by_id('ids-configuration').get_attribute('data-certificate'))
card_pin = datacert['Pin']
except (NoSuchElementException, TypeError, KeyError) as e:
try:
card_pin = browser.find_element_by_xpath('//*[@id="desktop"]/div[1]/div[2]/div[1]/div/div/div[1]/div[2]/div[4]').text.split('PIN:')[1].strip()
except NoSuchElementException:
card_pin = ''
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class PPDGExtractor(Extractor):
@staticmethod
def name():
return "PayPal Digital Gifts"
@staticmethod
def email():
return ['gifts@paypal.com']
@staticmethod
def delay():
time.sleep(5)
@staticmethod
def complete_challenge(browser, email, phonenum):
try:
browser.find_element_by_id('captcha-standalone')
x = browser.get_window_position()['x']
if x == -10000:
browser.set_window_position(10,10)
finished = False
while not finished:
try:
wait = WebDriverWait(browser, 10)
wait.until(EC.presence_of_element_located((By.ID, "react-engine-props")))
finished = True
except TimeoutException:
pass
if x == -10000:
browser.set_window_position(-10000,0)
except NoSuchElementException:
return None
@staticmethod
def fetch_payload(msg):
return msg.get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.find("a", text="View My Code")
if egc_link is not None:
# Open the link in the browser
return egc_link['href']
@staticmethod
def fetch_codes(browser):
script_json = json.loads(browser.find_element_by_id('react-engine-props').get_attribute('innerHTML'))
card_store = script_json['cardDetails']['description']
card_amount = script_json['cardDetails']['itemValue']
card_code = script_json['cardDetails']['giftCard']['card_number']
try:
card_pin = script_json['cardDetails']['giftCard']['security_code']
except KeyError:
card_pin = ''
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class CashstarExtractor(Extractor):
@staticmethod
def name():
return "Cashstar"
@staticmethod
def email():
return ['cashstar.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(1).get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.find("a", href=re.compile(".cashstar.com/gift-card/view/"))
if egc_link is not None:
return egc_link['href']
@staticmethod
def complete_challenge(browser, email, phonenum):
try:
browser.find_element_by_id("error-page")
return None
except NoSuchElementException:
pass
emailaddr = browser.find_element_by_id("id_value")
emailaddr.send_keys(email)
wait = WebDriverWait(browser, 3)
while emailaddr.get_attribute("value") != email:
try:
wait.until(EC.text_to_be_present_in_element_value((By.ID, "id_value"), email))
except TimeoutException:
emailaddr.clear()
emailaddr.send_keys(email)
emailaddr.submit()
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#skip, #redeem")))
try:
skip = browser.find_element_by_id("skip")
except NoSuchElementException:
skip = browser.find_element_by_id('redeem')
browser.get(skip.get_attribute('href'))
@staticmethod
def fetch_codes(browser):
try:
browser.find_element_by_id("error-page")
return None
except NoSuchElementException:
pass
try:
browser.find_element_by_class_name("promotion")
card_code = "{}{}{}{}".format(
browser.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/div/p[1]/span[1]').text,
browser.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/div/p[1]/span[2]').text,
browser.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/div/p[1]/span[3]').text,
browser.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/div/p[1]/span[4]').text
)
card_pin = browser.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/div/p[2]').text[5:]
card_amount = browser.find_element_by_xpath(
'//*[@id="container"]/div[1]/div[2]/div[2]/div[1]/div/div[1]/div/div/p[3]').text[8:-4]
card_store = browser.find_element_by_class_name("header").find_element_by_css_selector('a').get_attribute('href')
except NoSuchElementException:
card_code = "{}{}{}{}".format(
browser.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div[2]/p[1]/span[1]').text,
browser.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div[2]/p[1]/span[2]').text,
browser.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div[2]/p[1]/span[3]').text,
browser.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div[2]/p[1]/span[4]').text,
)
card_pin = browser.find_element_by_xpath('//*[@id="container"]/div[1]/div[2]/div[3]/div[2]/p[2]').text[5:]
card_amount = ''
card_store = browser.find_element_by_class_name("header").find_element_by_css_selector('a').get_attribute('href')
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class SamsungPayExtractor(Extractor):
@staticmethod
def name():
return "Samsung Pay"
@staticmethod
def email():
return ['no-reply@samsungpay.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(1).get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.find('img', src='http://giftcard-art.prod.looppay.com/redeem-button.png')
if egc_link is not None:
return egc_link.parent['href']
@staticmethod
def fetch_codes(browser):
if "gyft" in browser.current_url:
# card store
try:
card_store = browser.find_element_by_xpath('/html/body/main/aside/table/tbody/tr/td[2]/h6[2]').text
except NoSuchElementException:
card_store = 'unknown'
# card amount
try:
card_amount = browser.find_element_by_xpath('/html/body/main/aside/table/tbody/tr/td[2]/h6[1]').text
except NoSuchElementException:
card_amount = 'unknown'
# card number
try:
card_code = browser.find_element_by_xpath('/html/body/main/aside/div[5]/div/div[2]/div[2]').text
except NoSuchElementException:
card_code = 'unknown'
# card pin
try:
card_pin = browser.find_element_by_xpath('/html/body/main/aside/div[5]/div/div[4]/div[2]').text
except NoSuchElementException:
card_pin = ''
else:
# card store
card_store = Extractor.find_element(browser,
[
{'id': 'retailerName', 'method': browser.find_element_by_name,
'postprocess': lambda s: s.get_attribute('value')},
{'id': '//*[@id="main"]/div[1]/div[1]/img',
'postprocess': lambda s: s.get_attribute('alt')},
{'id': '/html/head/title',
'postprocess': lambda s: s.get_attribute('innerHTML')},
],
'Unknown Brand'
)
# Get the card amount
card_amount = Extractor.find_element(browser,
[
{'id': '//*[@id="amount"]'},
{'id': '//*[@id="card-details"]/b[2]'},
{'id': '//*[@id="main"]/div[2]/div[1]/h1/span',
'postprocess': lambda s: re.search('\$\d+', s.text.strip()).group(0)},
{'id': '//*[@id="main"]/div[1]/div[2]/h2'},
{'id': '//*[@id="egc-amount"]'},
{'id': '//*[@id="main"]/div[1]/div[1]/h1',
'postprocess': lambda s: re.search('\$\d+', s.text.strip()).group(0)},
],
'Unknown Value')
# Get the card number
card_code = Extractor.find_element(browser,
[
{'id': 'cardNumber', 'method': browser.find_element_by_name,
'postprocess': lambda s: s.get_attribute('value')},
{'id': '//*[@id="cardNumber2"]'},
{'id': '//*[@id="main"]/div[2]/div/p/span'},
{'id': '//*[@id="cardNumber3"]'}
],
'Unknown Card Number')
# Get the card PIN. Leave blank if we can't find it, as some cards don't have PINs
card_pin = Extractor.find_element(browser,
[
{'id': 'pinNumber', 'method': browser.find_element_by_name,
'postprocess': lambda s: s.get_attribute('value')},
{'id': '//*[@id="main"]/div[2]/div[2]/p[2]/span'},
{'id': '//*[@id="Span2"]'},
{'id': '//*[@id="pin-num"]/span'}
],
'')
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class AmazonExtractor(Extractor):
@staticmethod
def name():
return "Amazon"
@staticmethod
def email():
return ['gc-orders@gc.email.amazon.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(1).get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.find('img', alt='Get Gift Card')
if egc_link is not None:
return egc_link.parent['href']
@staticmethod
def fetch_codes(browser):
# card store
card_store = Extractor.find_element(browser,
[
{'id': '//*[@id="main"]/div[1]/div[1]/img',
'postprocess': lambda s: s.get_attribute('alt')},
{'id': '//*[@id="main"]/h1/strong'},
{'id': '/html/head/title',
'postprocess': lambda s: s.get_attribute('innerHTML')}
],
'Unknown Store')
#
# card_store = browser.find_element_by_xpath('//*[@id="main"]/div[1]/div[1]/img').get_attribute("alt")
# if card_store == "":
# card_store = browser.find_element_by_xpath('//*[@id="main"]/h1/strong').text.strip()
# Get the card amount
card_amount = Extractor.find_element(browser,
[
{'id': '//*[@id="main"]/div[1]/div[2]/h2'},
{'method': browser.find_element_by_id,
'id': 'amount'},
{'id': '//*[@id="main"]/div[2]/div/p[2]',
'postprocess': lambda s: re.search('.*?(\$\d*).*',
s.text.strip()).group(1)},
],
'Unknown Amount')
# try:
# card_amount = browser.find_element_by_xpath('//*[@id="main"]/div[1]/div[2]/h2').text.strip()
# except NoSuchElementException:
# card_amount = browser.find_element_by_id("amount").text.strip()
# Get the card number
card_code = Extractor.find_element(browser,
[
{'id': '//*[@id="cardNumber2"]'},
{'id': '//*[@id="redeem"]'}
],'Unknown Code')
#
# card_code = browser.find_element_by_xpath('//*[@id="cardNumber2"]').text.strip()
card_pin = Extractor.find_element(browser, [{'id': '//*[@id="main"]/div[2]/div[2]/p[2]/span'}], '')
if card_pin == card_code:
card_pin = Extractor.find_element(browser, [{'id': '//*[@id="claimCode"]'}], '')
# try:
# card_pin = browser.find_element_by_xpath('//*[@id="main"]/div[2]/div[2]/p[2]/span').text.strip()
# except NoSuchElementException:
# card_pin = ''
# if card_pin == card_code:
# try:
# card_pin = browser.find_element_by_xpath('//*[@id="claimCode"]').text.strip()
# except NoSuchElementException:
# pass
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class GiftCardMallExtractor(Extractor):
@staticmethod
def name():
return "Gift Card Mall"
@staticmethod
def email():
return ['gcm-support@giftcardmall.com', 'customerservice@giftcardmall.com', 'gcm-cust-serv@giftcardmall.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.findAll("a", {"class": "email-btn-link"})
if len(egc_link) == 0:
# egc_link = msg_parsed.findAll("a", text=re.compile("Click to View"))
egc_link = msg_parsed.findAll("a", text=re.compile("Click to Access"))
if len(egc_link) == 0:
egc_link = msg_parsed.findAll('a', {'class': 'activation-spot-url'})
urls = []
if len(egc_link) > 0:
for u in egc_link:
urls.insert(0, u['href'])
return urls
@staticmethod
def fetch_codes(browser):
# card store
card_store = Extractor.find_element(browser,
[{'id':'productName',
'method': browser.find_element_by_id,
'postprocess': lambda s: s.get_attribute('value')
}],
'Unknown Brand')
# card_store = browser.find_element_by_id('productName').get_attribute('value')
card_amount = Extractor.find_element(browser,
[
{'id': '//*[@id="main"]/div[1]/div[1]/h1',
'postprocess': lambda s: re.search('.*?(\$\d*).*', s).group(1)},
{'id': '//*[@id="main"]/div[1]/div[2]/h2'}
],
'Unknown Amount')
card_code = Extractor.find_element(browser,
[
{'method': browser.find_element_by_id,
'id': 'cardNumber',
'postprocess': lambda s: s.get_attribute('value')}
],
'Unknown Code')
card_pin = Extractor.find_element(browser,
[{'id':'pinNumber',
'method': browser.find_element_by_id,
'postprocess': lambda s: s.get_attribute('value')
}],
'')
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class BestBuyExtractor(Extractor):
@staticmethod
def name():
return "Best Buy"
@staticmethod
def complete_challenge(browser, email, phonenum):
try:
browser.find_element_by_id('phoneNumberChallenge')
except NoSuchElementException:
return None
phone = browser.find_element_by_name('phone')
phone_dashed = phonenum[0:3]+"-"+phonenum[3:6]+"-"+phonenum[6:10]
phone.send_keys(phonenum)
wait = WebDriverWait(browser, 15)
while phone.get_attribute("value") != phone_dashed:
try:
wait.until(EC.text_to_be_present_in_element_value((By.NAME, "phone"), phone_dashed))
except TimeoutException:
phone.send_keys(phonenum)
phone.submit()
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "gift-card-container")))
@staticmethod
def email():
return ['BestBuyInfo@emailinfo.bestbuy.com', 'bestbuygiftcards@cashstar.com']
# @staticmethod
# def subject():
# return "E-Gift Card"
# return ""
@staticmethod
def fetch_payload(msg):
return msg.get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.findAll('a', text=re.compile('Claim my e-gift card'))
if len(egc_link) == 0:
egc_link = msg_parsed.findAll("a", text=re.compile("Click to View"))
urls = []
if len(egc_link) > 0:
for u in egc_link:
urls.insert(0, u['href'])
return urls
@staticmethod
def fetch_codes(browser):
# card store
card_store = 'Best Buy'
card_code = browser.find_element_by_xpath('//*[@id="app"]/div/div/div[4]/div[2]/div[1]/p').text.strip()
card_pin = browser.find_element_by_xpath('//*[@id="app"]/div/div/div[4]/div[2]/div[2]/p').text.strip()
card_amount = browser.find_element_by_xpath('//*[@id="app"]/div/div/div[4]/div[2]/div[3]/div').text.split(" ")[0].strip()
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
class MyGiftCardsPlusExtractor(Extractor):
@staticmethod
def name():
return "MyGiftCardsPlus"
@staticmethod
def complete_challenge(browser, email, phonenum):
try:
browser.find_element_by_partial_link_text('Login')
except NoSuchElementException:
return None
wait = WebDriverWait(browser, 15)
while browser.title != "Gift Card":
try:
wait.until(EC.title_contains("Gift Card"))
except TimeoutException:
pass
@staticmethod
def email():
return ['ClientCareMGC@mygiftcardsplus.com']
@staticmethod
def fetch_payload(msg):
return msg.get_payload(1).get_payload(decode=True)
@staticmethod
def fetch_url(msg_parsed, browser, email):
egc_link = msg_parsed.findAll('a', text=re.compile('https://www.mygiftcardsplus.com/card'))
urls = []
if len(egc_link) > 0:
for u in egc_link:
urls.insert(0, u['href'])
return urls
@staticmethod
def fetch_codes(browser):
# card store
store_json = json.loads(browser.find_element_by_id('ids-configuration').get_attribute('data-configuration'))
card_store = store_json[0]['settings']['brandName']
script_json = json.loads(browser.find_element_by_id('ids-configuration').get_attribute('data-certificate'))
card_amount = script_json['InitialBalance']
card_code = script_json['CardNumber']
try:
card_pin = script_json['Pin']
except KeyError:
card_pin = ''
return {'card_store': card_store, 'card_amount': card_amount, 'card_code': card_code, 'card_pin': card_pin}
extractors_list = [AmazonExtractor, BestBuyExtractor, CashstarExtractor, SamsungPayExtractor, PPDGExtractor,
NeweggExtractor, StaplesExtractor, GiftCardMallExtractor, MyGiftCardsPlusExtractor]