-
Notifications
You must be signed in to change notification settings - Fork 0
/
featurescraper_NN.py
175 lines (156 loc) · 5.5 KB
/
featurescraper_NN.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
import numpy as np
import pandas as pd
import praw
import psaw
from tinydb import TinyDB, Query
import datetime
import time
query = Query()
# PRAW + PSAW
reddit = praw.Reddit(client_id=secrets.CLIENT_ID,
client_secret=secrets.CLIENT_SECRET,
user_agent=secrets.USERAGENT
)
print("Read only:", reddit.read_only)
ps = psaw.PushshiftAPI(reddit)
# Data
DATA_PATH = "C:/Users/peter/OneDrive/Documents/THESIS/score.csv"
df = pd.read_csv(DATA_PATH)
# return submissions from given time period
def sub_query(team, d1, d2):
start_epoch = time.mktime(d1.timetuple())
end_epoch = time.mktime(d2.timetuple())
submissions = list(ps.search_submissions(after=int(start_epoch),
before=int(end_epoch),
sort='desc',
sort_type='score',
subreddit=str(team),
limit=100))
if len(submissions) < 1:
print("NO SUBMISSIONS COLLECTED", team)
sub_dict = []
for sub in submissions:
thisdict = {
"id": sub.id,
"title": sub.title,
"score": sub.score
}
sub_dict.append(thisdict)
return sub_dict
# queries comments from given time period
def com_query(team, d1, d2):
start_epoch = time.mktime(d1.timetuple())
end_epoch = time.mktime(d2.timetuple())
comments = list(ps.search_comments(after=int(start_epoch),
before=int(end_epoch),
sort='desc',
sort_type='score',
subreddit=str(team),
limit=10))
if len(comments) < 1:
print("NO COMMENTS COLLECTED", team)
comment_dict= []
for com in comments:
thisdict = {
"id": com.id,
"body":com.body,
"score":com.score
}
comment_dict.append(thisdict)
return comment_dict
# DICTIONARIES
team_dict1 = {
"San Francisco 49ers": "49ers",
"Arizona Cardinals": "AZCardinals",
"Cincinnati Bengals": "bengals",
"Cleveland Browns": "Browns",
"Tampa Bay Buccaneers": "buccaneers",
"Buffalo Bills": "buffalobills",
"Los Angeles Chargers": "Chargers",
"San Diego Chargers": "Chargers",
"Chicago Bears": "CHIBears",
"Indianapolis Colts": "Colts",
"Dallas Cowboys": "cowboys",
"Denver Broncos": "DenverBroncos",
"Detroit Lions": "detroitlions",
"Philadelphia Eagles": "eagles",
"Atlanta Falcons": "falcons",
"Green Bay Packers": "GreenBayPackers",
"Jacksonville Jaguars": "Jaguars",
"Kansas City Chiefs": "KansasCityChiefs",
"Los Angeles Rams": "losangelesrams",
"Miami Dolphins": "miamidolphins",
"Minnesota Vikings": "minnesotavikings",
"New York Giants": "NYGiants",
"New York Jets": "nyjets",
"Oakland Raiders": "oaklandraiders",
"Carolina Panthers": "panthers",
"New England Patriots": "Patriots",
"Baltimore Ravens": "ravens",
"Washington Redskins": "Redskins",
"New Orleans Saints": "Saints",
"Seattle Seahawks": "Seahawks",
"Pittsburgh Steelers": "steelers",
"St. Louis Rams": "StLouisRams",
"Tennessee Titans": "Tennesseetitans",
"Houston Texans": "Texans"
}
team_dict2 = {
"SF": "49ers",
"ARI": "AZCardinals",
"CIN": "bengals",
"CLE": "Browns",
"TB": "buccaneers",
"BUF": "buffalobills",
"LAC": "Chargers",
"CHI": "CHIBears",
"IND": "Colts",
"DAL": "cowboys",
"DEN": "DenverBroncos",
"DET": "detroitlions",
"PHI": "eagles",
"ATL": "falcons",
"GB": "GreenBayPackers",
"JAX": "Jaguars",
"KC": "KansasCityChiefs",
"LAR": "losangelesrams",
"MIA": "miamidolphins",
"MIN": "minnesotavikings",
"NYG": "NYGiants",
"NYJ": "nyjets",
"OAK": "oaklandraiders",
"CAR": "panthers",
"NE": "Patriots",
"BAL": "ravens",
"WAS": "Redskins",
"NO": "Saints",
"SEA": "Seahawks",
"PIT": "steelers",
"RAM": "StLouisRams",
"TEN": "Tennesseetitans",
"HOU": "Texans"
}
print(df.shape[0])
df['schedule_date'] = pd.to_datetime(df['schedule_date'])
df['home_wts'] = ""
for delta in range(30):
df['home_submissions_'+str(delta)] = ""
df['away_submissions_'+str(delta)] = ""
df['home_comments_'+str(delta)] = ""
df['away_comments_'+str(delta)] = ""
for i, row in df.iterrows():
date = df['schedule_date'][i]
home = team_dict1[df['team_home'][i]]
away = team_dict1[df['team_away'][i]]
year = date.year
# rams city change
if year < 2016 and df['team_favorite_id'][i] == "LAR":
df['team_favorite_id'][i] = "RAM"
print("Processing row ", i, ":", home, " @ ", away, date)
for delta in range(30):
print(delta)
df['home_submissions_'+str(delta)][i] = sub_query(home, date - datetime.timedelta(days=delta+1), date- datetime.timedelta(days=delta))
df['home_comments_'+str(delta)][i] = com_query(home, date - datetime.timedelta(days=delta+1), date- datetime.timedelta(days=delta))
df['away_submissions_'+str(delta)][i] = sub_query(away, date - datetime.timedelta(days=delta+1), date- datetime.timedelta(days=delta))
df['away_comments_'+str(delta)][i] = com_query(away, date - datetime.timedelta(days=delta+1), date- datetime.timedelta(days=delta))
df.to_pickle('dataNN')