Use channel id to uniquely identify accounts, not username

This commit is contained in:
James Taylor 2018-12-30 23:27:51 -08:00
parent 248c2e1415
commit 913c258093
2 changed files with 11 additions and 9 deletions

View File

@ -16,11 +16,12 @@ except FileNotFoundError:
# global var for temporary storage of account info # global var for temporary storage of account info
accounts = {} accounts = {}
def username_list(): def account_list_data():
return accounts.keys() '''Returns iterable of (channel_id, account_display_name)'''
return ( (channel_id, account['display_name']) for channel_id, account in accounts.items() )
def save_accounts(): def save_accounts():
to_save = {username: account for username, account in accounts.items() if account['save']} to_save = {channel_id: account for channel_id, account in accounts.items() if account['save']}
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))
@ -28,9 +29,10 @@ 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):
accounts[username] = { accounts[result["channel_id"]] = {
"save":save, "save":save,
"channel_id": result["channel_id"], "username": username,
"display_name": username,
"cookies":cookiejar.as_lwp_str(ignore_discard=False, ignore_expires=False).split('\n'), "cookies":cookiejar.as_lwp_str(ignore_discard=False, ignore_expires=False).split('\n'),
} }
if save: if save:
@ -46,8 +48,8 @@ def cookiejar_from_lwp_str(lwp_str):
cookiejar._really_load(io.StringIO(lwp_str), "", False, False) cookiejar._really_load(io.StringIO(lwp_str), "", False, False)
return cookiejar return cookiejar
def account_cookiejar(username): def account_cookiejar(channel_id):
return cookiejar_from_lwp_str('\n'.join(accounts[username]['cookies'])) return cookiejar_from_lwp_str('\n'.join(accounts[channel_id]['cookies']))
def get_account_login_page(query_string): def get_account_login_page(query_string):
style = ''' style = '''

View File

@ -312,10 +312,10 @@ video_metadata_template = Template('''<section class="video-metadata">
</section> </section>
''') ''')
account_option_template = Template(''' account_option_template = Template('''
<option value="$username">$username</option>''') <option value="$channel_id">$display_name</option>''')
def comment_box_account_options(): def comment_box_account_options():
return ''.join(account_option_template.substitute(username=username) for username in accounts.username_list()) return ''.join(account_option_template.substitute(channel_id=channel_id, display_name=display_name) for channel_id, display_name in accounts.account_list_data())
comment_box_template = Template(''' comment_box_template = Template('''
<form action="$form_action" method="post" class="comment-form"> <form action="$form_action" method="post" class="comment-form">