This repository was archived by the owner on May 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmubiscli.py
executable file
·195 lines (172 loc) · 6.16 KB
/
mubiscli.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/python3.6
# to-do
# Error checking
# --> if there is no internet and no file
import getpass # Parola görünmez oluyor
import requests
import codecs # encoding="utf-8" desteği
import dateutil.parser as dparse
import time
import json
import calendar
import argparse
import os # $HOME path'i için
from bs4 import BeautifulSoup
from datetime import datetime
user_sessions = 'http://mubistip.maltepe.edu.tr/user_sessions'
parser = argparse.ArgumentParser()
nexti = parser.add_argument('-n', '--next', action='store_true',
help='Show the next week\'s program.', dest="nexti")
prev = parser.add_argument('-p', '--previous', action='store_true',
help='Show previous week\'s program.', dest="prev")
cach = parser.add_argument('-c', '--cache', action='store_true',
help='Show the cached calendar.', dest='cach')
args = parser.parse_args()
# Argümana göre dosya adı belirlenmesi
if args.nexti:
fname = ".takvim_next.json"
elif args.prev:
fname = ".takvim_prev.json"
else:
fname = ".takvim.json"
fname = os.path.join(os.path.expandvars("$HOME"),fname)
# Pazartesi ve Cumartesi 00:00 zamanı
def mond():
gunFarki_monday = (datetime
.isoweekday(datetime.now())-1)
monday=(int(datetime.now()
.strftime("%d")) - gunFarki_monday)
current_month = datetime.now().month
current_year = datetime.now().year
mon_ = datetime(current_year,current_month,
monday,0,0,0,0).timetuple()
mon__ = int(time.mktime(mon_))
return mon__
def satu():
gunFarki_saturday = (datetime
.isoweekday(datetime.now())-6)
saturday = (int(datetime.now()
.strftime("%d")) - gunFarki_saturday)
current_month = datetime.now().month
current_year = datetime.now().year
sat_ = datetime(current_year,current_month,
saturday,0,0,0,0).timetuple()
sat__ = int(time.mktime(sat_))
return sat__
# POST içeriğindeki öğrenci numarası
def html_parse(html):
soup = BeautifulSoup(html, 'html.parser')
ogrenci_no = soup.find("td").find("input")["title"].split("/")[3]
return ogrenci_no
# MUBIS'e giriş bilgileri
def usr_inf():
okul_no = input("Okul Numarası: ")
passw = getpass.getpass()
payload = {
"user_session[login]" : okul_no,
"user_session[password]" : passw,
"authenticity_token" : "3s5T/B0TpuFuwNFwNrqsDIqwW3FnA6lb4Z6/3FXb6B4=",
"utf8" : "✓",
"commit" : "Giriş Yap"
}
return payload
# Takvimi almak için gönderilen POST paketini oluşturma
def url_prep():
st_num = html_parse(p.text)
_url = 'http://mubistip.maltepe.edu.tr/ajanda/ogrenci_event_list.json?ogrenci='
start = mond()
end = satu()
if args.nexti:
# 604800 = Epoch değerinde 1 hafta
start = (start + 604800)
end = (end + 604800)
elif args.prev:
start = (start - 604800)
end = (end - 604800)
url = '{0}{1}&start={2}&end={3}'.format(_url ,st_num, start, end)
return url
# Takvimi dosyaya yazma
def wrtefile(buff):
jfilew = open(fname, "w")
jfilew.write(buff)
jfilew.close()
return 0
# Takvim sıralamasının düzenlenmesi
def listing(jfile):
jbuff = json.load(jfile) #object_pairs_hook=collections.OrderedDict)
# Gün bazlı ayırma için listeler
gun_programi, mon, tue, wed, thu, fri= ([] for q in range(6))
for entry in jbuff:
_ders_gunu = datetime.isoweekday(dparse.
parse(entry['start'].split("T")[0].split("-")[2]))
if _ders_gunu == 1:
mon.append(entry)
elif _ders_gunu == 2:
tue.append(entry)
elif _ders_gunu == 3:
wed.append(entry)
elif _ders_gunu == 4:
thu.append(entry)
else:
fri.append(entry)
if mon:
gun_programi.append(list(mon))
if tue:
gun_programi.append(list(tue))
if wed:
gun_programi.append(list(wed))
if thu:
gun_programi.append(list(thu))
if fri:
gun_programi.append(list(fri))
# I did not understand anything in here but it works...
for liste in gun_programi:
for mem in liste:
liste.sort(key=lambda mem:mem['start'])
return gun_programi
# Takvimi output ederkenki düzenin belirlenmesi
def outputing(gun_programi):
for gun in gun_programi:
_date=dparse.parse(gun[0]['start'].split("T")[0]).date()
_date_int = calendar.day_name[_date.weekday()]
print("---------------------------")
print(_date,"-",_date_int)
for entry in gun:
ders = entry['title']
_end_h=dparse.parse(entry['end']
.split("+")[0]).time().strftime("%H:%M")
_start_h = dparse.parse(entry['start']
.split("+")[0]).time().strftime("%H:%M")
print("{}-{}:{}".format(_start_h, _end_h, ders))
print("---------------------------")
print("\n")
if not args.cach:
try:
with requests.Session() as s:
payload = usr_inf()
p = s.post(user_sessions, data=payload)
if p.url == user_sessions:
print("MUBIS çalışmıyor. Kaydedilmiş program açılıyor.\n")
jfile = codecs.open(fname, mode="r", encoding="utf-8")
outputing(listing(jfile))
jfile.close()
else:
print("Oturum açıldı.")
url = url_prep()
_json_buff = s.get(url)
wrtefile(_json_buff.text)
print("Yedek alındı.\n")
jfile = codecs.open(fname, mode="r", encoding="utf-8")
outputing(listing(jfile))
jfile.close()
except requests.exceptions.RequestException as exc:
print("Bağlantı problemi:", exc)
print("Kaydedilmiş ders programı açılıyor.\n")
jfile = codecs.open(fname, mode="r", encoding="utf-8")
outputing(listing(jfile))
jfile.close()
else:
print("Kaydedilmiş ders programı açılıyor.\n")
jfile = codecs.open(fname, mode="r", encoding="utf-8")
outputing(listing(jfile))
jfile.close()