-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
1224 lines (1141 loc) · 59.6 KB
/
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
# -*- coding: utf-8 -*-
###
# AUTHORS: CHRISTIAN GIBSON,
# PROJECT: /r/MechMarket Bot
# UPDATED: SEPTEMBER 11, 2015
# USAGE: python bot.py [-h / --help] [-is / --interactive-shell]
# EXPECTS: python 3.4.0
# beautifulsoup4 4.4.0
# praw 3.2.1
# regex 2015.06.24
###
import argparse
import bs4
import cmd
import collections
import configparser
import copy
import errno
import inspect
import logging
import math
import multiprocessing
import os
import platform
import praw
import random
import regex
import shelve
import shutil
import threading
import time
import traceback
import urllib
import uuid
__AUTHORS__ = ['/u/NotMelNoGuitars']
__VERSION__ = 0.1
__CMD_STR__ = '>>> '
__INFO__ = 'MechKB0t-v%s on "%s" with << %s v%s >> at %s %s' % (
__VERSION__,
platform.platform(),
platform.python_implementation(),
platform.python_version(),
time.ctime(),
time.localtime().tm_zone)
def coerce_reddit_handles(handles=__AUTHORS__):
clean = regex.compile(r'[^A-Z0-9_/-]', regex.UNICODE + regex.IGNORECASE)
authors = []
for author in handles:
author = clean.sub('', str(author))
if ((author.startswith('/u/') or author.startswith('/r/'))
and len(author.split('/')) == 3):
authors.append(author)
else:
authors.append('/u/' + max(author.split('/'), key=len))
return authors
class config_generator():
# c = bot.config_generator()(bot.bot.CONFIG_DEFAULTS)() ; print(c.func_code)
_FUNC_CODE_ = """class config_handler(configparser.RawConfigParser):
def __init__(self, conf_file=None):
super(self.__class__, self).__init__()
self.true_values = frozenset(['true', 't', '1', 'y', 'yes', 'aye', 'on',
'use', 'active', 'activate'])
self.heatware_regex = None
if conf_file:
self.conf_file = os.path.abspath(conf_file)
else:
try:
self.conf_file = (os.path.dirname(os.path.abspath(
inspect.getsourcefile(lambda: None))) + os.sep + 'config.cfg')
except:
self.conf_file = None
if self.conf_file:
try:
self.read(self.conf_file)
if not self.sections():
self.generate_defaults()
self.status = errno.ENOENT
else:
self.status = 0
except:
traceback.print_exc()
self.status = errno.EIO
else:
self.status = errno.EBADF
def store(self):
with open(self.conf_file, 'w') as conf_handle:
self.write(conf_handle)
def protected_pull(self, section, option, cast=None, default=None):
if self.status:
raise EnvironmentError(self.status,
('Current status #%d <%s> "%s".' %
(self.status,
errno.errorcode[self.status],
os.strerror(self.status))),
self.conf_file)
try:
if cast:
return cast(self.get(section, option))
else:
return self.get(section, option)
except:
if default:
return default
else:
raise
def protected_pullboolean(self, section, option):
boolean = self.protected_pull(section, option).lower()
if boolean in self.true_values:
return True
return False
def protected_push(self, section, option, value):
if self.status:
raise EnvironmentError(self.status,
('Current status #%d <%s> "%s".' %
(self.status,
errno.errorcode[self.status],
os.strerror(self.status))),
self.conf_file)
try:
self.set(section, option, value)
self.store()
return True
except:
return False
def protected_pushboolean(self, section, option, value):
if value is True or value in self.true_values:
return self.protected_push(section, option, 'true')
return self.protected_push(section, option, 'false')
"""
def __init__(self):
pass
def __call__(self, sections, ignore_description=False):
if all(all('desc' in detail for _, detail in options.items())
for _, options in sections.items()) or ignore_description:
pass
else:
raise TypeError('Provided configuration does not provide a "desc" '
'field for each section option. As such, the %s '
'cannot create an interactive_initialization() '
'method. To create the constructor without the '
'interactive_initialization() method, set '
'"ignore_description" to True when calling %s.'
% (self.__class__, self.__class__))
added_methods = {attr_or_func: None
for attr_or_func in dir(configparser.RawConfigParser)}
added_methods['conf_file'] = None
added_methods['func_code'] = None
added_methods['heatware_regex'] = None
added_methods['protected_pull'] = None
added_methods['protected_pullboolean'] = None
added_methods['protected_push'] = None
added_methods['protected_pushboolean'] = None
added_methods['status'] = None
added_methods['store'] = None
added_methods['true_values'] = None
if ignore_description:
added_methods['generate_defaults'] = None
else:
added_methods['generate_defaults'] = None
added_methods['interactive_initialization'] = None
init_initials = [" def interactive_initialization(self):",
" to_initialize = ["]
init_defaults = [" def generate_defaults(self):"]
for section, options in sections.items():
init_defaults.append(" self.add_section('%s')" % section)
for option, detail in options.items():
if 'boolean' in detail:
pulltype = 'protected_pullboolean'
pushtype = 'protected_pushboolean'
else:
pulltype = 'protected_pull'
pushtype = 'protected_push'
if 'get' in detail:
if detail['get']:
get_method = detail['get']
else:
get_method = None
else:
get_method = 'get_%s_%s' % (section, option)
if get_method in added_methods:
raise SyntaxError('Attempted to add get method %s to new '
'config_handler object, but it was '
'already defined.' % get_method)
if get_method:
added_methods[get_method] = (
" def %s(self):\n"
" return self.%s('%s', '%s')\n"
% (get_method, pulltype, section, option))
if 'set' in detail:
if detail['set']:
set_method = detail['set']
else:
set_method = None
else:
set_method = 'set_%s_%s' % (section, option)
if set_method in added_methods:
raise SyntaxError('Attempted to add set method %s to new '
'config_handler object, but it was '
'already defined.' % set_method)
if set_method:
added_methods[set_method] = (
" def %s(self, value):\n"
" return self.%s('%s', '%s', value)\n"
% (set_method, pushtype, section, option))
if 'def' in detail:
init_defaults.append(
" self.set('%s', '%s', '%s')" %
(section, option, detail['def']))
else:
init_defaults.append(
" self.set('%s', '%s', '%s')" %
(section, option, ""))
if not ignore_description:
if 'def' in detail:
init_initials.append(
" ('%s', '%s', '%s', '%s', '%s')," %
(self.sanify(detail['desc']),
self.sanify(detail['def']),
pushtype, section, option))
else:
init_initials.append(
" ('%s', None, '%s', '%s', '%s')," %
(self.sanify(detail['desc']),
pushtype, section, option))
added_methods['generate_defaults'] = ('\n'.join(init_defaults) + '\n' +
' self.store()\n')
if not ignore_description:
init_initials.extend([
" ]",
"",
" for desc, def_, fxn, sec, opt in to_initialize:",
" value_set = False",
" while not value_set:",
" try:",
" print('Now setting [%s].[%s]:' % (sec, opt))",
" print('Description: %s' % desc)",
" if def_:",
" print('Leave blank to use default '",
" 'value \"%s\".' % def_)",
" val = input('Set [%s].[%s]: ' % (sec, opt))",
" if val:",
" getattr(self, fxn)(sec, opt, val)",
" value_set = True",
" elif def_:",
" getattr(self, fxn)(sec, opt, def_)",
" value_set = True",
" else:",
" print('(!!!) Invalid value provided, '",
" 'or no value provided with no '",
" 'default available.\\n')",
" if value_set:",
" rec = self.get(sec, opt)",
" print('Value set as \"%s\".' % rec,",
" end=' ')",
" chk = input('Is that correct? (y/n) ')",
" if chk.lower().strip().startswith('y'):",
" print('Input accepted and stored.'",
" '\\f\\n\\r')",
" else:",
" print('Interpreted response as '",
" '\"no\". Will recapture '",
" 'input.\\n')",
" value_set = False",
" except KeyboardInterrupt:",
" raise",
" except:",
" print('(!!!) Error encountered when '",
" 'attempting to set value.\\n')",
" self.store()"
])
added_methods['interactive_initialization'] = (
'\n'.join(init_initials) + '\n')
_func_code_ = (self._FUNC_CODE_ +
'\n'.join(filter(lambda x: isinstance(x, str),
added_methods.values())))
exec(compile(_func_code_, '<string>', 'exec'))
config = eval('config_handler')
config.func_code = _func_code_
return config
def sanify(self, text):
return text.encode('unicode-escape').decode().replace("'", "\\'")
_BS4_PARSER = 'html.parser'
_GET_CONFIG = config_generator()
logger = logging.getLogger()
logger.setLevel(logging.INFO)
class bot_prompt(cmd.Cmd):
# errno.ENOTTY
def __init__(self):
super(self.__class__, self).__init__()
self.prompt = __CMD_STR__
self.size = shutil.get_terminal_size()
self.height, self.width = self.size.lines, self.size.columns
class bot(praw.Reddit, threading.Thread):
CONFIG_DEFAULTS = collections.OrderedDict([
('crawl', collections.OrderedDict([
('file', {'def': 'data.record',
'desc': ('This is the name of the flatfile that will be '
'used to store all collected data on a user-by-'
'user basis.')}),
('hold', {'def': '10',
'desc': ('This is the number of seconds the bot will '
'spend in each state as a minimum.\nAs an '
'example, the bot has three states by default:\n'
' 1. Crawl /new of the target subreddit.\n'
' 2. Respond to user PMs.\n'
' 3. Crawl the trade thread of the target '
'subreddit.')}),
('sleep', {'def': '100',
'desc': ('This is the number of seconds the bot will '
'spend doing nothing after completing each set '
'of states.')})
])),
('reddit', collections.OrderedDict([
('user_agent', {'def': ('%s-%s:%s:MechKB0t-v%s (by %s)' %
(platform.system(), platform.processor(),
uuid.uuid5(uuid.NAMESPACE_OID, __INFO__),
__VERSION__,
', '.join(coerce_reddit_handles()))),
'desc': ('This is the plaintext string that will '
'be used by the admins at reddit to '
'identify this bot. It is recommended '
'that bots follow the format:\n'
' <platform>:<app ID>:<version string> '
'(by /u/<reddit username>)\n'
'Full rules and restrictions can be '
'found here: http://github.com/reddit/'
'reddit/wiki/API.')}),
('client_id', {'desc': ('This is the OAuth2 client_id created '
'for your reddit app instance. More '
'information can be found here: http://'
'github.com/reddit/reddit/wiki/OAuth2.')}),
('client_secret', {'desc': ('This is the OAuth2 client_secret '
'created for your reddit app instance. '
'More information can be found here: '
'http://github.com/reddit/reddit/wiki'
'/OAuth2.')}),
('redirect_url', {'desc': ('This is the OAuth2 redirect_url created '
'for your reddit app instance. More '
'information can be found here: http://'
'github.com/reddit/reddit/wiki/OAuth2.')}),
('subreddit', {'desc': 'The subreddit targeted by this bot.'}),
('multiprocess', {'def': 'false',
'get': 'is_multiprocessed',
'set': None,
'desc': 'Currently not implemented. Ignore.',
'boolean': True}),
('verbose', {'def': 'true',
'get': 'is_verbose',
'set': 'set_verbose',
'desc': ('Sets whether the bot will display its '
'actions during runtime, or simply log them.'),
'boolean': True})
])),
('monitor', collections.OrderedDict([
('log', {'def': 'event.log',
'desc': ('This is the flatfile that will be used to log '
'all actions taken by the bot.')}),
('posts', {'def': 'true',
'desc': ('Whether or not the bot will log basic '
'information concerning all posts observed '
'during its runtime.'),
'boolean': True}),
('users', {'def': 'true',
'desc': ('Whether or not the bot will record basic '
'infromation concerning all users observed '
'during its runtime.'),
'boolean': True}),
('format', {'def': '%(created)f -- %(levelname)s -> %(message)s',
'desc': ('This is the format string that will be used '
'in creating each entry in the log file. '
'Formatting options include:\n'
' %(asctime)s: Human-readable time when a '
'logged event was created.\n'
' %(created)f: Seconds since epoch when a '
'logged event was created.\n'
' %(filename)s: Source file that created a '
'logged event.\n'
' %(funcName)s: Function used that created a '
'logged event.\n'
' %(levelname)s: Severity of logged event as '
'an English string.\n'
' %(levelno)s: Severity of logged event as a '
'numeric value.\n'
' %(lineno)d: Line number of the source file '
'where a logged event was created.\n'
' %(module)s: Module that created a logged '
'event.\n'
' %(msecs)d: Millisecond portion of system '
'time when an event was logged.\n'
' %(message)s: Message provided when an event '
'was logged.\n'
' %(name)s: Name of the logger used to create '
'the logged event.\n'
' %(pathname)s: Full pathname of the source '
'file that created the logged event.\n'
' %(process)d: Process ID that created the '
'logged event.\n'
' %(processName)s: Process name that created '
'the logged event.\n'
' %(relativeCreated)d: Milliseconds after the '
'logging module was initially loaded that an '
'event was logged.\n'
' %(thread)d: Thread ID that created the '
'logged event.\n'
' %(threadName)s: Thread name that created '
'the logged event.\n'
'Further information can be found at: '
'http://docs.python.org/3.4/library/logging.'
'html#logging.LogRecord')},
('respond', {'def': 'true',
'desc': ('Whether or not the bot should make a post '
'on each new trade thread.'),
'boolean': True}),
('response', {'desc': ('The text template used when commenting on '
'a new trade thread. Formatting options '
'include:\n')})),
])),
('sidebar', collections.OrderedDict([
('add_button', {'def': 'false',
'get': 'should_add_button',
'desc': ('Whether the bot should add a button for '
'the current trade thread on the target '
'subreddit\'s sidebar.'),
'boolean': True}),
('button_text', {'desc': 'The text used for the created button.'}),
('button_start', {'desc': ('A specialized tag, included in the '
'sidebar\'s text, which determines '
'where the button starts.')}),
('button_end', {'desc': ('A specialized tag, included in the '
'sidebar\'s text, which determines where '
'the button ends.')})
])),
('class', collections.OrderedDict([
('use', {'def': 'true',
'desc': 'If the bot should monitor and update user flair.',
'boolean': True}),
('start', {'desc': 'Flair given to users never seen before.'}),
('limit', {'desc': ('Maximum integer indicating how many times '
'a user\'s flair can be incremented.')}),
('ignore', {'desc': ('A whitespace-separated list of flairs which '
'should be ignored if encountered by the bot.')}),
('pattern', {'desc': ('The pattern used to generate new user '
'flair following an increment. %i is used '
'to indicate where the integer value of the '
'flair should go. As a example, a flair '
'pattern of "u-%i" would take on the values '
'"u-1" for a user with a flair value of 1, '
'"u-2" for a user with a flair value of 2, '
'"u-3" for a user with a flair value of 3, '
'etc.')}),
('increment', {'def': '1',
'desc': ('The integer value that a user\'s flair '
'value will be incremented by with each '
'flair increment. Given a default value '
'of "1", a user with a flair value of 3 '
'would advance to a flair value of 4 after '
'completing a trade.')})
])),
('trade', collections.OrderedDict([
('method', {'def': 'post',
'desc': ('The method used by the bot to confirm user '
'trades. Three options are available, "pm", '
'"post", or "both". If "pm" is specified, '
'trades will be confirmed via private '
'message; with the sender in a trade sending '
'a private message to the bot containing the '
'reddit handle of the recipient. The bot then '
'contacts the other party, who confirms the '
'trade. If "post" is specified, a public '
'thread is used. Within the thread, the '
'sender creates a top-level comment, which '
'the recipient replies to with a comment '
'containing the phrase "confirmed". In the '
'case that "both" is specified, either option '
'can be used to confirm a trade.')}),
('post_id', {'desc': ('The id used by the trading thread within '
'the target subreddit. If left blank, the '
'bot will create its own trading thread. In '
'the case that "pm" is used as a method, '
'this value is ignored.')}),
('post_text', {'desc': ('The text template used when creating a '
'new trade thread. Supports formatting '
'arguments as found in Python\'s strftime '
'command. For more information, see: '
'https://docs.python.org/2/library/time.html'
'#time.strftime.')}),
('post_rate', {'def': 'monthly',
'desc': ('The rate at which the bot will create '
'new trading posts on the target subreddit.'
' Provided options include "daily", '
'"weekly", "monthly", "yearly", and "never"'
'. If "never" is selected, the post_id will'
' have to be updated manually by the user.')}),
('post_title', {'desc': ('The title template used when creating a '
'new trade thread\'s title. Supports '
'formatting arguments as found in Python\'s'
'strftime command. For more information, '
'see: https://docs.python.org/2/library/'
'time.html#time.strftime.')}),
('post_sticky', {'def': 'false',
'desc': ('If the bot makes the trade thread sticky'
' or not.')}),
('post_response', {'desc': ('The text template used when replying '
'to a confirmed trade comment on a '
'trade post. Supports formatting '
'arguments as found in Python\'s '
'strftime command. For more information'
', see: https://docs.python.org/2/'
'library/time.html#time.strftime.')}),
('message_text', {'desc': ('The text template used when sending a '
'private message to both users following'
' a confirmed trade. Supports formatting'
' arguments as found in Python\'s '
'strftime command. For more information,'
' see: https://docs.python.org/2/library'
'/time.html#time.strftime.')}),
('message_title', {'desc': ('The title template used when sending a '
'private message to both users '
'following a confirmed trade. Supports '
'formatting arguments as found in '
'Python\'s strftime command. For more '
'information, see: https://docs.python.'
'org/2/library/time.html#time.strftime.')}),
('respond', {'def': 'true',
'desc': ('If the bot should respond following a '
'confirmed trade or not.'),
'boolean': True}),
('age_msg', {'desc': ('Message used to reply when a user attempts '
'to confirm a trade when their account is '
'younger than the provided age limit.')}),
('age_type', {'def': 'days',
'desc': ('Units used in determining if a user\'s '
'account is too young to confirm a trade. '
'Options are "seconds", "minutes", "hours", '
'"days", "months".')}),
('age_limit', {'def': '30',
'desc': ('Numerical measurement used in determining '
'if a user\'s account is too young to '
'confirm a trade.')}),
('same_msg', {'desc': ('Message used to reply when a user attempts '
'to confirm a trade with themselves.')}),
('karma_msg', {'desc': ('Message used to reply when a user attempts'
' to confirm a trade when their account\'s '
'karma is below the provided karma limit.')}),
('karma_type', {'def': 'both',
'desc': ('Units used in determining if a user\'s '
'account has sufficient karma to confirm '
'a trade. Options are "comment", "link", '
'or "both".')}),
('karma_limit', {'def': '100',
'desc': ('Numerical measurement used in '
'determining if a user\'s account has '
'sufficient karma to confirm a trade.')})
])),
('heatware', collections.OrderedDict([
('method', {'def': 'pm',
'desc': ('The method by which the bot will collect a '
'user\'s heatware URL. Three options are '
'available, "pm", "post", and "both". If "pm" '
'is specified, users can submit heatware URLs '
'by means of private message to the bot. If '
'"post" is specified, users can submit their '
'heatware URLs by means of commenting in a '
'specified post. If "both" is specified, '
'either method can be used.')}),
('post_id', {'desc': ('The id used by the heatware thread in the '
'target subreddit.')}),
('post_text', {'desc': ('The text template used when creating a '
'new heatware thread. Supports formatting '
'arguments as found in Python\'s strftime '
'command. For more information, see: '
'https://docs.python.org/2/library/time.html'
'#time.strftime.')}),
('post_rate', {'def': 'yearly',
'desc': ('The rate at which the bot will create '
'new heatware posts on the target subreddit.'
' Provided options include "daily", '
'"weekly", "monthly", "yearly", and "never"'
'. If "never" is selected, the post_id will'
' have to be updated manually by the user.')}),
('post_title', {'desc': ('The title template used when creating a '
'new heatware thread\'s title. Supports '
'formatting arguments as found in Python\'s'
'strftime command. For more information, '
'see: https://docs.python.org/2/library/'
'time.html#time.strftime.')}),
('post_sticky', {'desc': ('If the bot makes the heatware thread '
'sticky or not.')}),
('post_response', {'desc': ('The text template used when replying '
'to an accepted heatware comment on a '
'heatware post. Supports formatting '
'arguments as found in Python\'s '
'strftime command. For more information'
', see: https://docs.python.org/2/'
'library/time.html#time.strftime.')}),
('message_text', {'desc': ('The text template used when sending a '
'private message to a user following'
' an accepted heatware profile. Supports '
'formatting arguments as found in Python\'s'
' strftime command. For more information,'
' see: https://docs.python.org/2/library'
'/time.html#time.strftime.')}),
('message_title', {'desc': ('The title template used when sending a '
'private message to a user following '
'an accepted heatware profile. Supports '
'formatting arguments as found in '
'Python\'s strftime command. For more '
'information, see: https://docs.python.'
'org/2/library/time.html#time.strftime.')}),
('regex', {'def': '(?:.*)(http(?:s?)://www\.heatware\.com/eval\.php\?id=[0-9]+)(?:.*)',
'set': None,
'desc': ('The regular expression used to _extract '
'heatware URLs from plaintext comments.')}),
('group', {'def': '1',
'set': None,
'desc': ('The group within the regular expression that '
'actually contained the captured heatware URL. '
'If left blank, the parser will accept the '
'entire match resulting from the regular '
'expression.')}),
('respond', {'def': 'true',
'desc': ('If a bot should respond to an accepted '
'heatware profile URL or not.'),
'boolean': True})
]))
])
def __init__(self, conf_file='config.cfg'):
config_constructor = _GET_CONFIG(self.CONFIG_DEFAULTS)
self.config_handler = config_constructor(conf_file)
if self.config_handler.status:
raise EnvironmentError(self.config_handler.status,
('Current status #%d <%s> "%s".' %
(self.config_handler.status,
errno.errorcode[
self.config_handler.status],
os.strerror(self.config_handler.status))),
conf_file)
log = logging.StreamHandler(self.config_handler.get_monitor_log())
fmt = logging.Formatter(self.config_handler.get_monitor_format())
log.setLevel(logging.DEBUG)
log.setFormatter(fmt)
logger.addHandler(log)
self.data_store = database_handler(
self.config_handler.get_crawl_file())
self.heat_parse = heatware_crawler()
self.run_states = {
state[6:].lstrip('_'): getattr(self, state)
for state in set(dir(self)).difference(dir(super()))
if (state.startswith('_state')
and hasattr(getattr(self, state), '__call__'))}
super().__init__(self.config_handler.get_reddit_user_agent())
self.set_oauth_app_info(self.config_handler.get_reddit_client_id(),
self.config_handler.get_reddit_client_secret(),
self.config_handler.get_reddit_redirect_url())
threading.Thread.__init__(self, daemon=True)
def run(self):
while True:
state_time = {state: max(1, self.config_handler.get_crawl_hold())
for state in self.run_states}
while any(t > 0 for t in state_time.values()):
for state, function in self.run_states.items():
if state_time[state] > 0:
self.state = state
state_start = time.time()
try:
function()
except:
pass
state_elaps = time.time() - state_start
if state_elaps > 0:
state_time[state] -= state_elaps
else:
state_time[state] = 0
time.sleep(self.config_handler.get_crawl_sleep())
self.shutdown()
def _state_trade(self):
"""
Performs processing necessary for the verification and updating
of user's css class following a successful trade.
Will need to call the following methods from self.config_handler:
get_trade_method()
if get_trade_method() in ['post', 'both']:
get_trade_post_id()
get_trade_post_text()
get_trade_post_rate()
get_trade_post_title()
get_trade_post_sticky()
get_trade_post_response()
should_add_button()
get_sidebar_button_text()
get_sidebar_button_start()
get_sidebar_button_end()
if get_trade_method() in ['pm', 'both']:
get_trade_message_text()
get_trade_message_title()
get_trade_respond()
get_trade_age_msg()
get_trade_age_type() -> ['seconds', 'minutes', 'hours', 'days', 'months']
get_trade_same_msg()
get_trade_karma_msg()
get_trade_karma_type() -> ['comment', 'link', 'both']
get_trade_karma_limit()
get_class_use()
get_class_start()
get_class_limit()
get_class_ignore()
get_class_pattern()
get_class_increment()
In addition, will need to log results to logger, and store updated
user information in self.data_store if get_monitor_users() is True.
"""
if self.config_handler.get_trade_method() in ['pm', 'both']:
pass
if self.config_handler.get_trade_method() in ['post', 'both']:
pass
def _state_posts(self):
"""
Monitors and replies to previously unseen posts on the target
subreddit's /new page.
Will need to call the following methods from self.config_handler:
get_monitor_posts()
get_monitor_users()
get_monitor_format()
get_monitor_respond()
get_monitor_response()
"""
pass
def _state_flair(self):
"""
Responsible for verifying and setting user flair with regards to their
accounts on http://www.HeatWare.com.
Will need to call the following methods from self.config_handler:
get_heatware_method()
if get_heatware_method() in ['post', 'both']:
get_heatware_post_id()
get_heatware_post_text()
get_heatware_post_rate()
get_heatware_post_title()
get_heatware_post_sticky()
get_heatware_post_response()
if get_heatware_method() in ['pm', 'both']:
get_heatware_message_text()
get_heatware_message_title()
get_heatware_regex()
get_heatware_group()
get_heatware_respond()
Recall:
>>> import time, pprint
>>> self.heat_parse.parse('2')
>>> while len(self.heat_parse) < 1: time.sleep(1)
>>> results = {id_: info for id_, info in self.heat_parse}
>>> pprint.pprint(results['2'])
{'aliases': {'amdmb': {'heat23': None},
'anandtech bbs': {'heat23': 'http://forum.anandtech.com'},
'arstechnica': {'heat23': None},
'geekhack': {'heatware': None},
'techpowerup!': {'heatware': None},
'webhostingtalk': {'heat23': None}},
'evaluations': {334221: {'comments': 'Great transaction, he sent money '
'via paypal and I shipped upon '
'payment.',
'date': '06-30-2005',
'forum': 'anandtech bbs',
'user': 'floben'},
344973: {'comments': 'What can I say about the owner of '
'heatware besides the fact that it '
'was an awesome transaction. I had '
'no worries about shipping first, '
'and his great communication '
'throughout the transaction put me '
'at ease.',
'date': '08-17-2005',
'forum': 'anandtech bbs',
'user': 'jackson18249'},
345198: {'comments': 'Quick payment & good communication. '
'You cannot ask for a smoother '
'transaction!',
'date': '08-23-2005',
'forum': 'anandtech bbs',
'user': 'hkklife'},
356225: {'comments': 'Super-fast payment, prompt response '
'to PMs. There was a delivery delay '
'(because of Katrina) but buyer was '
'very patient and kept in touch. '
'Thanks!',
'date': '09-27-2005',
'forum': 'anandtech bbs',
'user': 'fornax'},
423266: {'comments': 'This was simply one of the best '
'transactions I have experienced on '
'Anandtech. I sent Heat23 a paypal '
'e-check (expecting for funds to '
'clear first) but he crosshipped '
'minutes later on a Saturday. Got '
'the package Monday morning in the '
'office. Awesome.',
'date': '08-14-2006',
'forum': 'anandtech bbs',
'user': 'jloor'},
425040: {'comments': 'Fast payment, smooth transaction... '
'Good guy to deal with! Thanks!',
'date': '08-23-2006',
'forum': 'anandtech bbs',
'user': 'Doctor Feelgood'},
425650: {'comments': 'Heat23 threw in a couple of '
'freebies and shipped everything out '
'lightspeed. Thanks Man!',
'date': '08-26-2006',
'forum': 'anandtech bbs',
'user': 'ScottyWH'},
425699: {'comments': 'This was a very smooth transaction. '
'Heat sent me payment and I sent him '
'the camera. I would gladly sell to '
'him again. Thanks!',
'date': '08-20-2006',
'forum': 'anandtech bbs',
'user': 'dak125'},
426236: {'comments': 'The transaction went great, seller '
'was the easy to deal with and the '
'shipping was fast. (Freebie '
'included)...Love to deal again in '
'the future...',
'date': '08-29-2006',
'forum': 'anandtech bbs',
'user': 'mackle'},
487916: {'comments': 'Good communication, paid via '
"Paypal, smooth deal. If you can\\'t "
'trust heat23, who can you trust?;)',
'date': '08-23-2007',
'forum': 'anandtech bbs',
'user': 'Tates'},
496656: {'comments': 'Nice guy to work with. His '
'contribution to the trading '
'community is definitely '
'appreicated!!! Thanks again heat. :)',
'date': '11-08-2007',
'forum': 'anandtech bbs',
'user': 'ELopes580'},
527657: {'comments': 'Though took a bit to get the deal '
'done, he was courteous, kept in '
'touch, and made the whole '
'experience awesome! Thanks for the '
"phone, it\\'s awesome!",
'date': '08-04-2008',
'forum': 'anandtech bbs',
'user': 'proxops-pete'},
621980: {'comments': 'Donation acknowledgement and thanks '
'received. Thanks for spending your '
'time building something to do good.',
'date': '07-11-2011',
'forum': 'heatware',
'user': 'AmboBartok'},
690634: {'comments': 'Got payment quickly, great '
'comunication. Would deal with again '
'anytime. A++++',
'date': '07-23-2014',
'forum': 'anandtech bbs',
'user': 'Sniper82'},
699942: {'comments': 'Receiver was packed very well, in '
'what appeared to be the original '
'box. This receiver was shipped from '
'CA to NY and was in beautiful '
'condition when it arrived. Heat23 '
'even included a couple HDMI cables. '
'The item was as described, shipped '
'promptly, packed very well, and is '
'working well as I type this. This '
'transaction could not have gone '
"better, and I\\'d definitely deal "
'with Heat23 again.',
'date': '03-03-2015',
'forum': 'anandtech bbs',
'user': 'NicePants42'}},
'location': 'Austin, TX',
'rating': {'negative': 0, 'neutral': 0, 'positive': 188}}
"""
if self.config_handler.get_heatware_method() in ['pm', 'both']:
pass
if self.config_handler.get_heatware_method() in ['post', 'both']:
pass
def shutdown(self):
self.heat_parse.kill()
def __repr__(self):
# This section is a carbon copy of the vanilla codebase.
# ( See: threading.Thread.__repr__ )
thread_status = 'initial'
if self._started.is_set():
thread_status = 'started'
self.is_alive()
if self._is_stopped:
thread_status = 'stopped'
if self._daemonic:
thread_status += ' daemon'
if self._ident is not None:
thread_status += ' %s' % self._ident
reddit_status = 'logged'
if self.is_logged_in():
reddit_status += '-in'
else:
reddit_status += '-out'
if self.is_oauth_session():
reddit_status += ' oauth2'
return "<%s.%s {'thread': (%s, %s), 'reddit': (%s, %s)} at %s>" % (
self.__class__.__module__, self.__class__.__name__,
self.name, thread_status, self.user, reddit_status, hex(id(self)))
class database_handler(shelve.DbfilenameShelf):
def __init__(self, data_file):
super(self.__class__, self).__init__(filename=data_file)
def get(self, key):
try:
return self[key.lower()]
except:
return {}
def set(self, key, val):
try:
assert(isinstance(val, dict))
cur = self.get(key.lower())
val = self.update(val, cur)
self[key.lower()] = val
return True
except:
return False
def remove(self, key):
try:
del self[key.lower()]
return True
except:
return False
def update(self, new_, orig):
for key, val in orig.items():
if isinstance(val, dict):
new_[key] = self.update(new_.get(key, {}), val)
else:
new_[key] = orig[key]
return new_
def terminate(self):
self.sync()
self.close()
class heatware_crawler(multiprocessing.Process):
def __init__(self, page_wait=0, deep_parse=False, rand_wait=False):
# TODO: See if heat is okay with maximum one request per sixty seconds.