-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatware.py
72 lines (63 loc) · 2.59 KB
/
heatware.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
from ConfigParser import SafeConfigParser
from time import sleep
import logging
import os
import praw
import re
import sys
# load config file
containing_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
cfg_file = SafeConfigParser()
path_to_cfg = os.path.join(containing_dir, 'config.cfg')
cfg_file.read(path_to_cfg)
username = cfg_file.get('reddit', 'username')
password = cfg_file.get('reddit', 'password')
subreddit = cfg_file.get('reddit', 'subreddit')
multiprocess = cfg_file.get('reddit', 'multiprocess')
link_id = cfg_file.get('heatware', 'link_id')
regex = cfg_file.get('heatware', 'regex')
group = int(cfg_file.get('heatware', 'group'))
respond = cfg_file.get('heatware', 'respond')
added_msg = cfg_file.get('heatware', 'added_msg')
# Configure logging
logging.basicConfig(level=logging.INFO, filename='actions.log')
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)
def main():
while True:
try:
logging.info('Logging in as /u/' + username)
if multiprocess == 'true':
from praw.handlers import MultiprocessHandler
handler = MultiprocessHandler()
r = praw.Reddit(user_agent=username, handler=handler)
else:
r = praw.Reddit(user_agent=username)
r.login(username, password)
flaircount = 0
# Get the submission and the comments
submission = r.get_submission(submission_id=link_id)
for comment in submission.comments:
if not hasattr(comment, 'author'):
continue
if comment.is_root == True:
logging.info('Parsing comment from /u/%s.' % comment.author)
heatware = re.search(regex, comment.body)
if heatware:
url = heatware.group(group)
if not comment.author_flair_text:
flaircount = flaircount + 1
if comment.author_flair_css_class:
comment.subreddit.set_flair(comment.author, url, comment.author_flair_css_class)
else:
comment.subreddit.set_flair(comment.author, url, 'i-none')
if respond == 'true':
comment.reply(added_msg)
if flaircount > 0:
logging.info('Set flair for ' + str(flaircount) + ' user(s)!')
except Exception as e:
logging.error(e)
break
sleep(600)
if __name__ == '__main__':
main()