WSGI for logging in (POST request)
This commit is contained in:
parent
31a60602de
commit
60df0aec6d
@ -25,7 +25,18 @@ def save_accounts():
|
|||||||
with open(os.path.join(settings.data_dir, 'accounts.txt'), 'w', encoding='utf-8') as f:
|
with open(os.path.join(settings.data_dir, 'accounts.txt'), 'w', encoding='utf-8') as f:
|
||||||
f.write(json.dumps(to_save, indent=4))
|
f.write(json.dumps(to_save, indent=4))
|
||||||
|
|
||||||
def add_account(username, password, save, use_tor):
|
def cookiejar_from_lwp_str(lwp_str):
|
||||||
|
lwp_str = "#LWP-Cookies-2.0\n" + lwp_str # header required by _really_load for reading from "file"
|
||||||
|
cookiejar = http.cookiejar.LWPCookieJar()
|
||||||
|
# HACK: cookiejar module insists on using filenames and reading files for you,
|
||||||
|
# so present a StringIO to this internal method which takes a filelike object
|
||||||
|
cookiejar._really_load(io.StringIO(lwp_str), "", False, False)
|
||||||
|
return cookiejar
|
||||||
|
|
||||||
|
def account_cookiejar(channel_id):
|
||||||
|
return cookiejar_from_lwp_str('\n'.join(accounts[channel_id]['cookies']))
|
||||||
|
|
||||||
|
def _add_account(username, password, save, use_tor):
|
||||||
cookiejar = http.cookiejar.LWPCookieJar()
|
cookiejar = http.cookiejar.LWPCookieJar()
|
||||||
result = _login(username, password, cookiejar, use_tor)
|
result = _login(username, password, cookiejar, use_tor)
|
||||||
if isinstance(result, dict):
|
if isinstance(result, dict):
|
||||||
@ -40,16 +51,24 @@ def add_account(username, password, save, use_tor):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def cookiejar_from_lwp_str(lwp_str):
|
def add_account(env, start_response):
|
||||||
lwp_str = "#LWP-Cookies-2.0\n" + lwp_str # header required by _really_load for reading from "file"
|
fields = env['fields']
|
||||||
cookiejar = http.cookiejar.LWPCookieJar()
|
if 'save' in fields and fields['save'][0] == "on":
|
||||||
# HACK: cookiejar module insists on using filenames and reading files for you,
|
save_account = True
|
||||||
# so present a StringIO to this internal method which takes a filelike object
|
else:
|
||||||
cookiejar._really_load(io.StringIO(lwp_str), "", False, False)
|
save_account = False
|
||||||
return cookiejar
|
|
||||||
|
|
||||||
def account_cookiejar(channel_id):
|
if 'use_tor' in fields and fields['use_tor'][0] == "on":
|
||||||
return cookiejar_from_lwp_str('\n'.join(accounts[channel_id]['cookies']))
|
use_tor = True
|
||||||
|
else:
|
||||||
|
use_tor = False
|
||||||
|
|
||||||
|
if _add_account(fields['username'][0], fields['password'][0], save_account, use_tor ):
|
||||||
|
start_response('200 OK', () )
|
||||||
|
return b'Account successfully added'
|
||||||
|
else:
|
||||||
|
start_response('200 OK', () )
|
||||||
|
return b'Failed to add account'
|
||||||
|
|
||||||
def get_account_login_page(env, start_response):
|
def get_account_login_page(env, start_response):
|
||||||
start_response('200 OK', [('Content-type','text/html'),] )
|
start_response('200 OK', [('Content-type','text/html'),] )
|
||||||
|
@ -20,8 +20,7 @@ get_handlers = {
|
|||||||
}
|
}
|
||||||
post_handlers = {
|
post_handlers = {
|
||||||
'edit_playlist': local_playlist.edit_playlist,
|
'edit_playlist': local_playlist.edit_playlist,
|
||||||
|
'login': accounts.add_account,
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def youtube(env, start_response):
|
def youtube(env, start_response):
|
||||||
@ -132,24 +131,6 @@ def youtube(env, start_response):
|
|||||||
else:
|
else:
|
||||||
start_response('303 See Other', (('Location', common.URL_ORIGIN + '/comment_delete_fail'),) )
|
start_response('303 See Other', (('Location', common.URL_ORIGIN + '/comment_delete_fail'),) )
|
||||||
|
|
||||||
elif path == "/login":
|
|
||||||
if 'save' in fields and fields['save'][0] == "on":
|
|
||||||
save_account = True
|
|
||||||
else:
|
|
||||||
save_account = False
|
|
||||||
|
|
||||||
if 'use_tor' in fields and fields['use_tor'][0] == "on":
|
|
||||||
use_tor = True
|
|
||||||
else:
|
|
||||||
use_tor = False
|
|
||||||
|
|
||||||
if accounts.add_account(fields['username'][0], fields['password'][0], save_account, use_tor ):
|
|
||||||
start_response('200 OK', () )
|
|
||||||
return b'Account successfully added'
|
|
||||||
else:
|
|
||||||
start_response('200 OK', () )
|
|
||||||
return b'Failed to add account'
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
start_response('404 Not Found', ())
|
start_response('404 Not Found', ())
|
||||||
return b'404 Not Found'
|
return b'404 Not Found'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user