From 653c98af7ece18b89546dd2544ce6c72e57d3c10 Mon Sep 17 00:00:00 2001 From: Redwid Date: Mon, 21 May 2018 08:45:25 +0100 Subject: [PATCH 1/2] Modified code to work on android, removed sys.exit() code (it force exit android application as well), write result as json string into file, added new entry point: main.py. --- test/helper.py | 5 +---- youtube_dl/YoutubeDL.py | 16 ++++++-------- youtube_dl/__init__.py | 23 +++++++++++++++----- youtube_dl/compat.py | 2 +- youtube_dl/downloader/common.py | 2 +- youtube_dl/extractor/common.py | 5 +---- youtube_dl/main.py | 20 +++++++++++++++++ youtube_dl/utils.py | 38 +++++++++++++++++---------------- 8 files changed, 69 insertions(+), 42 deletions(-) create mode 100644 youtube_dl/main.py diff --git a/test/helper.py b/test/helper.py index dfee217a9..36ce9856d 100644 --- a/test/helper.py +++ b/test/helper.py @@ -50,10 +50,7 @@ def report_warning(message): Print the message to stderr, it will be prefixed with 'WARNING:' If stderr is a tty file the 'WARNING:' will be colored ''' - if sys.stderr.isatty() and compat_os_name != 'nt': - _msg_header = '\033[0;33mWARNING:\033[0m' - else: - _msg_header = 'WARNING:' + _msg_header = 'WARNING:' output = '%s %s\n' % (_msg_header, message) if 'b' in getattr(sys.stderr, 'mode', '') or sys.version_info[0] < 3: output = output.encode(preferredencoding()) diff --git a/youtube_dl/YoutubeDL.py b/youtube_dl/YoutubeDL.py index 046e03247..a342e6b81 100755 --- a/youtube_dl/YoutubeDL.py +++ b/youtube_dl/YoutubeDL.py @@ -600,10 +600,7 @@ class YoutubeDL(object): else: if self.params.get('no_warnings'): return - if not self.params.get('no_color') and self._err_file.isatty() and compat_os_name != 'nt': - _msg_header = '\033[0;33mWARNING:\033[0m' - else: - _msg_header = 'WARNING:' + _msg_header = 'WARNING:' warning_message = '%s %s' % (_msg_header, message) self.to_stderr(warning_message) @@ -612,10 +609,7 @@ class YoutubeDL(object): Do the same as trouble, but prefixes the message with 'ERROR:', colored in red if stderr is a tty file. ''' - if not self.params.get('no_color') and self._err_file.isatty() and compat_os_name != 'nt': - _msg_header = '\033[0;31mERROR:\033[0m' - else: - _msg_header = 'ERROR:' + _msg_header = 'ERROR:' error_message = '%s %s' % (_msg_header, message) self.trouble(error_message, tb) @@ -1735,7 +1729,11 @@ class YoutubeDL(object): if self.params.get('forceformat', False): self.to_stdout(info_dict['format']) if self.params.get('forcejson', False): - self.to_stdout(json.dumps(info_dict)) + #self.to_stdout(json.dumps(info_dict)) + self.to_stdout('Writing json data to youtube_dl.json') + with open('youtube_dl.json', 'w') as f: + f.write(json.dumps(info_dict).encode('utf-8')) + f.close() # Do nothing else if in simulate mode if self.params.get('simulate', False): diff --git a/youtube_dl/__init__.py b/youtube_dl/__init__.py index ba435ea42..ad8d06d6e 100644 --- a/youtube_dl/__init__.py +++ b/youtube_dl/__init__.py @@ -10,6 +10,7 @@ import io import os import random import sys +import json from .options import ( @@ -464,18 +465,30 @@ def _real_main(argv=None): ydl.to_screen('--max-download limit reached, aborting.') retcode = 101 - sys.exit(retcode) + #sys.exit(retcode) + return +def reportDone(result): + data = {} + data['result'] = result + + f = open('youtube_dl.done', 'w') + f.write(json.dumps(data)) + f.close() + def main(argv=None): try: _real_main(argv) + reportDone('OK') except DownloadError: - sys.exit(1) + reportDone('DownloadError') + #sys.exit(1) except SameFileError: - sys.exit('ERROR: fixed output name but more than one file to download') + reportDone('ERROR: fixed output name but more than one file to download') + #sys.exit('ERROR: fixed output name but more than one file to download') except KeyboardInterrupt: - sys.exit('\nERROR: Interrupted by user') - + reportDone('ERROR: Interrupted by user') + #sys.exit('\nERROR: Interrupted by user') __all__ = ['main', 'YoutubeDL', 'gen_extractors', 'list_extractors'] diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index 4a611f183..4bb8e8e2e 100644 --- a/youtube_dl/compat.py +++ b/youtube_dl/compat.py @@ -4,7 +4,7 @@ from __future__ import unicode_literals import base64 import binascii import collections -import ctypes +#import ctypes import email import getpass import io diff --git a/youtube_dl/downloader/common.py b/youtube_dl/downloader/common.py index edd125ee2..dc82e9596 100644 --- a/youtube_dl/downloader/common.py +++ b/youtube_dl/downloader/common.py @@ -240,7 +240,7 @@ class FileDownloader(object): self._report_progress_prev_line_length = len(fullmsg) clear_line = '\r' else: - clear_line = ('\r\x1b[K' if sys.stderr.isatty() else '\r') + clear_line = ('\r\x1b[K\r') self.to_screen(clear_line + fullmsg, skip_eol=not is_last_line) self.to_console_title('youtube-dl ' + msg) diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py index 3ef5af13c..e3e1dbd87 100644 --- a/youtube_dl/extractor/common.py +++ b/youtube_dl/extractor/common.py @@ -866,10 +866,7 @@ class InfoExtractor(object): if mobj: break - if not self._downloader.params.get('no_color') and compat_os_name != 'nt' and sys.stderr.isatty(): - _name = '\033[0;34m%s\033[0m' % name - else: - _name = name + _name = name if mobj: if group is None: diff --git a/youtube_dl/main.py b/youtube_dl/main.py new file mode 100644 index 000000000..03ba06a46 --- /dev/null +++ b/youtube_dl/main.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +from __future__ import unicode_literals + +# Execute with +# $ python youtube_dl/__main__.py (2.6+) +# $ python -m youtube_dl (2.7+) + +import sys + +if __package__ is None and not hasattr(sys, 'frozen'): + # direct call of __main__.py + import os.path + path = os.path.realpath(os.path.abspath(__file__)) + sys.path.insert(0, os.path.dirname(os.path.dirname(path))) + +import youtube_dl + +if __name__ == '__main__': + youtube_dl.main() + diff --git a/youtube_dl/utils.py b/youtube_dl/utils.py index f9ca63c58..608226578 100644 --- a/youtube_dl/utils.py +++ b/youtube_dl/utils.py @@ -8,7 +8,7 @@ import binascii import calendar import codecs import contextlib -import ctypes +#import ctypes import datetime import email.utils import email.header @@ -1183,6 +1183,8 @@ def unified_strdate(date_str, day_first=True): upload_date = datetime.datetime.strptime(date_str, expression).strftime('%Y%m%d') except ValueError: pass + except TypeError: + pass if upload_date is None: timetuple = email.utils.parsedate_tz(date_str) if timetuple: @@ -1753,23 +1755,23 @@ def setproctitle(title): if sys.platform.startswith('java'): return - try: - libc = ctypes.cdll.LoadLibrary('libc.so.6') - except OSError: - return - except TypeError: - # LoadLibrary in Windows Python 2.7.13 only expects - # a bytestring, but since unicode_literals turns - # every string into a unicode string, it fails. - return - title_bytes = title.encode('utf-8') - buf = ctypes.create_string_buffer(len(title_bytes)) - buf.value = title_bytes - try: - libc.prctl(15, buf, 0, 0, 0) - except AttributeError: - return # Strange libc, just skip this - + #try: + # libc = ctypes.cdll.LoadLibrary('libc.so.6') + #except OSError: + # return + #except TypeError: + # # LoadLibrary in Windows Python 2.7.13 only expects + # # a bytestring, but since unicode_literals turns + # # every string into a unicode string, it fails. + # return + #title_bytes = title.encode('utf-8') + #buf = ctypes.create_string_buffer(len(title_bytes)) + #buf.value = title_bytes + #try: + # libc.prctl(15, buf, 0, 0, 0) + #except AttributeError: + # return # Strange libc, just skip this + return def remove_start(s, start): return s[len(start):] if s is not None and s.startswith(start) else s From 6c0b521a36ac6784bc4519c6d3c489c06451e038 Mon Sep 17 00:00:00 2001 From: Redwid Date: Mon, 20 Aug 2018 09:55:35 +0100 Subject: [PATCH 2/2] Updated README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d9fe2350a..dbdea055f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ [![Build Status](https://travis-ci.org/rg3/youtube-dl.svg?branch=master)](https://travis-ci.org/rg3/youtube-dl) youtube-dl - download videos from youtube.com or other video platforms +Forked version that could work on android - [INSTALLATION](#installation) - [DESCRIPTION](#description)