Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

"""LICENSE 

Copyright 2019 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of bundesliga-tippspiel-reminder (btr). 

 

btr is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

btr is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with btr. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

from typing import List, Dict, Any, Optional 

from sqlalchemy.orm import Session 

from bokkichat.entities.message.TextMessage import TextMessage 

from kudubot.Bot import Bot 

from kudubot.db.Address import Address as Address 

from kudubot.parsing.CommandParser import CommandParser 

from bundesliga_tippspiel_reminder import version 

from bundesliga_tippspiel_reminder.db.ApiKey import ApiKey 

from bundesliga_tippspiel_reminder.db.Reminder import Reminder 

from bundesliga_tippspiel_reminder.BundesligaTippspielReminderParser import \ 

BundesligaTippspielReminderParser 

from bundesliga_tippspiel_reminder.api import api_request, api_is_authorized 

 

 

class BundesligaTippspielReminderBot(Bot): 

""" 

The bundesliga tippspiel reminder bot 

""" 

 

@classmethod 

def name(cls) -> str: 

""" 

:return: The name of the bot 

""" 

return "bundesliga-tippspiel-reminder" 

 

@classmethod 

def version(cls) -> str: 

""" 

:return: The current version of the bot 

""" 

return version 

 

@property 

def bg_pause(self) -> int: 

""" 

The pause between background iterations 

:return: The pause in seconds 

""" 

return 600 

 

@classmethod 

def parsers(cls) -> List[CommandParser]: 

""" 

:return: The parsers for the bot 

""" 

return [BundesligaTippspielReminderParser()] 

 

def is_authorized( 

self, 

address: Address, 

_: Dict[str, Any], 

db_session: Session 

) -> bool: 

""" 

Checks if a user is authorized 

:param address: The user to check 

:param _: possible command arguments 

:param db_session: The database session to use 

:return: True if authorized, False otherwise 

""" 

return self._get_api_key(address, db_session) is not None 

 

@classmethod 

def unauthorized_message(cls) -> str: 

""" 

:return: A custom message sent to a user if they tried to access 

a feature that requires authorization without being 

authorized 

""" 

return "Not authorized, use /login <username> <password> first" 

 

@staticmethod 

def _get_api_key(address: Address, db_session: Session) \ 

-> Optional[str]: 

""" 

Retrieves the API key for an address 

:param address: The address for which to get the API key 

:param db_session: The database session to use 

:return: The API key, or None if no API key exists 

""" 

api_key = db_session.query(ApiKey).filter_by(kudubot_user=address)\ 

.first() 

return None if api_key is None else api_key.key 

 

@staticmethod 

def _get_reminder(address: Address, db_session: Session) \ 

-> Optional[Reminder]: 

""" 

Retrieves the reminder object for a user from the database 

:param address: The address of the user 

:param db_session: The database session to use 

:return: The Reminder object or None if none exist. 

""" 

return db_session.query(Reminder).filter_by(kudubot_user=address)\ 

.first() 

 

def _on_login( 

self, 

sender: Address, 

args: Dict[str, Any], 

db_session: Session 

): 

""" 

Handles a login command 

:param sender: The sender of the message 

:param args: The command arguments 

:param db_session: The database session 

:return: None 

""" 

 

data = {"username": args["username"], "password": args["password"]} 

response = api_request("api_key", "post", data) 

 

if response["status"] == "ok": 

key = ApiKey( 

kudubot_user=sender, 

tippspiel_user=args["username"], 

key=response["data"]["api_key"] 

) 

db_session.add(key) 

db_session.commit() 

reply = "Logged in successfully" 

else: 

reply = "Login unsuccessful" 

 

reply = TextMessage(self.connection.address, sender, reply, "Login") 

self.connection.send(reply) 

 

def _on_is_authorized( 

self, 

sender: Address, 

_: Dict[str, Any], 

db_session: Session 

): 

""" 

Handles an is_authorized command 

:param sender: The sender of the message 

:param _: The command arguments 

:param db_session: The database session to use 

:return: None 

""" 

api_key = self._get_api_key(sender, db_session) 

reply = "yes" if api_is_authorized(api_key) else "no" 

self.connection.send(TextMessage( 

self.connection.address, sender, reply, "Authorized" 

)) 

 

@Bot.auth_required 

def _on_leaderboard( 

self, 

address: Address, 

_: Dict[str, Any], 

db_session: Session 

): 

""" 

Handles a leaderboard command 

:param address: The sender of the command 

:param _: Command arguments 

:param db_session: The database session to use 

:return: None 

""" 

api_key = self._get_api_key(address, db_session) 

response = api_request("leaderboard", "get", {}, api_key) 

 

if response["status"] == "ok": 

leaderboard = response["data"]["leaderboard"] 

formatted = [] 

for i, (user, points) in enumerate(leaderboard): 

formatted.append("{}: {} ({})".format( 

i + 1, 

user["username"], 

points 

)) 

self.send_txt(address, "\n".join(formatted), "Leaderboard") 

 

@Bot.auth_required 

def _on_reminder_state( 

self, 

address: Address, 

_: Dict[str, Any], 

db_session: Session 

): 

""" 

Handles a reminder_state command 

:param address: The sender of the command 

:param _: Command arguments 

:param db_session: The database session to use 

:return: None 

""" 

reminder = self._get_reminder(address, db_session) 

if reminder is None: 

reply = "No reminder set" 

else: 

reply = "Reminder set to go off {} hours before a match.".format( 

int(reminder.reminder_time / 3600) 

) 

self.send_txt(address, reply, "Reminder State") 

 

@Bot.auth_required 

def _on_activate_reminder( 

self, 

address: Address, 

args: Dict[str, Any], 

db_session: Session 

): 

""" 

Handles activating reminders 

:param address: The sender of the message 

:param args: Command arguments 

:param db_session: The database session to use 

:return: None 

""" 

reminder = self._get_reminder(address, db_session) 

 

hours = args["hours"] 

seconds = hours * 3600 

if hours < 1 or hours > 120: 

self.send_txt( 

address, 

"Reminders can only be 1-120 hours", 

"Invalid Reminder Time" 

) 

return 

 

if reminder is None: 

reminder = Reminder( 

kudubot_user=address, 

reminder_time=seconds 

) 

db_session.add(reminder) 

else: 

reminder.reminder_time = seconds 

 

db_session.commit() 

 

self.send_txt( 

address, 

"Reminder set to {} hours".format(hours), 

"Reminder Time set" 

) 

 

@Bot.auth_required 

def _on_deactivate_reminder( 

self, 

address: Address, 

_: Dict[str, Any], 

db_session: Session 

): 

""" 

Deactivates a reminder 

:param address: The sender of the message 

:param _: Command arguments 

:param db_session: The database session to use 

:return: None 

""" 

reminder = self._get_reminder(address, db_session) 

if reminder is not None: 

db_session.delete(reminder) 

db_session.commit() 

self.send_txt(address, "Reminder Deactivated", "Reminder Deactivated") 

 

def bg_iteration(self, _: int, db_session: Session): 

""" 

Implements behaviours of the Background thread that periodically 

checks if any reminders are due 

:return: None 

""" 

self.logger.info("Checking for due reminders") 

 

for reminder in db_session.query(Reminder).all(): 

api_key = self._get_api_key(reminder.kudubot_user, db_session) 

 

if not api_is_authorized(api_key): 

continue 

 

resp = api_request("match", "get", {}, api_key) 

matches = resp["data"]["matches"] 

 

bets = api_request("bet", "get", {}, api_key)["data"]["bets"] 

due = reminder.get_due_matches(matches, bets) 

 

if len(due) > 0: 

body = "Reminders for hk-tippspiel.com:\n\n" 

for match in due: 

body += "{} vs. {}\n".format( 

match["home_team"]["name"], 

match["away_team"]["name"] 

) 

msg = TextMessage( 

self.connection.address, 

reminder.kudubot_user, 

body, 

"Reminders" 

) 

self.connection.send(msg) 

last_match = max(due, key=lambda x: x["kickoff"]) 

reminder.last_reminder = last_match["kickoff"] 

db_session.commit() 

 

self.logger.info("Finished checking for due reminders")