Example #1
0
def main():
    with redirect_stdout(stdout_capture_streaming):
        yt_uri = "8cOJhLM66D4"
        yt_country = "FR"
        argv = f"-f best --no-part --no-cache-dir -o /dev/null --newline --geo-bypass-country {yt_country} https://youtu.be/{yt_uri}"

        youtube_dl.main(argv.split())
Example #2
0
def setup_debug3():
    try:
        youtube_dl.main()
    except:
        wf(stackprinter.format(sys.exc_info()), 'logs/error.except.main3.log',
           'a')
        raise
Example #3
0
   def run(self):
      self.download_status_bar.set_status(Status.PROCESSING)
      self.download_status_bar.append_log('Start of `' + self.name + '`\n')

      #Redirect ytdl stdout to a string FIXME android: redirect output somehow prevent downloads
      if platform != 'android':
         sys_stdout = sys.stdout
         str_stdout = StringIO()
         sys.stdout = str_stdout

      try:
         youtube_dl.main(self.ytdl_args)
      except SystemExit:    #ignore ytdl calls to sys.exit()
         pass
      except Exception:     #ignore exception 'str' object has no attribute 'write'
         pass

      if platform != 'android':
         #redirect back stedout to system stdout
         sys.stdout = sys_stdout
         log = str_stdout.getvalue()    #TODO: get output every n seconds instead of waiting end of youtube_dl.main
         self.download_status_bar.append_log(log)

      self.download_status_bar.append_log('End of `' + self.name + '`\n')
      self.download_status_bar.set_status(Status.DONE)
Example #4
0
    def download_entry(self, entry, config):
        """Calls Youtube-Dl with new config and fails the entry on error

        Raises:
            PluginError if operation fails
        """

        # video URLs have to be the last items in the list
        entry_config = list(config['ytdl-config'])
        entry_config.append(entry['url'])

        if entry.task.manager.options.test:
            log.info('Would start Youtube-Dl with "%s"', entry_config)
            return

        log.debug('Starting Youtube-Dl with "%s"', entry_config)
        
        try:
            youtube_dl.main(entry_config)
        except SystemExit as e:
            if e.code != 0:
                entry.fail('Youtube-Dl returned error code: %s' % str(e))
        except Exception as e:
            raise PluginError('Unknown error: %s' % str(e), log)

        return
Example #5
0
def getvid(url):
    _u = urlparse(url)
    # If yt, we can directly get the audio file from the video
    if _u.netloc == 'www.youtube.com':
        write('getvid', 'Is YT', True)
        vidId = _u.query.split('v=')[1]
        os.chdir('temp')
        youtube_dl.main(['-x', vidId])
        os.chdir('..')
    elif _u.netloc == 'youtu.be':
        write('getvid', 'Is YT', True)
        vidId = _u.path[1:]
        os.chdir('temp')
        with youtube_dl.YoutubeDL() as ydl:
            ydl.download([vidId])
        os.chdir('..')
    else:
        # Oh boy
        resp = requests.get(url)
        page = BeautifulSoup(resp.text)
        vids = page.select('video')
        for i in vids:
            src = i.attrs['src']
            if src.startswith('blob:'):
                # Impossible to handle sadly
                pass
            else:
                _s = urlparse(src)
                filename = _s.path.split('/')[-1]
                os.chdir('temp')
                with open(filename, 'w+') as f:
                    f.write(requests.get(src).content)
                extractaudio.extract(filename, filename + '.mp3')
                os.chdir('..')
Example #6
0
def setup():
    import sys
    import os
    sys.path.insert(0, '/Users/alberthan/VSCodeProjects/HDLogger')
    hd_tracer = hdTracer(vars_to_watch)
    oldout, olderr = sys.stdout, sys.stderr
    sys.stdout = newout = io.StringIO()
    sys.stderr = newerr = io.StringIO()
    sys.settrace(hd_tracer.trace_dispatch)
    try:
        youtube_dl.main()
    # except SystemExit as err:
    # wf(stackprinter.format(err), 'logs/system_exit.expected_error.log', 'a')
    # print('--- expected SystemExit: \x1b[1;32mSuccess\x1b[0m')
    except:
        wf(stackprinter.format(sys.exc_info()), 'logs/error.except.main.log',
           'a')
        raise
    finally:
        # hd_tracer.varswatcher.write_var_history()
        output, error = newout.getvalue(), newerr.getvalue()
        print(output)
        wf(output, 'logs/stdout.finally.log', 'a')
        wf(error, 'logs/stderr.finally.log', 'a')
    return (inspect.currentframe(), globals(), locals())
Example #7
0
	def resolve(self, url, resolver):
		if 'plugin://' in url: return
		if not self.started: self.start()
		self.info_dicts = []
		stdout, stderr, exit = sys.stdout, sys.stderr, sys.exit
		# prevent youtube_dl from shutting us down, discard stderr/stdout
		sys.exit = self.noexit
		sys.stderr = sys.stdout = self.devnull
		# TODO: --cookies (https://github.com/rg3/youtube-dl/issues/41)
		#       --max-downloads (overuse triggers '402: Payment Required')
		# v2015.04.26+: explicitly setting '-f best' to avoid new 'merged format' separate a/v urls
		youtube_dl.main(['-s', '--no-playlist', '-f', 'best', url])
		sys.stdout, sys.stderr, sys.exit = stdout, stderr, exit

		if len(self.info_dicts) > 0:
			# get the first result
			i = self.info_dicts[0]
			if i.get('url') is None and i.get('requested_formats') is not None:
				# v 2014.12.10+ (shouldn't happen, but just in case)
				req = info_dict.get('requested_formats')
				if len(req) == 1:
					i = req[0]
				else:
					raise 'Error: multiple "merged format" urls received'
#			from pprint import pprint
#			pprint(i)
			details = {}
			if 'http_headers' in i:
				headers = {k:v for k,v in i['http_headers'].items() if not k.lower().startswith('accept')}
				if headers:
					details['headers'] = headers
			resolver.add(i['url'] + i.get('play_path', ''), i.get('fulltitle'), i.get('thumbnail'),
				details=details if details else None)
Example #8
0
 def run():
     command = (f'youtube-dl '
                f'-f {quality}[ext={ext}]/'
                f'{quality}video+bestaudio/bestaudio '
                f'--merge-output-format "{ext}" '
                f'-o "{path}" '
                f'{url}')
     youtube_dl.main(shlex.split(command)[1:])
Example #9
0
def youtube_dl_main_x(argv=None):
    ytdl_common.sanitize_filename = ytdl_YoutubeDL.sanitize_filename = limit_len_sanitize_filename
    ytdl_common.InfoExtractor = NewInfoExtractor
    youtube_dl.extractor.GenericIE = GenericInfoExtractor
    youtube_dl.extractor.IwaraIE = IwaraIE
    youtube_dl.extractor.PornHubIE = PornHubIE
    youtube_dl.extractor.XVideosIE = XVideosIE
    youtube_dl.main(argv)
 def run_youtube_dl(self, args):
     # this is the only way to use the youtube-dl config file, if you
     # directly pass the args to youtube_dl.main it doesn't read it
     with unittest.mock.patch('sys.argv', ['youtube-dl'] + args):
         try:
             youtube_dl.main()
         except SystemExit as err:
             if err.code != 0:
                 raise YoutubeDLError(err.code)
Example #11
0
def main():
    print '\("`v")/ Start!'
    URL = getBaseURL()
    soup = getSoup(URL)
    vimeoIDs = getVimeoId(soup)

    VIMEO = 'http://vimeo.com/'
    urls = map(lambda x: VIMEO + x, vimeoIDs)

    ytdl_param = ['-t', '-A']
    youtube_dl.main(ytdl_param + urls)
    print '\("`v")/ END!'
def main():
    print '\("`v")/ Start!'
    URL = getBaseURL()
    soup = getSoup(URL)
    vimeoIDs = getVimeoId(soup)

    VIMEO = 'http://vimeo.com/'
    urls = map(lambda x: VIMEO+x,vimeoIDs)

    ytdl_param = ['-t', '-A']
    youtube_dl.main(ytdl_param+urls)
    print '\("`v")/ END!'
Example #13
0
def download():
    print request.form.get("url")
    link = request.form.get("url")
    title = get_title()
    try:
        print os.path.join(UPLOAD_FOLDER,title)
        youtube_dl.main(['-f', '85/84/83/82/38/37/22/18/120/35/34','-o',"/var/www/video/uploads/" + title + '.%(ext)s', link])
    except: # messy hack - this triggers each time, but all seems to work ok. if omitted an exception is thrown
        pass
    title += ".mp4"
    try:
        return send_from_directory(UPLOAD_FOLDER, title, as_attachment=True)
    except:
        return render_template("index.html", failed=True)
Example #14
0
def setup_hunter_call():
    sys.path.insert(0, '/Users/alberthan/VSCodeProjects/HDLogger')
    lines = io.StringIO()
    try:
        with hunter.trace(filename__contains="youtube",
                          action=hunter.CallPrinter(stream=lines)):
            youtube_dl.main()
    except:
        wf(stackprinter.format(sys.exc_info()), 'logs/error.except.main.log',
           'a')
        raise
    finally:
        output = lines.getvalue()
        wf(output, 'logs/huntercall.finally.log', 'a')
    return (inspect.currentframe(), globals(), locals())
Example #15
0
	def resolve(self, url, resolver):
		if 'plugin://' in url: return
		if not self.started: self.start()
		self.info_dicts = []
		stdout, stderr, exit = sys.stdout, sys.stderr, sys.exit
		# prevent youtube_dl from shutting us down, discard stderr/stdout
		sys.exit = self.noexit
		sys.stderr = sys.stdout = self.devnull
		# TODO: --cookies (https://github.com/rg3/youtube-dl/issues/41)
		#       --max-downloads (overuse triggers '402: Payment Required')
		youtube_dl.main(['-s', url])
		sys.stdout, sys.stderr, sys.exit = stdout, stderr, exit
		u = '\n'.join([(i['url'] + i.get('play_path', '')) for i in self.info_dicts])
		if u:
			i = self.info_dicts[0]
			resolver.add(u, i.get('fulltitle'), i.get('thumbnail'))
Example #16
0
async def ytlink(ctx, url: str):
    #queue? make a list!
    song_there = os.path.isfile("song.mp3")
    try:
        if song_there:
            os.remove("song.mp3")
    except Exception:
        await ctx.send("uh.. something went really wrong")
        return

    songtitle = youtube_dl.main(url + '--get-title')
    await ctx.send(songtitle)

    voice = get(client.voice_clients, guild=ctx.guild)
    ydl_opts = {
        'format':
        'bestaudio/best',
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
    }

    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([url])
    for file in os.listdir("./"):
        if file.endswith(".mp3"):
            os.rename(file, 'song.mp3')

    voice.play(discord.FFmpegPCMAudio("song.mp3"))
    voice.volume = 100
    voice.is_playing()
Example #17
0
def downloader(download_queue):
    # importing locally, this takes forever, while script is silent
    import youtube_dl

    while True:
        url = download_queue.get(True)
        pid = os.fork()
        if not pid:
            print('Downloading... ({})'.format(url))
            youtube_dl.main(['--no-check-certificate', url.split()[-1]])
            exit()
        os.wait()
        print('Download finished')
        _a.makeToast('Finished: {}...'.format(url[:10]))
        _a.vibrate(1000)
        os.system('df .')
def downloadUrl(url, args, outputDir=os.path.join(home, "Music/Downloads")):
    ''' Fragment from here
    http://stackoverflow.com/questions/18054500/how-to-use-youtube-dl-from-a-python-programm
    '''
    if not os.path.isdir(outputDir):
        os.makedirs(outputDir)
    outputFile = os.path.join(outputDir, '%(title)s-%(id)s.%(ext)s')
    if "--playlist" not in args:
        opts = ["-k", "-x", "-o", "{}".format(outputFile)
                , "--audio-format", "mp3"
                , "--no-playlist" ] + [ url ]
    else:
        print("[INFO] Downloading playlist from youtube.")
        opts = ["-k", "-x", "-o", "{}".format(outputFile)
                , "--audio-format", "mp3" ] + [ url ]

    youtube_dl.main(opts)
Example #19
0
def setup_debug():
    import sys, os
    sys.path.insert(0, '/Users/alberthan/VSCodeProjects/HDLogger')
    hd_tracer = hdTracer()
    sys.settrace(hd_tracer.trace_dispatch)
    try:
        youtube_dl.main()
    # except SystemExit as err:
    # wf(stackprinter.format(err), 'logs/system_exit.expected_error.log', 'a')
    # print('--- expected SystemExit: \x1b[1;32mSuccess\x1b[0m')
    except:
        wf(stackprinter.format(sys.exc_info()), 'logs/error.except.main.log',
           'a')
        raise
    finally:
        # hd_tracer.varswatcher.write_var_history()
        wf(output, 'logs/setup_debug.finally0.log', 'a')
    return (inspect.currentframe(), globals(), locals())
Example #20
0
def setup_debug2():
    import sys, os
    sys.path.insert(0, '/Users/alberthan/VSCodeProjects/HDLogger')
    hd_tracer = hdTracer()
    oldout, olderr = sys.stdout, sys.stderr
    sys.stdout = newout = io.StringIO()
    sys.stderr = newerr = io.StringIO()
    sys.settrace(hd_tracer.trace_dispatch)
    try:
        youtube_dl.main()
    except:
        wf(stackprinter.format(sys.exc_info()), 'logs/error.except.main.log',
           'a')
        raise
    finally:
        output, error = newout.getvalue(), newerr.getvalue()
        wf(output, 'logs/stdout.finally2.log', 'a')
        wf(error, 'logs/stderr.finally2.log', 'a')
    return (inspect.currentframe(), globals(), locals())
Example #21
0
def downloadUrl(url, args, outputDir=os.path.join(home, "Music/Downloads")):
    ''' Fragment from here
    http://stackoverflow.com/questions/18054500/how-to-use-youtube-dl-from-a-python-programm
    '''
    if not os.path.isdir(outputDir):
        os.makedirs(outputDir)
    outputFile = os.path.join(outputDir, '%(title)s-%(id)s.%(ext)s')
    if "--playlist" not in args:
        opts = [
            "-k", "-x", "-o", "{}".format(outputFile), "--audio-format", "mp3",
            "--no-playlist"
        ] + [url]
    else:
        print("[INFO] Downloading playlist from youtube.")
        opts = [
            "-k", "-x", "-o", "{}".format(outputFile), "--audio-format", "mp3"
        ] + [url]

    youtube_dl.main(opts)
Example #22
0
    def resolve(self, url, resolver):
        if 'plugin://' in url: return
        if not self.started: self.start()
        self.info_dicts = []
        stdout, stderr, exit = sys.stdout, sys.stderr, sys.exit
        # prevent youtube_dl from shutting us down, discard stderr/stdout
        sys.exit = self.noexit
        sys.stderr = sys.stdout = self.devnull
        # TODO: --cookies (https://github.com/rg3/youtube-dl/issues/41)
        #       --max-downloads (overuse triggers '402: Payment Required')
        # v2015.04.26+: explicitly setting '-f best' to avoid new 'merged format' separate a/v urls
        youtube_dl.main(['-s', '--no-playlist', '-f', 'best', url])
        sys.stdout, sys.stderr, sys.exit = stdout, stderr, exit

        if len(self.info_dicts) > 0:
            # get the first result
            i = self.info_dicts[0]
            if i.get('url') is None and i.get('requested_formats') is not None:
                # v 2014.12.10+ (shouldn't happen, but just in case)
                req = info_dict.get('requested_formats')
                if len(req) == 1:
                    i = req[0]
                else:
                    raise 'Error: multiple "merged format" urls received'
#			from pprint import pprint
#			pprint(i)
            details = {}
            if 'http_headers' in i:
                headers = {
                    k: v
                    for k, v in i['http_headers'].items()
                    if not k.lower().startswith('accept')
                }
                if headers:
                    details['headers'] = headers
            resolver.add(i['url'] + i.get('play_path', ''),
                         i.get('fulltitle'),
                         i.get('thumbnail'),
                         details=details if details else None)
Example #23
0
def dispatch(path, videos):
    """
  @param  path  unicode
  @param  videos  [str]
  @return  bool
  """
    dprint("enter")
    import youtube_dl
    oname = os.path.join(path, '%(title)s.%(ext)s')
    argv = [
        '-o',
        oname,
        None,  # vid
        #'--restrict-filenames',
    ]
    ret = []
    for vid in videos:
        argv[-1] = vid
        try:
            youtube_dl.main(argv)
        except SystemExit, e:
            dprint("exit code = %s" % e.code)
            ret.append(e.code)
Example #24
0
def ydl_main():
    from youtube_dl import main
    main()
Example #25
0
def get_videos():
    opts = [
        "-o", "./Videos/%(title)s.%(ext)s",
        "https://www.youtube.com/playlist?list=PLD63A284B7615313A"
    ]
    youtube_dl.main(opts)
Example #26
0
import feedparser
import sys
import urlparse
import youtube_dl

print 'Olls Youtube Subscription Downloader'

feedUrl = 'http://gdata.youtube.com/feeds/api/users/' + sys.argv[1] + '/uploads?max-results=1&alt=rss&orderby=published'
print 'Feed URL: ' + feedUrl + '\n'

feed = feedparser.parse(feedUrl)

videoLink = feed['entries'][0]['links'][0]['href']
print 'Video Link: ' + videoLink + '\n'

youtube_dl.main(videoLink)
Example #27
0
File: v.py Project: qiuri2008/yts
        self.buff = ''
        self.__console__ = sys.stdout

    def write(self, outstream):
        self.buff += outstream

    def flush(self):
        ''

    def get_data(self):
        return self.buff

    def reset(self):
        sys.stdout = self.__console__


def get_play_url(id):
    r_obj = my_print()
    sys.stdout = r_obj
    youtube_dl.main([u'-j', id])
    r_obj.reset()
    return r_obj.get_data()


if __name__ == '__main__':
    # print get_play_url(u'http://www.ted.com/talks/r_luke_dubois_insightful_human_portraits_made_from_data')
    youtube_dl.main([
        sys.argv[1], '-j', '--no-check-certificate',
        '--youtube-skip-dash-manifest', '--restrict-filenames'
    ])
def download(vids, what):
    if (what == 'video'):
        vid = vids[0]
        print(vid)
        ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
        with ydl:
            result = ydl.extract_info(
                vid,
                download=False  # We just want to extract the info
            )
        video = result
        # print(video)
        # print(json.dumps(video, indent = 2))
        print('---------------------------\n')
        print(video['title'])
        count = 1
        choices = {}
        audio = 1000
        for v_format in video['formats']:
            # print(v_format['format'] + " " + v_format['ext'])
            f = v_format['format'].split(' - ')[0]
            first_word = v_format['format'].split()[2]
            if (first_word == 'audio'):
                audio = min(audio, int(f))
                continue
            # print(first_word)
            # print(f)
            try:
                print('{}. {} - {} ({}MB)'.format(
                    count, v_format['format'].split(' - ')[1], v_format['ext'],
                    float(v_format['filesize']) / 1000000))
            except:
                print('{}. {} - {}'.format(count,
                                           v_format['format'].split(' - ')[1],
                                           v_format['ext']))
            choices[str(count)] = [f, first_word]
            count += 1
        # print(choices)
        choice = input(':   ')
        if (not (re.match(r'^([0-9]+)$', str(choice)) and int(choice) >= 1
                 and int(choice) <= int(count - 1))):
            choice = str(count - 1)
        opt = [
            '-cif',
            str(choices[choice][0] + '+' + str(audio)), '-o',
            '%(title)s.%(ext)s', vid
        ]

        youtube_dl.main(opt)
    else:
        # ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s'})
        # with ydl:
        #     result = ydl.extract_info(
        #         vid,
        #             download=False # We just want to extract the info
        #     )
        # video = result
        # # print(video)
        # # print(json.dumps(video, indent = 2))
        # print(video['title'])
        # # print('----------------------')
        # for v_format in video['formats']:
        #     # print(v_format['format'] + " " + v_format['ext'])
        #     f = v_format['format'].split(' - ')[0]
        #     first_word = v_format['format'].split()[2]
        #     if(first_word == 'audio'):
        #         opt = ['-f', f, vid]
        #         youtube_dl.main(opt)
        opt = [
            '--extract-audio', '--audio-format', 'mp3', '-o',
            '%(title)s.%(ext)s'
        ]
        for v in vids:
            opt.append(v)
        youtube_dl.main(opt)
Example #29
0
File: dl.py Project: araa47/vidl
def main():

    options = {
        'url': '',
        'file_format': 'mp3',
        'audio_only': True,
        'no_md': False,
        'no_smart_md': False,
        'no_dl': False,
        'verbose': False,
        'download_folder': config.get_config('download_folder'),
        'output_template': config.get_config('output_template'),
    }

    video_formats = ['mp4']
    audio_formats = ['mp3', 'wav', 'm4a']
    id3_metadata_formats = ['mp3']
    ytdl_output_template = os.path.join(options['download_folder'],
                                        options['output_template'])

    # parse arguments
    for arg in sys.argv[1:]:
        if arg in audio_formats:
            options['audio_only'] = True
            options['file_format'] = arg
        elif arg in video_formats:
            options['audio_only'] = False
            options['file_format'] = arg
        elif arg in ['--no-md']:
            options['no_md'] = True
        elif arg in ['--no-smart-md']:
            options['no_smart_md'] = True
        elif arg in ['--no-dl']:
            options['no_dl'] = True
        elif arg in ['-v', '--verbose']:
            options['verbose'] = True
        elif '.' in arg:
            parsed_url = urlparse(arg)
            url = arg
            if (parsed_url.scheme == ''): url = 'https://' + url
            print(url)
            options['url'] = url
        else:
            log.fatal('Unknown argument:', arg)
    if options['url'] == '':
        log.fatal('No URL provided')

    # get info
    log('Fetching URL info')
    ytdl_get_info_options = {
        'outtmpl': ytdl_output_template,
        'quiet': False if options['verbose'] else True,
    }
    with youtube_dl.YoutubeDL(ytdl_get_info_options) as ytdl:
        try:
            info_result = ytdl.extract_info(options['url'], download=False)
        except Exception as err:
            if options['verbose']: logging.exception(err)
            log.fatal('youtube-dl failed to get URL info')
        if options['verbose']: log.pretty(info_result)

    # delete None properties/indexes
    def callback(value):
        return value != None

    cleaned_info_result = deep_filter(copy.deepcopy(info_result), callback)

    # restructure
    url_info = copy.deepcopy(cleaned_info_result)
    if 'entries' in cleaned_info_result:
        videos = cleaned_info_result['entries']
        playlist_info = copy.deepcopy(cleaned_info_result)
        del playlist_info['entries']
    else:
        videos = [cleaned_info_result]
        playlist_info = {}

    # generate ytdl arguments
    ytdl_args = []
    if options['audio_only']:
        ytdl_args += ['-x']
        ytdl_args += ['-f', 'best']
        ytdl_args += ['--audio-format', options['file_format']]
    else:
        ytdl_args += ['-f', 'bestvideo+bestaudio']
        ytdl_args += ['--recode-video', options['file_format']]
    ytdl_args += ['--audio-quality', '0']
    ytdl_args += ['-o', ytdl_output_template]
    if options['file_format'] in ['mp3', 'm4a', 'mp4']:
        ytdl_args += ['--embed-thumbnail']
    if not options['verbose']:
        ytdl_args += ['--quiet']
    # band-aid solution for 403 error caused by cache
    ytdl_args += ['--rm-cache-dir']

    video_index = -1
    first_video_artist = ''
    errors = []
    for video in videos:
        video_index += 1
        try:
            filename = ytdl.prepare_filename(video)
        except (Exception, SystemExit) as err:
            if options['verbose']: logging.exception(err)
            error_msg = 'Failed to generate a filename for URL: ' + green(
                video['webpage_url'])
            if len(videos) == 1: log.fatal(error_msg)
            log.error(error_msg)
            errors.append(video)
            continue
        filename_split = filename.split('.')
        filename_split[len(filename_split) - 1] = options['file_format']
        filename = '.'.join(filename_split)
        if options['verbose']:
            # print youtube-dl command:
            command = green('youtube-dl command: ') + 'youtube-dl '
            for arg in ytdl_args + [video['webpage_url']]:
                if ' ' in arg or '&' in arg: command += "'" + arg + "' "
                else: command += arg + ' '
            log(command)
        if options['no_dl']:
            continue
        log('Downloading')

        # download
        try:
            youtube_dl.main(ytdl_args + [video['webpage_url']])
        except (Exception, SystemExit) as err:
            if type(err) == SystemExit and err.code == 0:
                # don't treat sys.exit(0) as error
                pass
            else:
                if options['verbose']: logging.exception(err)
                error_msg = 'Failed to download URL: ' + green(
                    video['webpage_url'])
                if len(videos) == 1: log.fatal(error_msg)
                log.error(error_msg)
                errors.append(video)
                continue
        log('Saved as', filename)

        # id3 tags
        if options['file_format'] in id3_metadata_formats and not options[
                'no_md']:
            log('Adding metadata to file')

            # get artist/title from title
            parsed_title = {}
            if not options['no_smart_md']:
                if 'title' in video and video['title'].count(' - ') == 1:
                    split_title = video['title'].split(' - ')
                    parsed_title['artist'] = split_title[0]
                    parsed_title['title'] = split_title[1]

            md = {}
            playlist = True if len(videos) > 1 else False

            # smart title
            if 'title' in parsed_title:
                smart_title = True
            else:
                smart_title = False
            # title
            if 'title' in video:
                md['title'] = video['title']
            elif 'track' in video:
                md['title'] = video['track']

            # smart artist
            if 'artist' in parsed_title:
                smart_artist = True
            else:
                smart_artist = False
            # artist
            if 'uploader' in video:
                md['artist'] = video['uploader']
            elif 'artist' in video:
                md['artist'] = video['artist']
            # youtube music artist
            if  video['extractor'] == 'youtube' \
            and video['uploader'].endswith(' - Topic') \
            and 'artist' in video:
                if 'categories' not in video:
                    md['artist'] = video['artist']
                elif video['categories'] == ['Music']:
                    md['artist'] = video['artist']

            use_first_video_artist = False
            if playlist:
                #album
                if 'title' in playlist_info:
                    md['album'] = playlist_info['title']
                elif 'playlist_title' in video:
                    md['album'] = video['playlist_title']
                elif 'playlist' in video and type(video['playlist']) == str:
                    md['album'] = video['playlist']
                #album_artist
                if 'uploader' in playlist_info:
                    md['album_artist'] = playlist_info['uploader']
                elif 'playlist_uploader' in video:
                    md['album_artist'] = video['playlist_uploader']
                else:
                    use_first_video_artist = True
                # track_number
                if 'playlist_index' in video:
                    md['track_number'] = video['playlist_index']
                else:
                    md['track_number'] = video_index + 1
                # track_count
                if 'n_entries' in video:
                    md['track_count'] = video['n_entries']
                else:
                    md['track_count'] = len(videos)
            # year
            def is_int(number):
                try:
                    int(number)
                    return True
                except ValueError:
                    return False

            if 'release_date' in video and is_int(video['release_date'][:4]):
                md['year'] = video['release_date'][:4]
            elif 'publish_date' in video and is_int(video['publish_date'][:4]):
                md['year'] = video['publish_date'][:4]
            elif 'upload_date' in video and is_int(video['upload_date'][:4]):
                md['year'] = video['upload_date'][:4]

            dumb_md = copy.deepcopy(md)
            if smart_title: md['title'] = parsed_title['title']
            if smart_artist: md['artist'] = parsed_title['artist']

            if playlist:
                # save artist of first video

                if video_index == 0 and 'artist' in md:
                    first_video_artist = md['artist']

                # use first video's artist as album artist if no other is found
                if use_first_video_artist:
                    md['album_artist'] = first_video_artist

            md = config.user_md_parser(md, dumb_md, video, url_info)

            md_module.add_metadata(filename, md)

    if len(errors) >= 1:
        msg = 'There were errors when downloading the following URLs:'
        for video in errors:
            msg += f'\n- {green(video["webpage_url"])}: {video["uploader"]} - {video["title"]}'
        log.fatal(msg)
    log('Done')
Example #30
0
import youtube_dl
title = "test"
link = "http://www.youtube.com/watch?v=a1Y73sPHKxw"

#print(youtube_dl.main(['-f', '85/84/83/82/38/37/22/18/120/35/34','-o','/home/gareth/video/uploads/' + title  + '.%(ext)s', link, '-s', '--get-filename']))
#youtube_dl.main([link, '--all-format', '--get-filename', '--get-format'])

print "-----------------"


print (youtube_dl.main([link, '--all-format',  '--get-format']))

print (12345)
Example #31
0
#!/usr/bin/env python

# install youtube_dl and fake ctypes in lib/python2.7/site-packages

import os
import youtube_dl
import androidhelper

print "trying to retrieve from clipboard"

try:
    os.chdir("/mnt/sdcard/Download")
except:
    print "failed chdir /mnt/sdcard/Download"

print "working in", os.getcwd()

the_url = androidhelper.Android().getClipboard().result

the_url = the_url.split()[-1]

if __name__ == "__main__":
    youtube_dl.main(["--format", "m4a", the_url])
    def download_meiju_episode(self, collector, meiju_ename, season_id, episode_id, save_folder_path):

        # Check whether collector instance invalid
        if collector is None:
            logger.error("Collector instance is None")
            return

        # Check whether invalid input parameter
        if save_folder_path is None or len(save_folder_path) == 0:
            logger.error("Invalid folder path")
            return

        # Check whether need to create dir
        if not os.path.exists(save_folder_path):
            os.makedirs(save_folder_path)
            logger.debug("Create directory %s" % save_folder_path)

        # Check whether dir contains the Meiju folder
        if save_folder_path.find(meiju_ename.replace(" ", "_")) == -1:
            save_folder_path = os.path.join(save_folder_path, meiju_ename.replace(" ", "_"))
            if not os.path.exists(save_folder_path):
                os.makedirs(save_folder_path)

        # Check whether dir contains Season folder
        if save_folder_path.find("Season"+str(season_id)) == -1:
            save_folder_path = os.path.join(save_folder_path, "Season"+str(season_id))
            if not os.path.exists(save_folder_path):
                os.makedirs(save_folder_path)

        # Check whether download file already exists
        output_file_name = "Season" + str(season_id) + "Ep" + str(episode_id) + ".mp4"
        output_file_path = os.path.join(os.path.abspath(save_folder_path), output_file_name)
        if os.path.exists(output_file_path):
            logger.debug("File %s already exists, no need to download" % output_file_path)
            return

        # Get episode instace
        if meiju_ename in collector.meiju_ename_inst_dict:
            meiju_inst = collector.meiju_ename_inst_dict[meiju_ename]
            if season_id in meiju_inst.season_id_inst_dict:
                season_inst = meiju_inst.season_id_inst_dict[season_id]
                if episode_id in season_inst.episode_id_inst_dict:
                    episode_inst = season_inst.episode_id_inst_dict[episode_id]

                    episode_url = episode_inst.url
                    cookiejar = CookieJar()
                    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
                    values = {"log": "us02",
                              "pwd": "0000",
                              "redirect_to": episode_url,
                              "a": "login",
                              "Submit": "%E7%99%BB%E5%85%A5"}
                    data = urllib.urlencode(values)
                    response = opener.open(episode_url, data)
                    response_str = response.read()
                    soup = BeautifulSoup(response_str.replace("\n",""), "html.parser")

                    # First we lookup videomega first
                    iframe_tag_list = soup.find_all("iframe", src=re.compile("videomega"))
                    if len(iframe_tag_list) != 0:
                        for iframe_tag in iframe_tag_list:
                            urlstr = iframe_tag["src"][:iframe_tag["src"].find("&")]
                            logger.debug("Videomega URL: %s" % urlstr)
                            logger.debug("Save to file %s" % output_file_path)
                    # Sometimes when iframe tag is under script tag, BeautifulSoup cannot parse it
                    else:
                        pattern = r"src=\"(http:\/\/videomega.tv\/view.php\?ref=.+)\""
                        regex = re.compile(pattern)
                        match = regex.search(response_str)
                        urlstr = match.group(1)[:match.group(1).find("&")]
                        logger.debug("Videomega URL: %s" % urlstr)
                        logger.debug("Save to file %s" % output_file_path)

                    # If there is part file, first we backup it
                    part_file_path = output_file_path + ".part"
                    if os.path.exists(part_file_path):
                        shutil.copyfile(part_file_path, part_file_path + ".bak")

                    # Use youtube-dl to download the video
                    argv = ["-o", unicode(output_file_path), urlstr]
                    try:
                        youtube_dl.main(argv)

                        # If download completes, we need to check the file size to decide whether succeed
                        file_size = os.path.getsize(output_file_path)
                        # If file size less than 1M, just remove the file and recover the part file
                        if file_size < 1000000:
                            os.remove(output_file_path)
                            if os.path.exists(part_file_path + ".bak"):
                                shutil.move(part_file_path + ".bak", part_file_path)
                        # If file size is ok, we just remove the part backup file
                        else:
                            if os.path.exists(part_file_path + ".bak"):
                                os.remove(part_file_path + ".bak")
                    except:
                        logger.error("Error downloading the video")
                        # Remove the backup of part file
                        if os.path.exists(part_file_path + ".bak"):
                            os.remove(part_file_path + ".bak")
                else:
                    logger.error("Failed to lookup Episode instance with Episode Id %d" % episode_id)
                    return
            else:
                logger.error("Failed to lookup Season instance with Season Id %d" % season_id)
                return
        else:
            logger.error("Failed to lookup Meiju with English name %s" % meiju_ename)
            return
Example #33
0
def youtube_dl(options):
    try:
        yt.main(options)
    except SystemExit as e:  # yt.main calls sys.exit
        if e.code != 0:
            raise e
Example #34
0
#! /usr/bin/env python

from youtube_dl import main
from sys import argv

if __name__ == '__main__':
    url = "https://www.youtube.com/playlist?list=PLdN-rWQpN5Rp5OCjOQsR4RR8Kz7EQAFKy"
    user = "******"

    if argv[1] == "--music":
        cmd = [
            "--yes-playlist", "-o", "~/Zene/%(title)s.%(ext)s",
            "--username={}".format(user), "-x", url
        ]
    else:
        cmd = [
            "--yes-playlist", "-o", "~/Videók/%(title)s.%(ext)s",
            "--username={}".format(user), url
        ]

    main(cmd)
Example #35
0
#!/usr/bin/env python
import youtube_dl

DOWNLOAD_DIR = '/media/NETCODE-VIDEOS/%(title)s.%(ext)s'
with open('urls.txt', 'w+') as urls:
    with open('links.csv', 'r') as f:
        for line in f:
            print line
            if 'link,title' in line:
                print "link,title"
                continue
            url='http://player.vimeo.com/video/{id}\n'.format(id=line.split(',')[0])
            print url
            urls.write(url)
        
youtube_dl.main([url,'-a', 'urls.txt', '-o', DOWNLOAD_DIR])
	
Example #36
0
os.chdir(target)
pending = glob.glob('*.part')
if not pending:
    print('---\nNo pending files.')
    exit()
print('---\nFound {} pending files:\n---'.format(len(pending)))
print('\n---\n'.join(pending))

os.system('df .')

start = time.time()
print('Importing youtube_dl...')
import youtube_dl
print('Done in {:.1f} s'.format(time.time() - start))

android = androidhelper.Android()

for pending_file in pending:
    the_url = pending_file[:-5]
    filename, _ = os.path.splitext(the_url)
    the_url = filename[-11:]
    print('---\nTo download: {}\n---'.format(the_url))
    if not os.fork():
        youtube_dl.main(['--no-check-certificate', the_url])
    os.wait()

    os.system('df .')
    android.vibrate(1000)
    print('---')

Example #37
0
#!/usr/bin/env python

# install youtube_dl and fake ctypes in lib/python2.7/site-packages

import os
import youtube_dl
import androidhelper

print "trying to retrieve from clipboard"

try:
    os.chdir('/mnt/sdcard/Download')
except:
    print 'failed chdir /mnt/sdcard/Download'

print 'working in', os.getcwd()

the_url = androidhelper.Android().getClipboard().result

the_url = the_url.split()[-1]

if __name__ == '__main__':
    youtube_dl.main([the_url])
Example #38
0
def get_videos():
    opts = ["-o", "./Videos/%(title)s.%(ext)s",
            "https://www.youtube.com/playlist?list=PLD63A284B7615313A"]
    youtube_dl.main(opts)
Example #39
0
def inicio():
    try:
        raise ValueError  #gera um erro para acessar o except

    except ValueError:
        print("pegamos uma excessão")


def teste():
    try:
        numeroteste = int(input("digite um numero entre 0 e 10: "))
        if 0 < numeroteste > 10:
            raise MeuErro
        else:
            print(f"deu tudo certo, o numero escolhido foi {numeroteste}")
    except MeuErro:
        print(f"entrada invalida!{numeroteste}")


class MeuErro(Exception):
    def __str__(self):
        return "numero já digitado!"


teste()

raise MeuErro

if __name__ == "__main__":
    main()
Example #40
0
target = os.path.dirname(__file__) + '/../../Download'

try:
    os.chdir(target)
except OSError:
    print('failed changing directory:', target)
    exit()

os.system('df .')

android = androidhelper.Android()

if not android.checkWifiState().result:
    print('Not on WiFi, exiting.')
    exit()

the_url = android.getClipboard().result
the_url = the_url.split()[-1]


def after_download():
    os.system('df .')
    android.vibrate(2000)


atexit.register(after_download)

print('Starting download of: ' + the_url)
import youtube_dl
youtube_dl.main(['--no-check-certificate', '--format', 'm4a', the_url])
Example #41
0
File: v.py Project: qiuri2008/yts
def get_play_url(id):
    r_obj = my_print()
    sys.stdout = r_obj
    youtube_dl.main([u'-j', id])
    r_obj.reset()
    return r_obj.get_data()
Example #42
0
#!/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()
Example #43
0
import os, sys, io, pathlib, subprocess
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QFileDialog
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QRunnable, QObject, QThreadPool
import youtube_dl

# if this script was called with the youtube-dl module, we need to run that
_expect_mod = False
for index, arg in enumerate(sys.argv):
    if arg == "-m":
        _expect_mod = True
    if _expect_mod:
        if arg == "youtube_dl":
            # run youtube-dl with remaining args
            youtube_dl.main(sys.argv[index+1:])
            exit()

class DownloadSignals(QObject):
    """Possible signals from a DownloadWorker"""
    finished = pyqtSignal(int)
    message = pyqtSignal(str)
    ready = pyqtSignal(str)


class DownloadWorker(QRunnable):
    '''
    Worker thread for downloading the youtube video. By running the
    subprocess in a separate thread, the PyQt5 will not hang.
    '''
    signals = DownloadSignals()