-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_nba_bot.py
136 lines (120 loc) · 5.08 KB
/
lib_nba_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
#/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
import argparse
import os
import praw
import re
import sys
nba_players = []
dict = {}
reddit = praw.Reddit('bot1')
def main(argv=sys.argv[1:]):
limit_count = 3
subreddit_name = "nba_bot_testing"
print(argv)
if len(argv) is 1:
if not isinstance(argv[0],str):
sys.exit("Given subreddit is not a string")
else:
subreddit_name = argv[0]
elif len(argv) is 2:
if not isinstance(argv[0],str) or not isinstance(argv[1],str):
if not isinstance(argv[1],str):
sys.exit("Given subreddit is not a string")
else:
sys.exit("Given limit is not a int")
else:
subreddit_name = argv[0]
limit_count = int(argv[1])
elif len(argv) >= 3:
sys.exit("Too many arguments")
posts_replied_to = repeat_comments()
load_nba_players()
title_parse(subreddit_name, limit_count,posts_replied_to)
text_parse(subreddit_name, limit_count,posts_replied_to)
post_comments(posts_replied_to)
write_to_file(posts_replied_to)
def repeat_comments():
if not os.path.isfile("posts_replied_to.txt"):
posts_replied_to = []
else:
# Read the file into a list and remove any empty values
with open("posts_replied_to.txt", "r") as f:
posts_replied_to = f.read()
posts_replied_to = posts_replied_to.split("\n")
posts_replied_to = list(filter(None, posts_replied_to))
return posts_replied_to
def load_nba_players():
f = open("names.txt", "r")
for x in f:
name = [y.strip() for y in x.split(',')]
first_name = name[1].lower()
last_name = name[0].lower()
nba_players.append(first_name + " " + last_name)
f.close()
def title_parse(subreddit_name, limit,posts_replied_to):
subreddit = reddit.subreddit(subreddit_name)
for submission in subreddit.new(limit=3):
if submission.id not in posts_replied_to:
title = submission.title
data = title.split()
first = 0
second = 1
for temp in data:
try:
first_name = re.sub("[^a-zA-Z]+", "", data[first]).lower()
last_name = re.sub("[^a-zA-Z]+", "", data[second]).lower()
potential_name = first_name + " " + last_name
potential_name = potential_name.lower()
print(potential_name)
if potential_name in nba_players:
potential_name = first_name.capitalize() + " " + last_name.capitalize()
comment = "[" + potential_name + "]" + "(" + "https://www.basketball-reference.com/players/" + last_name[:1] + "/" + last_name[:5] + first_name[:2] + "01.html" + ")" + "\n\n"
if (dict.get(submission) is None):
dict[submission] = comment
else:
comment += dict[submission]
dict[submission] = comment
first += 1
second += 1
except IndexError:
pass
def text_parse(subreddit_name,limit_count,posts_replied_to):
subreddit = reddit.subreddit(subreddit_name)
for submission in subreddit.new(limit=limit_count):
if submission.id not in posts_replied_to:
text = submission.selftext
data = text.split()
first = 0
second = 1
for temp in data:
try:
first_name = re.sub("[^a-zA-Z]+", "", data[first]).lower()
last_name = re.sub("[^a-zA-Z]+", "", data[second]).lower()
potential_name = first_name + " " + last_name
potential_name = potential_name.lower()
print(potential_name)
if potential_name in nba_players:
potential_name = first_name.capitalize() + " " + last_name.capitalize()
comment = "[" + potential_name + "]" + "(" + "https://www.basketball-reference.com/players/" + last_name[:1] + "/" + last_name[:5] + first_name[:2] + "01.html" + ")" + "\n\n"
if (dict.get(submission) is None):
dict[submission] = comment
else:
comment += dict[submission]
dict[submission] = comment
first += 1
second += 1
except IndexError:
pass
def post_comments(posts_replied_to):
first = True
for submission in dict:
if first:
submission.reply("Players mentioned in this thread: \n\n" + dict[submission])
first = False
else:
submission.reply(dict[submission])
posts_replied_to.append(submission.id)
def write_to_file(posts_replied_to):
with open("posts_replied_to.txt", "w") as f:
for post_id in posts_replied_to:
f.write(post_id + "\n")