Skip to content

Commit 25f7986

Browse files
committed
nico: remove default cookie option, add a way to set settings in nico.txt
1 parent ad08519 commit 25f7986

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ options:
155155
--dump Dump all the metadata to json files.
156156
--thumb Download thumbnail only. Only works for video type (not live type).
157157
--cookies COOKIES, -c COOKIES
158-
Cookie source. [Default: chrome]
158+
Cookie source.
159159
Provide either:
160160
- A browser name to fetch from;
161161
- The value of "user_session";

nico.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class NicoDownloader():
2121
def __init__(self, cookies, proxy=None, save_dir=None):
2222
def validate_cookie(cookies):
2323
for cookie in cookies:
24-
if cookie.name == 'user_session':
24+
if cookie.name == 'user_session' and cookie.value:
2525
print('Find user_session in cookie:', cookie.value)
2626
break
2727
else:
@@ -112,7 +112,7 @@ async def download(video_id, output, cookies, verbose=False):
112112
cookies = self.session.cookies.get_dict()
113113
asyncio.run(download(video_id, output, cookies))
114114

115-
def download_timeshift(self, url_or_video_id, info_only=False, comments='yes', verbose=False, dump=False, auto_reserve=False):
115+
def download_timeshift(self, url_or_video_id, info_only=False, comments='no', verbose=False, dump=False, auto_reserve=False):
116116
video_id, url, video_type = self._parse_url_or_video_id(url_or_video_id)
117117

118118
return_value = {
@@ -316,8 +316,17 @@ def download_thumbnail(self, url_or_video_id, info_only=False, dump=False):
316316
download(thumbnail_url, filename=self.save_dir / video_id)
317317

318318
if __name__ == "__main__":
319+
import shlex
320+
import sys
319321
import argparse
320322

323+
# auto load arguments from nico.txt
324+
for f in [Path(__file__).parent / 'nico.txt', Path('nico.txt')]:
325+
if f.exists():
326+
with open(f, encoding='utf8') as f:
327+
sys.argv.extend(shlex.split(f.read().replace('\\', '\\\\')))
328+
break
329+
321330
# https://stackoverflow.com/questions/3853722/how-to-insert-newlines-on-argparse-help-text
322331
class SmartFormatter(argparse.HelpFormatter):
323332
def _split_lines(self, text, width):
@@ -331,7 +340,7 @@ def _split_lines(self, text, width):
331340
parser.add_argument('--info', '-i', action='store_true', help='Print info only.')
332341
parser.add_argument('--dump', action='store_true', help='Dump all the metadata to json files.')
333342
parser.add_argument('--thumb', action='store_true', help='Download thumbnail only. Only works for video type (not live type).')
334-
parser.add_argument('--cookies', '-c', default='chrome', help='R|Cookie source. [Default: chrome]\nProvide either:\n - A browser name to fetch from;\n - The value of "user_session";\n - A Netscape-style cookie file.')
343+
parser.add_argument('--cookies', '-c', help='R|Cookie source.\nProvide either:\n - A browser name to fetch from;\n - The value of "user_session";\n - A Netscape-style cookie file.')
335344
parser.add_argument('--comments', '-d', default='no', choices=['yes', 'no', 'only'], help='Control if comments (danmaku) are downloaded. [Default: no]')
336345
parser.add_argument('--proxy', default='auto', help='Specify a proxy, "none", or "auto" (automatically detects system proxy settings). [Default: auto]')
337346
parser.add_argument('--save-dir', '-o', help='Specify the directory to save the downloaded files. [Default: current directory]')

0 commit comments

Comments
 (0)