This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
130 lines (104 loc) · 4.41 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
import requests
from bs4 import BeautifulSoup
import json
import os
import tokens
from twython import Twython
import time
def get_data(targetId):
url = 'http://www.ncaa.com/scoreboards/basketball-men/d1'
# url = 'https://www.ncaa.com/scoreboard/basketball-men/d1/2018/03/10'
soup = BeautifulSoup(requests.get(url).content, 'lxml')
return soup.find(id=targetId)
def get_date():
return get_data('scoreboardDateNav').select('.scoreboardDateNav-date.hasGames.current .scoreboardDateNav-dayNumber').getText()
def parse_row(row):
team = row.find_all('td', {'class': ['school']})[0].find('a').text.strip()
score = row.find_all('td', {'class': ['final']})[0].text.strip()
return team, score
def set_daily_file_path():
# path = 'data/' + get_date() + '.json'
path = 'data/' + 'TEST' + '.json'
return os.path.abspath(path)
def read_data(file):
file_path = file
if os.path.exists(file_path):
with open(file_path, 'r') as f:
local_data = json.load(f)
else:
local_data = {}
return local_data
def compare_web_and_local_data(web_data, local_data):
for k, v in web_data.items():
if k not in local_data:
local_data[k] = web_data[k]
return local_data
def get_tokens():
# return Twython(tokens.app_key, tokens.app_secret, tokens.oauth_token, tokens.oauth_token_secret)
twitter = Twython(tokens.APP_KEY, tokens.APP_SECRET)
auth = twitter.get_authentication_tokens()
return auth
def pair_teams_with_friends(data):
path = os.path.abspath('data/_friends.json')
friend_data = read_data(path)
for k, v in friend_data.items():
if k == data:
if not friend_data[k][2]:
friend = friend_data[k][0] + ' ' + friend_data[k][1]
name_first = friend_data[k][0]
conference = friend_data[k][3]
else:
friend = friend_data[k][2]
name_first = friend_data[k][0]
conference = friend_data[k][3]
return friend, name_first, conference
def post_result_sentences(updated_data):
twitter = get_tokens()
for k, v in updated_data.items():
away_team = updated_data[k][0]
away_score = int(updated_data[k][1])
home_team = updated_data[k][2]
home_score = int(updated_data[k][3])
is_posted = updated_data[k][4]
home_friend = pair_teams_with_friends(home_team)
away_friend = pair_teams_with_friends(away_team)
if not is_posted:
if away_score > home_score:
if away_friend and home_friend:
result = "FINAL: %s (%s) uses supreme friendship to beat %s (%s), %s-%s. %s moves on to the next round. #FMAA19" % (away_friend[0], away_team, home_friend[0], home_team, away_score, home_score, away_friend[1])
twitter.update_status(status=result)
print(result)
time.sleep(2)
updated_data[k][4] = True
else:
if away_friend and home_friend:
result = "FINAL: %s (%s) defeats %s (%s), %s-%s, with an excellent display of friendship. %s moves on to the next round. #FMAA19" % (home_friend[0], home_team, away_friend[0], away_team, home_score, away_score, home_friend[1])
twitter.update_status(status=result)
print(result)
time.sleep(2)
updated_data[k][4] = True
return updated_data
def write_data(final_data):
file_path = set_daily_file_path()
with open(file_path, 'w') as f:
json.dump(final_data, f, indent=4, separators=(',', ': '))
def main():
web_data = {}
is_posted = False
return get_data('scoreboard')
# def main():
# web_data = {}
# is_posted = False
# for game in get_data().find_all('section', class_='game'):
# if game.find('div', class_='final') is None:
# continue
# key = game['id'].split('/')[-1]
# away_team, away_score = parse_row(game.find_all('tr')[1])
# home_team, home_score = parse_row(game.find_all('tr')[2])
# web_data[key] = [away_team, away_score, home_team, home_score, is_posted]
# local_data = read_data(set_daily_file_path())
# updated_data = compare_web_and_local_data(web_data, local_data)
# final_data = post_result_sentences(updated_data)
# write_data(final_data)
if __name__ == '__main__':
main()