summaryrefslogtreecommitdiffstats
path: root/skripti/poprbot.py
blob: 2c8efe77eefe3bb6b0cd94205a7d3b4e363da736 (plain) (blame)
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
#!/usr/bin/python3
from sys import argv
from os import getenv
import requests
import json
from datetime import datetime
from time import sleep
from bs4 import BeautifulSoup
popr = "https://popr.uni-lj.si/"
def prijavi_se(s, event, slotid):
	form = BeautifulSoup(s.get(popr + "student/events.html?page=booking&id=" + str(event) + "&slotId=" + str(slotid) + "&bookingAction=BOOK").text, features="lxml").find("form")
	dejta = "_eventId_book=Submit"
	for inp in form.findAll("input"):
		try:
			dejta += "&" + inp.attrs["name"] + "=" + inp.attrs["value"]
		except KeyError:
			pass
	s.post(popr + "student/events.html", data=dejta, headers={"Content-Type": "application/x-www-form-urlencoded"})
def get_event(s, event):
	s.get(popr + "student/svc/events.html") # session keepalive
	jwt = s.get(popr + "api/user/token/refresh").text
	s.headers.update({"Authorization": "Bearer " + jwt});
	return json.loads(s.get(popr + "ext/svc/events-api-0-0/events/user/event/slots/" + event + "?locale=en").text)[0];
def main():
	if len(argv) < 2:
		print("Prvi argument naj bo numerični ID dogodka.")
		return
	event = argv[1]
	slot = None
	if len(argv) >= 3:
		slot = int(argv[2])
	if getenv("COOKIES") == None:
		print("Okoljska spremenljivka COOKIES naj vsebuje piškotke v obliki ime=vrednost;ime2=vrednost2;... Potrebujete dva piškotka -- GUANXI_GUARD_SERVICE_PROVIDER... in JSESSION")
		return
	s = requests.Session()
	for pair in getenv("COOKIES").split(";"):
		s.cookies.set(pair.split("=")[0], pair.split("=")[1]);
	ev = get_event(s, event)
	slotid = ev["slots"][0]["id"]
	if len(ev["slots"]) > 1 and slot == None:
		print("Za izbrani dogodek obstaja več slotov. V drugi argument bi morali napisati številko slota, začenši z 0. Sloti na voljo:");
		for sl in range(len(ev["slots"])):
			print(str(sl) + "\t" + ev["slots"][sl]["start"] + "-" + ev["slots"][sl]["end"] + "\t" + ev["slots"][sl]["location"]["name"])
		return
	if slot != None:
		slotid = ev["slots"][slot]["id"]
	else:
		slot = 0
	if "message" in ev["slots"][slot].keys() and "Booking not yet open" in ev["slots"][slot]["message"]:
		ura = ev["slots"][slot]["message"].split(" ")[-1]
		datum = ev["slots"][slot]["message"].split(" ")[-3]
		kdaj = datetime(int(datum.split("-")[2]), ["Nul", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"].index(datum.split("-")[1]), int(datum.split("-")[0]), int(ura.split(":")[0]), int(ura.split(":")[1]))
		print("Prijave na dogodek se odpro " + kdaj.ctime() + ". Do takrat čakam, nato pa bom snajpal prijavo.")
		while True:
			if (kdaj-datetime.now()).total_seconds() < 190:
				break
			ev2 = get_event(s, event);
			if ev2 != ev:
				print("NAPAKA! Dogodek se je spremenil!")
				print("prejšnji: " + ev)
				print("trenutni: " + ev2)
				return
			sleep(180)
		sleep((kdaj-datetime.now()).total_seconds()-3)
		retries = 0
		while True:
			prijavi_se(s, event, slotid)
			if (kdaj-datetime.now()).total_seconds() < -3:
				ev = get_event(s, event)
				if ev["slots"][slot]["booked"] == True:
					print("Prijavljeni ste.")
					return
				else:
					if ev["slots"][slot]["bookable"] == False:
						retries += 1
						if retries < 10:
							continue
					print("NAPAKA! Niste prijavljeni.")
					return
	prijavi_se(s, event, slotid)
	ev = get_event(s, event)
	if ev["slots"][slot]["booked"] == True:
		print("Prijavljeni ste.")
	else:
		print("NAPAKA! Niste prijavljeni.")
main()