Skip to content

Commit 5bfefe2

Browse files
committed
Drop browser_cookie3 and use rookiepy instead
1 parent d5de6b1 commit 5bfefe2

2 files changed

Lines changed: 43 additions & 23 deletions

File tree

nico.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from urllib.request import getproxies
88
from pathlib import Path
99

10-
import browser_cookie3
1110
import websocket
1211
from rich.console import Console
1312
from rich.table import Table
@@ -28,18 +27,10 @@ def validate_cookie(cookies):
2827
else:
2928
print(f'WARN: Cannot find user_session in cookie. You\'re probably not logged in.')
3029
# exit(1) # make it non-fatal
31-
32-
if cookies.lower() in ['chrome', 'firefox', 'edge']:
33-
print(f'Fetching cookies from browser {cookies}...')
34-
cookies = cookies.lower()
35-
if cookies == 'chrome':
36-
cookies = browser_cookie3.chrome(domain_name='.nicovideo.jp')
37-
elif cookies == 'firefox':
38-
cookies = browser_cookie3.firefox(domain_name='.nicovideo.jp')
39-
elif cookies == 'edge':
40-
cookies = browser_cookie3.edge(domain_name='.nicovideo.jp')
30+
if isinstance(cookies, str) and cookies.lower() in ['chrome', 'firefox', 'edge']:
31+
cookies = load_cookie(cookies + '/nicovideo.jp')
4132
validate_cookie(cookies)
42-
elif cookies.startswith('user_session_'):
33+
elif isinstance(cookies, str) and cookies.startswith('user_session_'):
4334
cookies = {'user_session': cookies}
4435
elif Path(cookies).exists():
4536
print(f'Loading cookies from file {cookies}...')
@@ -59,10 +50,10 @@ def validate_cookie(cookies):
5950
elif proxy == 'auto':
6051
proxies = getproxies()
6152
if proxy := proxies.get('http'):
62-
print(f'INFO: automatically use system proxy {proxy}')
63-
53+
print(f'INFO: Automatically use system proxy {proxy}')
6454
# I don't think system proxy would be missing scheme, but just in case
6555
if proxy and '://' not in proxy:
56+
print('WARN: Proxy is missing scheme. Assuming http://')
6657
proxy = f'http://{proxy}'
6758

6859
self.session.proxies = {'http': proxy, 'https': proxy}
@@ -253,7 +244,8 @@ def download_timeshift(self, url_or_video_id, info_only=False, comments='yes', v
253244
room_info = None
254245
stream_info = None
255246

256-
ex = concurrent.futures.ThreadPoolExecutor(max_workers=1)
247+
#TODO: fix danmaku downloading with the new method
248+
# ex = concurrent.futures.ThreadPoolExecutor(max_workers=1)
257249

258250
while True:
259251
verbose and print("Receiving...")
@@ -263,6 +255,7 @@ def download_timeshift(self, url_or_video_id, info_only=False, comments='yes', v
263255
if data['type'] == 'stream':
264256
stream_info = data
265257
break
258+
#TODO: danmaku downloading is temporarily disabled until the new method is implemented
266259
# if data['type'] == 'room':
267260
# room_info = data
268261
# if comments in ['yes', 'only']:
@@ -315,7 +308,8 @@ def download_timeshift(self, url_or_video_id, info_only=False, comments='yes', v
315308
print('CMD is:')
316309
print(cmd)
317310
run(cmd, shell=True)
318-
ex.shutdown(wait=True) # ensure download_comments is finished
311+
# TODO: fix danmaku downloading with the new method
312+
# ex.shutdown(wait=True) # ensure download_comments is finished
319313

320314
return_value.update({
321315
'master_m3u8_url': master_m3u8_url,

util.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# so we can use this file in other projects without installing them.
1515
# or to copy and paste the functions to other public projects directly.
1616
# to install them all, you can do:
17-
# pip install requests lxml beautifulsoup4 python-dateutil pytz pyperclip wcwidth rich browser_cookie3
17+
# pip install requests lxml beautifulsoup4 python-dateutil pytz pyperclip wcwidth rich rookiepy
1818

1919
# ==================== CONSTANTS ====================
2020

@@ -248,6 +248,23 @@ def save(self, f):
248248
s += '\t'.join([str(cell) for cell in row]) + '\n'
249249
f.write_text(s, encoding='utf8')
250250

251+
def multi_col_print(data, columns=5):
252+
'''Print a list of data in a table with columns'''
253+
from rich.console import Console
254+
from rich.table import Table
255+
256+
table = Table(show_header=False, box=None)
257+
for _ in range(columns):
258+
table.add_column(style='cyan')
259+
# add data to table vertically
260+
row_count = len(data) // columns + 1
261+
for i in range(row_count):
262+
row = data[i::row_count]
263+
table.add_row(*row)
264+
265+
console = Console()
266+
console.print(table)
267+
251268
def array_to_range_text(a, sep=', ', dash='-'):
252269
s = ''
253270
prev_seg = None
@@ -815,7 +832,8 @@ def load_cookie(s):
815832
from http.cookiejar import MozillaCookieJar
816833
from requests.cookies import RequestsCookieJar, create_cookie
817834
import browser_cookie3
818-
# pip install browser_cookie3
835+
import rookiepy
836+
# pip install browser_cookie3 rookiepy
819837

820838
def convert(cj):
821839
cookies = RequestsCookieJar()
@@ -833,14 +851,22 @@ def convert(cj):
833851
return cookies
834852

835853
if m := re.search(r'^(chrome|firefox|edge)(/.+)?', str(s), re.IGNORECASE):
836-
domain_name = m[2].lstrip('/') if m[2] else ""
854+
# domain_name = m[2].lstrip('/') if m[2] else None
855+
# if m[1] == 'chrome':
856+
# cj = browser_cookie3.chrome(domain_name=domain_name)
857+
# elif m[1] == 'firefox':
858+
# cj = browser_cookie3.firefox(domain_name=domain_name)
859+
# elif m[1] == 'edge':
860+
# cj = browser_cookie3.edge(domain_name=domain_name)
861+
# return convert(cj)
862+
domains = [m[2].lstrip('/')] if m[2] else None
837863
if m[1] == 'chrome':
838-
cj = browser_cookie3.chrome(domain_name=domain_name)
864+
rcookies = rookiepy.chrome(domains=domains)
839865
elif m[1] == 'firefox':
840-
cj = browser_cookie3.firefox(domain_name=domain_name)
866+
rcookies = rookiepy.firefox(domains=domains)
841867
elif m[1] == 'edge':
842-
cj = browser_cookie3.edge(domain_name=domain_name)
843-
return convert(cj)
868+
rcookies = rookiepy.edge(domains=domains)
869+
return convert(rookiepy.to_cookiejar(rcookies))
844870

845871
if Path(s).exists():
846872
cj = MozillaCookieJar(s)

0 commit comments

Comments
 (0)