-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpl_bot.py
160 lines (131 loc) · 3.83 KB
/
fpl_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
#!/usr/bin/python
import json
import praw
import requests
import time
import os
header = {'x-requested-with': 'XMLHttpRequest'}
URL = "http://www.fplstatistics.co.uk/Home/AjaxPricesHandler?sEcho=2&iColumns=12&sColumns=%2Cweb_name%2CPClubName%2CPosition%2CStatus%2CpercentSelected%2CCost%2CPriceChangesinGW%2Cunlockdt%2CNTIDelta%2CNTIPERCENTNJD%2CPId&mDataProp_0=0&sSearch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=false"
#&iDisplayStart=1&iDisplayLength=50
def mainFunction():
user_agent = "houseOfBalloons 1.1" #user_agent
r = praw.Reddit(user_agent=user_agent) # reddit instance
r.login(os.environ['REDDIT_USERNAME'], os.environ['REDDIT_PASSWORD']) #login
risers = getRisers()
fallers = getFallers()
if (risers and fallers):
today = time.strftime("%d %B %Y")
message = generateMessage(risers, fallers)
title = " Fantasy Premier League Price Change Predictions (" + today + ")"
r.submit('fantasypl', title, text = message)
print "Post Sent!"
return True
else:
print "Risers or fallers was null"
return False
def generateMessage(risers, fallers):
today = time.strftime("%d %B %Y")
newLine = "\r\n\r\n \r\n\r\n"
hr = "\r\n\r\n---\r\n\r\n"
message = "**PRICE RISE PREDICTIONS** \r\n"
message += newLine
message += "**Highly Likely Risers** \r\n"
message += genTable(risers['high'])
message += newLine
message += "**Likely Risers** \r\n"
message += genTable(risers['med'])
message += newLine
message += "**Possible Risers** \r\n"
message += genTable(risers['low'])
message += newLine
message += hr
message += "**PRICE FALL PREDICTIONS** \r\n"
message += newLine
message += "\r\n **Highly Likely Fallers** \r\n"
message += genTable(fallers['high'])
message += newLine
message += "**Likely Fallers** \r\n"
message += genTable(fallers['med'])
message += newLine
message += "**Possible Fallers** \r\n"
message += genTable(fallers['low'])
message += newLine
message += hr
message += "- Data Source: www.fplstatistics.co.uk \r\n - I am a bot. Message /u/trent_9002 for any criticism/suggestions."
return message
def genTable(data):
if not data:
message = "\r\n *None for today*\r\n"
return message
message = "\r\n"
message += "Player Name|Club|Position|Ownership(%)|Current Price|Likelihood\r\n"
message += ":-:|:-:|:-:|:-:|:-:|:-:\r\n"
for each in data:
name = each[1]
club = each[2]
pos = each[3]
owners = each[5]
price = each[6]
change = each[10]
message += name + "|" + club + "|" + pos + "|" + owners + "|" + price + "|" + change + "\r\n"
return message
def getRisers():
riseUrl = URL + "&iDisplayStart=0&iDisplayLength=50"
try:
output = requests.get(riseUrl, headers = header)
except Exception, e:
print "Risers wasn't downloaded"
return False
result = output.json()
try:
result = result["aaData"]
except Exception, e:
print "No aaData for risers was found"
return False
high = []
med = []
low = []
for elem in result:
val = elem[10]
if (float(val) >= 100):
#high prob fall
high.append(elem)
elif (float(val) >= 90):
med.append(elem)
elif (float(val) >= 80):
low.append(elem)
else:
break
retValue = {"high": high, "med": med, "low": low}
return retValue
def getFallers():
fallUrl = URL + "&iDisplayStart=488&iDisplayLength=588"
try:
output = requests.get(fallUrl, headers = header)
except Exception, e:
print "Fallers wasn't downloaded"
return False
result = output.json()
try:
result = result["aaData"]
except Exception, e:
print "No aaData for fallers was found"
return False
high = []
med = []
low = []
result.reverse()
for elem in result:
val = elem[10]
if (float(val) <= -100):
#high prob fall
high.append(elem)
elif (float(val) <= -95):
med.append(elem)
elif (float(val) <= -85):
low.append(elem)
else:
break
retValue = {"high": high, "med": med, "low": low}
return retValue
mainFunction()