-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiveaway_bot.py
executable file
·1293 lines (1045 loc) · 40.6 KB
/
giveaway_bot.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import abc
import configparser
import os
import json
import logging
import multiprocessing
import os
import re
import sys
import time
from datetime import datetime, timedelta
from optparse import OptionParser
from http import cookiejar
from requests.exceptions import TooManyRedirects
import bs4
import requests
try:
import lxml
except ImportError:
PARSER = "html.parser"
else:
PARSER = "lxml"
os.chdir(os.path.dirname(__file__))
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50) Gecko/20100101 Firefox/50.0'
UNIT_TESTS = False
TRAVIS_BUILD = False
class Error(Exception):
pass
class AuthError(Exception):
pass
class ParseError(Exception):
pass
class NoItemsError(Exception):
pass
class ReapError(Exception):
pass
def caching_property(prop):
def wrapped(self):
name = prop.__name__
try:
return getattr(self, 'cached_%s' % name)
except AttributeError:
setattr(self, 'cached_%s' % name, prop(self))
return getattr(self, 'cached_%s' % name)
return wrapped
def retrying(fn):
def wrapped(*args, **kwargs):
obj = args[0]
try:
retries = int(obj.config['retry'])
except KeyError:
retries = 0
try:
timeout = int(obj.config['timeout'])
except KeyError:
timeout = 0
retry = 0
while True:
try:
return fn(*args, **kwargs)
except AuthError:
if retry < retries:
retry += 1
continue
else:
obj._crash("Can't login to %s. Check cookies." % obj.verbose_name)
except ParseError:
if retry < retries:
retry += 1
continue
else:
obj._crash("%s parsing error. Interrupt parsing." % obj.verbose_name)
except NoItemsError:
if retry < retries:
retry += 1
continue
else:
return []
except ReapError:
if retry < retries:
retry += 1
continue
else:
obj._crash("%s reap error. Interrupt reaping." % obj.verbose_name)
except TooManyRedirects:
obj._crash("Can't login to %s. Check cookies." % obj.verbose_name)
except:
if retry < retries:
retry += 1
continue
else:
raise
time.sleep(timeout)
return wrapped
class GiveawayBot:
def __init__(self, log_level):
self.log_level = log_level
self.log = logging.getLogger('Bot')
if not self.log.hasHandlers():
formatter = logging.Formatter('[%(asctime)s][%(levelname)s][%(name)s]: %(message)s',
"%Y-%m-%d %H:%M:%S")
console = logging.StreamHandler()
console.setFormatter(formatter)
self.log.addHandler(console)
self.log.setLevel(self.log_level)
self.config = configparser.ConfigParser()
try:
if TRAVIS_BUILD:
self.config.read_file(open("giveaway_bot.exp"))
else:
self.config.read_file(open("giveaway_bot.ini"))
except FileNotFoundError:
self.log.error("No config file. Please copy «giveaway_bot.exp» as «giveaway_bot.ini» and edit it.")
sys.exit()
self.harvesters = [{"name": "SteamGifts"}, {"name": "IndieGala"}]
self.processes_logs = {}
def start(self):
for harvester in self.harvesters:
enable = int(self.config[harvester['name']]['enable'])
if enable:
queue = multiprocessing.Queue()
self.processes_logs.update({harvester['name']: queue})
process = multiprocessing.Process(target=spawner, name=harvester['name'],
args=(harvester['name'], queue, self.log_level))
process.start()
def stop(self):
for child in multiprocessing.active_children():
child.terminate()
sys.exit()
class Parser(metaclass=abc.ABCMeta):
name = None
verbose_name = None
site_url = None
check_tag = None
check_type = None
check_text = None
cookies = {}
cookies_file = None
def __init__(self, queue, log_level):
"""
Base parser class
:param queue: queue for send result or error messages to main process.
"""
self.login = False
self.log_level = log_level
self.log = logging.getLogger(self.name)
if not self.log.hasHandlers():
formatter = logging.Formatter('[%(asctime)s][%(levelname)s][%(name)s]: %(message)s',
"%Y-%m-%d %H:%M:%S")
console = logging.StreamHandler()
console.setFormatter(formatter)
self.log.addHandler(console)
self.log.setLevel(log_level)
self.config = configparser.ConfigParser()
if TRAVIS_BUILD:
self.config.read_file(open("giveaway_bot.exp"))
else:
self.config.read_file(open("giveaway_bot.ini"))
self.config = self.config[self.name]
self.queue = queue
# self.cookies_file = '%s.cookies' % self.name
# self.cj = cookiejar.LWPCookieJar(self.cookies_file)
# try:
# self.cj.load()
# except FileNotFoundError:
# for key in self.cookies:
# self.cookies[key] = self.config[key]
#
# expires = datetime.now() + timedelta(days=365)
# expires = expires.timestamp()
#
# cookiejar_from_dict(self.cookies, cookiejar=self.cj, expires=expires, discard=False, rest={})
for key in self.cookies:
self.cookies[key] = self.config[key]
if all(bool(self.cookies[key]) is False for key in self.cookies):
self.cookies = None
self.cj = requests.utils.cookiejar_from_dict(self.cookies)
self.session = requests.Session()
self.session.cookies = self.cj
def _crash(self, msg):
"""
Call if something wrong and add error to message queue
:param msg: message to log
"""
timestamp = datetime.now()
results = {'timestamp': timestamp, 'status': 'error', 'msg': msg}
self.queue.put(results)
self.log.error(msg)
if UNIT_TESTS:
raise
else:
os._exit(1)
def _login_check(self, html):
"""
Check what loged before parse.
:param html: content if parsing page
"""
soup = bs4.BeautifulSoup(html, PARSER)
login = soup.find(self.check_tag, {self.check_type, self.check_text})
if login:
self.login = True
self.log.debug("%s login successful" % self.verbose_name)
else:
self.login = False
raise AuthError
# def __del__(self):
# if self.login:
# self.cj.save()
# else:
# try:
# os.remove(self.cookies_file)
# except FileNotFoundError:
# pass
def singleton(class_):
instances = {}
def getinstance(*args, **kwargs):
if class_ not in instances:
instances[class_] = class_(*args, **kwargs)
return instances[class_]
return getinstance
@singleton
class SteamParser(Parser):
name = "Steam"
verbose_name = "«Steam Community»"
site_url = "http://steamcommunity.com/"
check_tag = "a"
check_type = "class"
check_text = "user_avatar"
cookies = {'steamLogin': None}
@property
@retrying
@caching_property
def wishlist(self):
"""
:return: steam wishlist with apend user wishlist from config
"""
self.log.info('Fetching Steam Wishlist.')
wishlist = []
url = "http://steamcommunity.com/profiles/%s/wishlist/" % self.config["steamLogin"][:17]
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
soup = bs4.BeautifulSoup(html, PARSER)
items = soup.find_all('div', {"class", 'wishlistRow'})
for item in items:
try:
price = item.find('div', {'class': 'price'}).text.strip()
price_old = ''
except AttributeError:
try:
price = item.find('div', {'class': 'discount_final_price'}).text.strip()
price_old = item.find('div', {'class': 'discount_original_price'}).text.strip()
except AttributeError:
price = ''
price_old = ''
data = {'id': int(str.strip(item['id'], 'game_')),
'title': item.find('h4', {'class': 'ellipsis'}).text,
'price': price, 'price_old': price_old}
wishlist.append(data)
self.log.info('In You Steam Wishlist %s games.' % len(wishlist))
local_wishlist = [int(x.strip()) for x in self.config['wishlist'].split(',') if x]
for game_id in local_wishlist:
data = {'id': game_id, 'title': self.get_title(game_id)}
wishlist.append(data)
return wishlist
@property
@retrying
@caching_property
def library(self):
"""
:return: games in steam library
"""
self.log.info('Fetching Steam Library.')
library = []
url = "%s/profiles/%s/games/?tab=all" % (self.site_url, self.config["steamLogin"][:17])
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
for row in html.decode().splitlines():
if 'var rgGames = ' in row:
library = json.loads(str.strip(str.rstrip(str.replace(row, 'var rgGames = ', ''), ';')))
self.log.info('In You Steam Library %s games.' % len(library))
return library
@retrying
def get_os_list(self, game_id):
"""
Return list of game supported OS
:param game_id: steam game id
:return: ['win', 'lin', 'mac']
"""
os_list = []
url = "http://store.steampowered.com/app/%s" % game_id
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
soup = bs4.BeautifulSoup(html, PARSER)
if soup.find('span', {'class': 'platform_img win'}):
os_list.append('win')
if soup.find('span', {'class': 'platform_img linux'}):
os_list.append('lin')
if soup.find('span', {'class': 'platform_img mac'}):
os_list.append('mac')
return os_list
@retrying
def get_type(self, game_id):
# TODO add other types like film
"""
Return app type
:param game_id: steam game id
:return: now can return only 'dlc' and 'game'
"""
url = "http://store.steampowered.com/app/%s" % game_id
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
soup = bs4.BeautifulSoup(html, PARSER)
if soup.find('div', {'class': 'game_area_dlc_bubble'}):
return 'dlc'
else:
return 'game'
@retrying
def get_cards(self, game_id):
"""
Return cards support status
:param game_id:
:return: True or False
"""
cards = False
url = "http://store.steampowered.com/app/%s" % game_id
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
soup = bs4.BeautifulSoup(html, PARSER)
categories = soup.find('div', {'id': 'category_block'})
for img in categories.find_all('img', {'class': 'category_icon'}):
if 'ico_cards.png' in img['src']:
cards = True
break
return cards
@retrying
def get_title(self, game_id):
url = "http://store.steampowered.com/app/%s" % game_id
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
soup = bs4.BeautifulSoup(html, PARSER)
return soup.find('div', {'class': 'apphub_AppName'}).text
class Harvester(Parser):
required_filters = []
disabled_filters = []
internal_filters = []
def __init__(self, queue, log_level):
super(Harvester, self).__init__(queue, log_level)
self.filters = self.required_filters
# I know it's shit ^_^
try:
# list(map(lambda s: self.filters.append(s) if s not in self.filters else None,
# map(lambda s: s if len(s) > 1 else s[0],
# map(lambda s: list(map(lambda s: s.strip(), s)),
# map(lambda s: s.split('='),
# map(lambda s: s if s else None, self.config['filters'].split(',')))))))
[self.filters.append(x) for x in
[x if len(x) > 1 else x[0] for x in
[[x.strip() for x in x.split('=')] for x in
[x for x in self.config['filters'].split(',') if x]]] if x not in self.filters]
except (KeyError, AttributeError):
pass
self.filters = [f for f in self.filters if f not in self.disabled_filters]
if 'wishlist' in self.filters:
try:
self.filters.remove('library')
except ValueError:
pass
def start(self):
self.log.info("Starting %s harvester..." % self.verbose_name)
sow = self._sow()
reap = self._reap()
if reap:
self.log.info('You have not accepted prizes, check it at %s !' % self.site_url)
else:
self.log.info("You don't win anything. For now...")
timestamp = datetime.now()
results = {'timestamp': timestamp, 'status': 'ok', 'sow': sow, 'reap': reap}
self.log.info("Harvesting %s is over!" % self.verbose_name)
self.queue.put(results)
@property
@abc.abstractproperty
def level(self):
pass
@property
@abc.abstractproperty
def points(self):
pass
def _sow(self):
giveaways_enter = []
sow = True
points = self.points
page = 1
while sow:
giveaways = self._get_giveaways(page)
if not giveaways:
self.log.info('No more giveaways.')
break
for flt in self.filters:
if flt not in self.internal_filters:
try:
if isinstance(flt, str):
giveaways = getattr(self, "_filter_%s" % flt)(giveaways)
elif isinstance(flt, list):
giveaways = getattr(self, "_arged_filter_%s" % flt[0])(giveaways, flt[1])
except AttributeError:
continue
page += 1
for giveaway in giveaways:
if int(points) >= int(giveaway.points):
status = self._enter_giveaway(giveaway)
if status == 'ok':
points -= giveaway.points
giveaways_enter.append({'title': giveaway.title, 'href': giveaway.href})
self.log.info('Take part in «%s» giveaway.' % giveaway.title)
else:
self.log.info("Not Enough Points.")
sow = False
break
return giveaways_enter
@abc.abstractmethod
def _reap(self):
pass
@abc.abstractmethod
def _get_giveaways(self, page):
pass
def _enter_giveaway(self, giveaway):
status = giveaway.enter()
return status
def _filter_trust(self, giveaways):
"""
Exclude giveaways base on author's feedback
:param giveaway:
:return: equal trust=1
"""
filtred_giveaways = []
for g in giveaways:
try:
if int(g.trust_points) > 0:
filtred_giveaways.append(g)
except:
pass
return filtred_giveaways
def _arged_filter_trust(self, giveaways, trust):
"""
Exclude giveaways base on author's feedback
:param giveaways: list of giveaways
:param trust: 1|0|-1
:return: 1 - list of giveaways with positive feedback, 0 - positive and zero(balansed), -1 - not filtered
"""
filtered_giveaways = []
if int(trust) <= -1:
return giveaways
else:
for g in giveaways:
try:
if int(g.trust_points) >= int(trust):
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _arged_filter_max_points(self, giveaways, points):
"""
Exclude giveaways coast more then points
:param giveaways: list of giveaways
:return: fitered giveaways
"""
filtered_giveaways = []
for g in giveaways:
try:
if int(g.points) <= int(points):
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _arged_filter_min_points(self, giveaways, points):
"""
Exclude giveaways coast less then points
:param giveaways: list of giveaways
:return: fitered giveaways
"""
filtered_giveaways = []
for g in giveaways:
try:
if int(g.points) >= int(points):
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _filter_level(self, giveaways):
"""
Exclude giveaways that to height level
"""
filtered_giveaways = []
for g in giveaways:
try:
if int(g.level) <= self.level:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _arged_filter_min_level(self, giveaways, level):
"""
Exclude giveaways with lesser level
:param giveaways: list of giveaways
:return: fitered giveaways
"""
filtered_giveaways = []
for g in giveaways:
try:
if int(g.level) >= int(level):
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _arged_filter_os(self, giveaways, os):
"""
Exclude giveaways what not support os
:param giveaways: list of giveaways
:param os: target os
:return: fitered giveaways
"""
if os == 'all':
return giveaways
else:
filtered_giveaways = []
for g in giveaways:
try:
if os in g.os_list:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _filter_entered(self, giveaways):
"""
Exclude giveaways that already entered
"""
filtered_giveaways = []
for g in giveaways:
try:
if not g.entered:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _filter_library(self, giveaways):
"""
Exclude game's giveaways that already in yor library
"""
filtered_giveaways = []
for g in giveaways:
try:
if not g.in_library:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _filter_wishlist(self, giveaways):
"""
Exclude giveaways of games that what not in you wishlist
"""
filtered_giveaways = []
for g in giveaways:
try:
if g.in_wishlist:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _filter_dlc(self, giveaways):
"""
Exclude dlc's giveaways
"""
filtered_giveaways = []
for g in giveaways:
try:
if not g.dlc:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
def _filter_cards(self, giveaways):
"""
Exclude giveaways games without cards
"""
filtered_giveaways = []
for g in giveaways:
try:
if g.cards:
filtered_giveaways.append(g)
except:
pass
return filtered_giveaways
class Giveaway(Parser):
def __init__(self, queue, log_level, game_id):
super(Giveaway, self).__init__(queue, log_level)
self.game_id = game_id
self.steam = None
self.wishlist = None
self.library = None
@property
@caching_property
def in_wishlist(self):
in_wishlist = False
if self.steam is None:
self.steam = SteamParser(self.queue, self.log_level)
if self.wishlist is None:
self.wishlist = self.steam.wishlist
for game in self.wishlist:
if self.game_id == game["id"]:
in_wishlist = True
break
return in_wishlist
@property
@caching_property
def in_library(self):
in_library = False
if self.steam is None:
self.steam = SteamParser(self.queue, self.log_level)
if self.library is None:
self.library = self.steam.library
for game in self.library:
if self.game_id == game["appid"]:
in_library = True
break
return in_library
@property
@caching_property
def os_list(self):
if not self.steam:
self.steam = SteamParser(self.queue, self.log_level)
return self.steam.get_os_list(self.game_id)
@property
@caching_property
def dlc(self):
if not self.steam:
self.steam = SteamParser(self.queue, self.log_level)
app_type = self.steam.get_type(self.game_id)
if app_type == 'dlc':
return True
else:
return False
@property
@caching_property
def cards(self):
if not self.steam:
self.steam = SteamParser(self.queue, self.log_level)
return self.steam.get_cards(self.game_id)
@abc.abstractmethod
def enter(self):
pass
class SteamGiftsHarvester(Harvester):
name = "SteamGifts"
verbose_name = "«Steam Gifts»"
site_url = "https://www.steamgifts.com"
check_tag = "div"
check_type = "class"
check_text = "nav__avatar-inner-wrap"
cookies = {'PHPSESSID': None}
required_filters = ['entered', 'level', 'library']
internal_filters = ['library', 'level', 'os', 'wishlist']
@property
@retrying
@caching_property
def level(self):
html = self.session.get(self.site_url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
soup = bs4.BeautifulSoup(html, PARSER)
try:
return int(re.findall('\d+', soup.find('span', {'class', 'nav__points'}).nextSibling.nextSibling.text)[0])
except AttributeError:
return 0
@property
@retrying
@caching_property
def points(self):
html = self.session.get(self.site_url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
soup = bs4.BeautifulSoup(html, PARSER)
try:
return int(soup.find('span', {'class', 'nav__points'}).text)
except AttributeError:
raise ParseError
@property
@retrying
@caching_property
def xsrf_token(self):
html = self.session.get(self.site_url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
soup = bs4.BeautifulSoup(html, PARSER)
try:
return soup.find('input', {'name': 'xsrf_token'})['value']
except AttributeError:
raise ParseError
@retrying
def _get_giveaways(self, page):
giveaways = []
self._internal_filters()
url = '%s/giveaways/search' % self.site_url
params = {'page': page}
if 'wishlist' in self.filters:
params.update({'type': 'wishlist'})
html = self.session.get(url, cookies=self.cookies, params=params, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
soup = bs4.BeautifulSoup(html, PARSER)
items = soup.find('div', {'class': 'page__heading'}).next_sibling.next_sibling.find_all('div', {'class': 'giveaway__row-outer-wrap'})
if not items:
raise NoItemsError
for item in items:
header = item.find('a', {'class': 'giveaway__heading__name'})
href = "%s%s" % (self.site_url, header['href'])
title = header.text.strip()
code = str.split(header['href'], '/')[2]
if item.find('div', {'class': 'is-faded'}):
entered = True
else:
entered = False
try:
level = int(re.findall('\d+', item.find('div', {'title': 'Contributor Level'}).text)[0])
except AttributeError:
level = 0
points = int(re.findall('\d+', item.find('span', {'class': 'giveaway__heading__thin'}).text)[0])
try:
game_id = int(str.split(item.find('h2', {'class': 'giveaway__heading'}).find('a', {'class': 'giveaway__icon', 'target': '_blank'})['href'], '/')[4])
except TypeError:
game_id = None
profile_url = "%s%s" % (self.site_url, item.find('a', {'class': 'giveaway__username'})['href'])
giveaway = SteamGiftsGiveaway(self.queue, self.log_level, game_id, self.xsrf_token, code, title, href, entered, level, points, profile_url)
giveaways.append(giveaway)
return giveaways
@retrying
def _reap(self):
giveaways_win = []
url = '%s/giveaways/won' % self.site_url
html = self.session.get(url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
self._login_check(html)
soup = bs4.BeautifulSoup(html, PARSER)
items = soup.find('div', {'class': 'table__rows'}).find_all('div', {'class': 'table__row-outer-wrap'})
if not items:
raise NoItemsError
for item in items:
not_received = item.find('div', {'class': 'table__gift-feedback-received is-hidden'})
if not_received:
item_header = item.find('a', {'class': 'table__column__heading'})
item_title = item_header.text.strip()
item_href = "%s%s" % (self.site_url, item_header['href'])
giveaways_win.append({'title': item_title, 'href': item_href}, )
else:
continue
return giveaways_win
def _internal_filters(self):
if ['os', 'win'] in self.filters:
filter_os = 1
elif ['os', 'lin'] in self.filters:
filter_os = 2
elif ['os', 'mac'] in self.filters:
filter_os = 3
else:
filter_os = 0
filter_giveaways_exist_in_account = 1
filter_giveaways_level = 1
filter_giveaways_missing_base_game = 1
data = {'xsrf_token': self.xsrf_token, 'filter_os': filter_os,
'filter_giveaways_exist_in_account': filter_giveaways_exist_in_account,
'filter_giveaways_level': filter_giveaways_level,
'filter_giveaways_missing_base_game': filter_giveaways_missing_base_game}
url = "%s/account/settings/giveaways" % self.site_url
self.session.post(url, cookies=self.cookies, data=data,
headers={'User-Agent': USER_AGENT}).status_code
class SteamGiftsGiveaway(Giveaway):
name = "SteamGifts"
verbose_name = "«Steam Gifts»"
site_url = "https://www.steamgifts.com"
check_tag = "div"
check_type = "class"
check_text = "nav__avatar-inner-wrap"
cookies = {'PHPSESSID': None}
def __init__(self, queue, log_level, game_id, xsrf_token, code, title, href, entered, level, points, profile_url):
super(SteamGiftsGiveaway, self).__init__(queue, log_level, game_id)
self.xsrf_token = xsrf_token
self.code = code
self.title = title
self.href = href
self.entered = entered
self.level = level
self.points = points
self.profile_url = profile_url
@property
@caching_property
def trust_points(self):
html = self.session.get(self.profile_url, cookies=self.cookies, headers={'User-Agent': USER_AGENT}).content
soup = bs4.BeautifulSoup(html, PARSER)
gift_sent_row = soup.find('span', {'title': re.compile('\d+ Awaiting Feedback, \d+ Not Received')})
gift_wait, gift_fail = list(map(lambda i: int(i), re.findall('\d+', gift_sent_row['title'])))
gift_sent = int(gift_sent_row.a.text.replace(',', ''))
trust_points = gift_sent - gift_wait - gift_fail
return trust_points
def enter(self):
data = {'xsrf_token': self.xsrf_token, 'do': 'entry_insert', 'code': self.code}
url = "%s/ajax.php" % self.site_url
code = self.session.post(url, cookies=self.cookies, data=data, headers={'User-Agent': USER_AGENT}).status_code
if code == 200:
return 'ok'
else:
return 'error'
# TODO incapsula bypass
class IndieGalaHarvester(Harvester):
name = "IndieGala"
verbose_name = "«Indie Gala»"
site_url = "https://www.indiegala.com/giveaways"