forked from jachinlin/geektime_dl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dailylesson & 'all' args (jachinlin#51)
* upate readme * support 'all' argument for all clis * add dailylesson cli * update version to 1.0.0
- Loading branch information
Showing
11 changed files
with
222 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,110 @@ | ||
# coding=utf8 | ||
|
||
from geektime_dl.cli import Command | ||
import os | ||
import sys | ||
|
||
from termcolor import colored | ||
|
||
from geektime_dl.data_client.gk_apis import GkApiError | ||
from geektime_dl.utils.ebook import Render | ||
from geektime_dl.utils.m3u8_downloader import Downloader | ||
from geektime_dl.cli import Command, add_argument | ||
|
||
|
||
class Daily(Command): | ||
"""保存每日一课视频 | ||
geektime daily -v <video_id> [--url-only] [--hd-only] \ | ||
[--output-folder=<output_folder>] | ||
"""保存每日一课视频""""" | ||
|
||
def get_all_course_ids(self, dc, type_: str): | ||
cid_list = [] | ||
data = dc.get_video_collection_list() | ||
for c in data: | ||
cid_list.append(int(c['collection_id'])) | ||
|
||
return cid_list | ||
|
||
@add_argument("collection_ids", type=str, | ||
help="specify the target video collection ids") | ||
@add_argument("--url-only", dest="url_only", action='store_true', | ||
default=False, help="download mp3/mp4 url only") | ||
@add_argument("--hd-only", dest="hd_only", action='store_true', | ||
default=False, help="download mp4 with high quality") | ||
@add_argument("--workers", dest="workers", type=int, save=True, | ||
help="specify the number of threads to download mp3/mp4") | ||
def run(self, cfg: dict): | ||
|
||
dc = self.get_data_client(cfg) | ||
collection_ids = self.parse_course_ids(cfg['collection_ids'], dc) | ||
output_folder = self._format_output_folder(cfg) | ||
|
||
dl = Downloader(output_folder, workers=cfg['workers']) | ||
|
||
for collection_id in collection_ids: | ||
try: | ||
course_data = dc.get_video_collection_intro(collection_id) | ||
except GkApiError as e: | ||
sys.stderr.write('{}\n\n'.format(e)) | ||
continue | ||
|
||
out_dir = os.path.join( | ||
output_folder, | ||
Render.format_file_name(course_data['title'])) | ||
if not os.path.isdir(out_dir): | ||
os.makedirs(out_dir) | ||
|
||
# fetch raw data | ||
print(colored('开始下载视频:{}-{}'.format( | ||
collection_id, course_data['title']), 'green')) | ||
pbar_desc = '数据爬取中:{}'.format(course_data['title'][:10]) | ||
data = dc.get_video_collection_content( | ||
collection_id, pbar_desc=pbar_desc) | ||
|
||
# save url | ||
if cfg['url_only']: | ||
self._parse_and_save_url(course_data, data, out_dir) | ||
continue | ||
|
||
# download mp4 | ||
for post in data: | ||
fn = (Render.format_file_name(post['article_title']) + | ||
('.hd' if cfg['hd_only'] else '.sd')) | ||
if os.path.isfile(os.path.join(out_dir, fn) + '.ts'): | ||
sys.stdout.write(fn + ' exists\n') | ||
continue | ||
url = self._parse_url(post, cfg['hd_only']) | ||
if url: | ||
dl.run(url, os.path.join(out_dir, fn)) | ||
dl.shutdown() | ||
|
||
@staticmethod | ||
def _format_output_folder(cfg): | ||
output_folder = os.path.join(cfg['output_folder'], 'dailylesson') | ||
output_folder = os.path.expanduser(output_folder) | ||
if not os.path.isdir(output_folder): | ||
os.makedirs(output_folder) | ||
return output_folder | ||
|
||
@staticmethod | ||
def _parse_and_save_url(course_intro, course_data, out_dir): | ||
title = Render.format_file_name(course_intro['title']) | ||
fn = os.path.join(out_dir, '{}.mp4.txt'.format(title)) | ||
with open(fn, 'w') as f: | ||
f.write('\n'.join(["{}:\n{}\n{}\n\n".format( | ||
Render.format_file_name(post['article_title']), | ||
(post.get('video_media_map') or {}).get('hd', {}).get('url'), | ||
(post.get('video_media_map') or {}).get('sd', {}).get('url') | ||
) for post in course_data])) | ||
|
||
sys.stdout.write('视频链接下载完成:{}\n\n'.format(fn)) | ||
|
||
`[]`表示可选,`<>`表示相应变量值 | ||
@staticmethod | ||
def _parse_url(post_content: dict, hd_only: bool): | ||
|
||
--url-only: 只保存视频url | ||
--hd-only:下载高清视频,默认下载标清视频 | ||
output_folder: 视频存放目录,默认当前目录 | ||
""" | ||
def run(self, cfg: dict): # noqa: C901 | ||
if hd_only: # some post has sd mp4 only | ||
url = ((post_content.get('video_media_map') or {}).get( | ||
'hd', {}).get('url') or post_content['video_media'].get( | ||
'sd', {}).get('url')) | ||
else: | ||
url = (post_content.get('video_media_map') or {}).get( | ||
'sd', {}).get('url') | ||
|
||
pass | ||
return url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.