Thursday, 8 October 2020

Youtube-dl subscription to mp3

So my goal is to make code so it would automatically download from all my subscribed Youtube channels to mp3 files. I having difficulty to deal with EO error which is not clear for me, thus I never had to deal with it, I've done research but nothing could help me out, so here's the code:

import opml
import feedparser
import youtube_dl
from glob import glob
from pprint import pprint

from time import time, mktime, strptime
from datetime import datetime

if len(glob('last.txt')) == 0:
    f = open ('last.txt' , 'w')
    f.write(str(time()))
    print('Initialized last.txt file for timestamp')
    f.close()
else:
    f = open('last.txt' , 'r')
    content = f.read()
    f.close()
    
    outline = opml.parse('subs.xml')
    
    ptime = datetime.utcfromtimestamp(float(content))
    ftime = time()
    urls = []
    for i in range(0,len(outline[0])):
        urls.append(outline[0][i].xmlUrl)
    print(urls)
    
    videos = []
    for i in range(0,len(urls)):
        print('Parsing through channel '+str(i+1)+' out of '+str(len(urls)), end='\r')
        feed = feedparser.parse(urls[i])
        for j in range(0,len(feed['items'])):
            timef = feed['items'][j]['published_parsed']
            dt = datetime.fromtimestamp(mktime(timef))
            if dt > ptime:
                videos.append(feed['items'][j]['link'])
                
    if len(videos) == 0:
        print('Sorry, no new video found')
    else:
        print(str(len(videos))+' bew vudeis found')
        
    ydl_options = {
            'ignoreerrors' : True,
            'format': 'bestaudio[filesize<30]',
            'keepvideo': False,
            'outtmpl': 'filename',
            'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'audioquality': '0',
                    'preferredquality': '320',
            }]
    }
     
    with youtube_dl.YoutubeDL(ydl_options) as ydl:
        ydl.download(videos)
        

I have tried new YoutubeManager subs.xml , tried other Youtube account with different channels and their subs.xml nothing helped.

And here is my error output

runfile('C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py', wdir='C:/Users/sound/Desktop/PythonProjets')
Traceback (most recent call last):

  File "<ipython-input-1-ff8a84b96d09>", line 1, in <module>
    runfile('C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py', wdir='C:/Users/sound/Desktop/PythonProjets')

  File "C:\Users\sound\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\sound\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/sound/Desktop/PythonProjets/youtubesubscriptions.py", line 29, in <module>
    outline = opml.parse('subs.xml')

  File "C:\Users\sound\Anaconda3\lib\site-packages\opml\__init__.py", line 67, in parse
    return Opml(lxml.etree.parse(opml_url))

  File "src/lxml/etree.pyx", line 3435, in lxml.etree.parse

  File "src/lxml/parser.pxi", line 1840, in lxml.etree._parseDocument

  File "src/lxml/parser.pxi", line 1866, in lxml.etree._parseDocumentFromURL

  File "src/lxml/parser.pxi", line 1770, in lxml.etree._parseDocFromFile

  File "src/lxml/parser.pxi", line 1163, in lxml.etree._BaseParser._parseDocFromFile

  File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc

  File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult

  File "src/lxml/parser.pxi", line 638, in lxml.etree._raiseParseError

OSError: Error reading file 'subs.xml': failed to load external entity "subs.xml"


from Youtube-dl subscription to mp3

No comments:

Post a Comment