From 04937f7c64652ba3c1ecc29a920d6563f8f6786c Mon Sep 17 00:00:00 2001 From: joyfuI Date: Mon, 7 Dec 2020 23:59:36 +0900 Subject: [PATCH] =?UTF-8?q?download=20API=EC=97=90=20cookiefile,=20headers?= =?UTF-8?q?=20=ED=82=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit download API에 cookiefile, headers 키 추가 (Thanks to soju6jan) --- README.md | 10 +++++++++- info.json | 2 +- logic_normal.py | 7 ++++--- my_youtube_dl.py | 16 +++++----------- plugin.py | 17 ++++++----------- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2a4a880..89c89e1 100644 --- a/README.md +++ b/README.md @@ -74,8 +74,11 @@ API를 제공합니다. 다른 플러그인에서 동영상 정보나 다운로 `dateafter` | 지정한 날짜 이후에 업로드된 동영상만 다운로드. 미지정 시 모든 동영상 다운로드 | X | String `archive` | 다운로드한 동영상의 ID를 기록할 파일 경로. 파일이 이미 있으면 이미 다운로드한 동영상은 다운로드 하지 않음. 미지정 시 기록하지 않음 | X | String `start` | 다운로드 준비 후 바로 다운로드를 시작할지 여부. 기본값: `false` | X | Boolean +`cookiefile` | 다운로드 시 필요한 쿠키 파일 경로 | X | String +`headers` | 다운로드 시 사용할 헤더. 기본값: `{}` | X | String -`dateafter` 키에 넣을 수 있는 날짜는 `YYYYMMDD` 또는 `(now|today)[+-][0-9](day|week|month|year)(s)?` 형식의 문자열입니다. +`dateafter` 키에 넣을 수 있는 날짜는 `YYYYMMDD` 또는 `(now|today)[+-][0-9](day|week|month|year)(s)?` 형식의 문자열입니다. +`headers` 키에 넣는 값은 `json` 형식의 문자열입니다. #### Response 키 | 설명 | 타입 --- | --- | --- @@ -132,6 +135,11 @@ API를 제공합니다. 다른 플러그인에서 동영상 정보나 다운로 물론 해당 정보가 없으면 null입니다. ## Changelog +v2.1.0 +* download API에 cookiefile, headers 키 추가 + 카카오TV 동영상 다운로드에 활용할 수 있습니다. 잘하면 로봇 체크 패스에도 사용이 가능한 듯 싶습니다. + Thanks to [soju6jan](https://github.com/soju6jan) + v2.0.0 * youtube-dlc 추가 사용할 youtube-dl 패키지를 선택할 수 있습니다. 설정 변경 후 재시작해야 적용됩니다. diff --git a/info.json b/info.json index 21833e4..fa542f4 100644 --- a/info.json +++ b/info.json @@ -1 +1 @@ -{"description": "\uc720\ud29c\ube0c, \ub124\uc774\ubc84TV \ub4f1 \ub3d9\uc601\uc0c1 \uc0ac\uc774\ud2b8\uc5d0\uc11c \ub3d9\uc601\uc0c1 \ub2e4\uc6b4\ub85c\ub4dc", "name": "youtube-dl", "more": "", "version": "2.0.0", "home": "https://github.com/joyfuI/youtube-dl", "category_name": "vod", "developer": "joyfuI"} \ No newline at end of file +{"description": "\uc720\ud29c\ube0c, \ub124\uc774\ubc84TV \ub4f1 \ub3d9\uc601\uc0c1 \uc0ac\uc774\ud2b8\uc5d0\uc11c \ub3d9\uc601\uc0c1 \ub2e4\uc6b4\ub85c\ub4dc", "name": "youtube-dl", "more": "", "version": "2.1.0", "home": "https://github.com/joyfuI/youtube-dl", "category_name": "vod", "developer": "joyfuI"} \ No newline at end of file diff --git a/logic_normal.py b/logic_normal.py index 3782ab2..6483768 100644 --- a/logic_normal.py +++ b/logic_normal.py @@ -110,10 +110,11 @@ class LogicNormal(object): opts['proxy'] = kwagrs['proxy'] if 'ffmpeg_path' in kwagrs and kwagrs['ffmpeg_path']: opts['ffmpeg_location'] = kwagrs['ffmpeg_path'] + if 'cookiefile' in kwagrs and kwagrs['cookiefile']: + opts['cookiefile'] = kwagrs['cookiefile'] dateafter = kwagrs.get('dateafter') - # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 - youtube_dl = MyYoutubeDL(plugin, url, filename, temp_path, save_path, opts, dateafter, headers=kwagrs['headers'], cookiefile=kwagrs['cookiefile']) - # by soju6jan + headers = kwagrs.get('headers') + youtube_dl = MyYoutubeDL(plugin, url, filename, temp_path, save_path, opts, dateafter, headers) youtube_dl.key = kwagrs.get('key') LogicNormal.youtube_dl_list.append(youtube_dl) # 리스트 추가 return youtube_dl diff --git a/my_youtube_dl.py b/my_youtube_dl.py index 07d4652..4f230e4 100644 --- a/my_youtube_dl.py +++ b/my_youtube_dl.py @@ -45,7 +45,7 @@ class MyYoutubeDL(object): __index = 0 _last_msg = '' - def __init__(self, plugin, url, filename, temp_path, save_path=None, opts=None, dateafter=None, datebefore=None, headers=None, cookiefile=None): + def __init__(self, plugin, url, filename, temp_path, save_path=None, opts=None, dateafter=None, datebefore=None, headers={}): # from youtube_dl.utils import DateRange from .plugin import YOUTUBE_DL_PACKAGE DateRange = __import__('%s.utils' % YOUTUBE_DL_PACKAGE, fromlist=['DateRange']).DateRange @@ -66,6 +66,7 @@ class MyYoutubeDL(object): self.opts = opts if dateafter or datebefore: self.opts['daterange'] = DateRange(start=dateafter, end=datebefore) + self.headers = headers self.index = MyYoutubeDL.__index MyYoutubeDL.__index += 1 self.__status = Status.READY @@ -92,9 +93,6 @@ class MyYoutubeDL(object): 'eta': None, # 예상 시간(s) 'speed': None # 다운로드 속도(bytes/s) } - # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 - self.headers = headers - self.cookiefile = cookiefile def start(self): if self.status != Status.READY: @@ -112,12 +110,10 @@ class MyYoutubeDL(object): try: self.start_time = datetime.now() self.status = Status.START - # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 - # headers는 전역으로 계속 사용하기 때문에 매번 세팅. - youtube_dl_utils = __import__('%s.utils' % YOUTUBE_DL_PACKAGE) - youtube_dl_utils.std_headers = {} if self.headers is None else self.headers + # headers는 전역으로 계속 사용하기 때문에 매번 세팅 + youtube_dl.utils.std_headers = self.headers # 동영상 정보 가져오기 - info_dict = MyYoutubeDL.get_info_dict(self.url, self.opts.get('proxy'), cookiefile=self.cookiefile) + info_dict = MyYoutubeDL.get_info_dict(self.url, self.opts.get('proxy'), self.opts.get('cookiefile')) if info_dict is None: self.status = Status.ERROR return @@ -133,8 +129,6 @@ class MyYoutubeDL(object): 'ignoreerrors': True, 'cachedir': False } - if self.cookiefile: - ydl_opts['cookiefile'] = self.cookiefile ydl_opts.update(self.opts) with youtube_dl.YoutubeDL(ydl_opts) as ydl: ydl.download([self.url]) diff --git a/plugin.py b/plugin.py index cf5e3b5..8124958 100644 --- a/plugin.py +++ b/plugin.py @@ -5,6 +5,7 @@ import os import sys import traceback import subprocess +import json # third-party from flask import Blueprint, request, render_template, redirect, jsonify, abort @@ -44,7 +45,7 @@ menu = { } plugin_info = { - 'version': '2.0.0', + 'version': '2.1.0', 'name': 'youtube-dl', 'category_name': 'vod', 'developer': 'joyfuI', @@ -209,14 +210,8 @@ def api(sub): dateafter = request.values.get('dateafter', None) archive = request.values.get('archive', None) start = request.values.get('start', False) - # 2020-12-06 by soju6jan. api로 headers, cookiefile 전달 - headers = None - tmp_headers = request.values.get('headers', None) - if tmp_headers is not None: - import json - headers = json.loads(tmp_headers) # header는 json.dumps로 넘어오는 것으로 함. unqoute 등을 해야하는지 고려해야함. cookiefile = request.values.get('cookiefile', None) - # by soju6jan + headers = request.values.get('headers', '{}') ret = { 'errorCode': 0, 'index': None @@ -243,9 +238,9 @@ def api(sub): proxy=ModelSetting.get('proxy'), ffmpeg_path=ModelSetting.get('ffmpeg_path'), key=key, - # 2020-12-06 by soju6jan. - headers=headers, - cookiefile=cookiefile) + cookiefile=cookiefile, + # header는 json.dumps로 넘어오는 것으로 함. unqoute 등을 해야하는지 고려해야 함 + headers=json.loads(headers)) if youtube_dl is None: return LogicNormal.abort(ret, 10) # 실패 ret['index'] = youtube_dl.index