-
Notifications
You must be signed in to change notification settings - Fork 1
/
autharea.py
49 lines (43 loc) · 1.53 KB
/
autharea.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
import sys
import re
# local libs
from commonimports import parse
from util import importlocal, FIMFICTION, get_url, get_session, fail
bs4 = importlocal('bs4')
requests = importlocal('requests')
usr = ''
pas = ''
logged_in = False
def possibly_req_auth(username, password):
global usr, pas, logged_in, localCookie
if logged_in:
return
if not usr:
usr = username or input('Username: ')
if not pas:
pas = password or input('Password: ')
login_data = parse.urlencode(
{'username': usr,'password': pas}
).encode('ascii')
print('open', FIMFICTION + '/ajax/login.php', login_data)
ret = get_session().post(FIMFICTION + '/ajax/login.php', data={'username': usr, 'password': pas})
print(type(ret.json()))
if 'signing_key' not in ret.json():
fail('Login failed, check your username and password')
logged_in = True
return usr, pas
patterns = {'bookshelfid': re.compile(r'/bookshelf/(\d+)/.+')}
def get_user_shelves(username, password):
(username, password) = possibly_req_auth(username, password)
library = get_url(FIMFICTION + ('/user/{}/library'.format(username)))
libsoup = bs4.BeautifulSoup(library)
shlvs = libsoup(class_='bookshelf-card')
lib = []
for shlf in shlvs:
count = int(shlf('a')[0].b.string)
if count != 0:
link = str(shlf('a')[0]['href'])
match = patterns['bookshelfid'].match(link)
lib.append(int(match.group(1)))
return lib
__import__('util').autharea = sys.modules[__name__]