3

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"
  • I feel like we are missing parts of your code. Is that all the code you have for this project? – FrozenAra Oct 07 '20 at 12:58
  • I added now the beginning of code, I could not post it before, because it said it's to much code and needed more explanation, but It's error that is out of my knowledge and research I done. – Edgaras Vaninas Oct 07 '20 at 16:50

1 Answers1

0

The error shows that you dont have access to that file.
If I run print(opml.parse('subs.xml')) on my PC I get exactly the same error message. Either the path is wrong or you dont have read access to that file.

The way your code is setup means that python looks for that file in the path where you are running the .py file from.
Is subs.xml in the same folder as your python file?
One way you could try would be linking the path directly like this:
outline = opml.parse(r'C:\folder_name\subs.xml')

FrozenAra
  • 509
  • 2
  • 14
  • It always was in the same folder, and I tried your way: `outline = opml.parse(r'C:\Users\sound\Desktop\PythonProjets\subs.xml')` and I still have this error: `OSError: Error reading file 'C:\Users\sound\Desktop\PythonProjets\subs.xml': failed to load external entity "file:/C:/Users/sound/Desktop/PythonProjets/subs.xml"` – Edgaras Vaninas Oct 08 '20 at 15:36
  • Its possible that you have a read problem with that file then. Can you place the file in another location and try that? – FrozenAra Oct 08 '20 at 15:43
  • I've did swap to D disk, but still same error. ```OSError: Error reading file 'D:\subs.xml': failed to load external entity "file:/D:/subs.xml" ``` – Edgaras Vaninas Oct 08 '20 at 15:47
  • Can you please try the following. Create a new .py file inside your project folder where you youtubesubscriptions.py is placed and put in the following: `import os` `print(os.access(r'C:\Users\sound\Desktop\PythonProjets\subs.xml', 2))` of course make sure the file is actually in that place. This checks if you have read access on that file. Tell me what it returns please. – FrozenAra Oct 08 '20 at 15:54
  • I did as you said, this is my outcome: ```runfile('C:/Users/sound/Desktop/PythonProjets/access.py', wdir='C:/Users/sound/Desktop/PythonProjets') False``` – Edgaras Vaninas Oct 08 '20 at 16:15
  • Okay thats what I was hoping for. Its not good but at least I know whats wrong now. You dont have read access to that file. What you can try next is to copy the whole project to your D drive and try to run your code again. – FrozenAra Oct 08 '20 at 16:44
  • I did as you said, but still having same issue, do you have any other solution? I will definitely award you with bounty. And sorry for slow reply , had busy weekend. – Edgaras Vaninas Oct 12 '20 at 10:59
  • Its all good. Ill see what I can think of! One more test we can try. Can you run your code again like at the start. From your C: Drive and in your normal Folder. But instead of using the normal `subs.xml` can you create an empty .xml file? Just call it `test.xml` and do the same test with that file. Either we will get the same error again or we will get a different error. If we get a different error then before we know its the xml file that creates a problem. You should get an error like this `lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1` – FrozenAra Oct 12 '20 at 11:08
  • Unfortunately, even with empty test.xml in C: Drive I received same error. ```OSError: Error reading file 'C:\Users\sound\Desktop\PythonProjets\subs.xml': failed to load external entity "file:/C:/Users/sound/Desktop/PythonProjets/subs.xml" ``` I'm about to get Linux at this point, due it's gonna be good experience for me in general and gonna test there. But if you have any suggestion, I would really really appreciate your help. – Edgaras Vaninas Oct 12 '20 at 12:22
  • I just realized we forgot to try one thing. Can you try to run the scrip with admin rights? – FrozenAra Oct 12 '20 at 12:29
  • So I did as you said, and I ran test.xml ``` File "file:/C:/Users/sound/Desktop/PythonProjets/test.xml", line 1 XMLSyntaxError: Document is empty, line 1, column 1 ``` Seems you were right and I'm having issue with subs.xml file, do you have idea why? – Edgaras Vaninas Oct 12 '20 at 13:50
  • Wait. Did you try the admin test on the test.xml? If so then you only need to run your original script (With subs.xml) with admin rights and it should work. – FrozenAra Oct 12 '20 at 13:54
  • 1
    So I managed to get through now seems all bad issue was that my xml file had in 'name.xml' as being xml already :D `TypeError: __init__() got an unexpected keyword argument audioquality` Can't ask more from you. I'm going to proceed ant try to launch my project. Thank you @FrozenAra – Edgaras Vaninas Oct 12 '20 at 13:59
  • Not sure about the exact problem anymore but glad I could help! And thanks for the reward! – FrozenAra Oct 12 '20 at 14:19
  • Basically it was named subs.xml (while type xml) and on test when I created file it was test(while type xml) and it worked, such a minor mistake hold me up for a while. – Edgaras Vaninas Oct 12 '20 at 15:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222925/discussion-between-edgaras-vaninas-and-frozenara). – Edgaras Vaninas Oct 12 '20 at 15:10