ZeroQI / Hama.bundle

Plex HTTP Anidb Metadata Agent (HAMA)
GNU General Public License v3.0
1.2k stars 113 forks source link

Two issues with the new urllib2 function. #194

Closed KurtzPT closed 4 years ago

KurtzPT commented 6 years ago

I'm going to create this new issue here because the old one is on the wrong repository.

I just updated to your fixed code (https://github.com/ZeroQI/Hama.bundle/commit/2a30c2d0904a4ab683fd4fa3bc66f9cde14f38be) and tried to test it out a bit.

First error:

After trying to refresh metadata from one anime show, Hama crashed (I found this on com.plexapp.system.log):

2018-05-10 10:49:27,466 (779c9a563700) : CRITICAL (agentservice:565) - Exception in update for com.plexapp.agents.hama://tvdb4-81797?lang=en (most recent call last): File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/System.bundle/Contents/Code/agentservice.py", line 407, in update_task self.agent_update_metadata(identifier, media_type, guid, id, lang, dbid, parentGUID, force, version, parentID, periodic) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/System.bundle/Contents/Code/agentservice.py", line 1236, in agent_update_metadata kwargs=kwargs File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/messaging.py", line 86, in call_external_function packed_result = self._core.networking.http_request(url, cacheTime=0, timeout=None, immediate=True).content File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 370, in http_request return HTTPRequest(self._core, url, data, h, url_cache, encoding, errors, timeout, immediate, sleep, opener, follow_redirects, method) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 141, in init self.load() File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Versions/2/Python/Framework/components/networking.py", line 181, in load f = self._opener.open(req, timeout=self._timeout) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 444, in open response = meth(req, response) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 557, in http_response 'http', request, response, code, msg, hdrs) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 482, in error return self._call_chain(args) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 416, in _call_chain result = func(args) File "/usr/lib/plexmediaserver/Resources/Plug-ins-31d3c0c65/Framework.bundle/Contents/Resources/Platforms/Shared/Libraries/urllib2_new.py", line 565, in http_error_default raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) HTTPError: HTTP Error 500: Internal Server Error

It does seem that there's no error handling from this new download function when the remote URL gives a HTTP error? It doesn't crash if the remote server doesn't throw a HTTP error.


Second error:

I removed tvdb4 poster file to force the plugin to download it again, and the problem isn't fixed, but it gives a different error this time:

2018-05-10 10:59:09,505 (72dc4ae59700) : INFO (common:408) - common.GetMetadata() - tvdb4 mode 2018-05-10 10:59:09,535 (72dc4ae59700) : INFO (common:417) - common.GetMetadata() - No 'tvdb4.posters.xml' file detected locally 2018-05-10 10:59:09,535 (72dc4ae59700) : DEBUG (common:267) - common.LoadFile() - relativeFilename: 'tvdb4.posters.xml', Data.Exists(relativeFilename): False, fe: False, url: 'http://rawgit.com/ZeroQI/Absolute-Series-Scanner/master/tvdb4.posters.xml' does not exists in cache 2018-05-10 10:59:10,045 (72dc4ae59700) : WARNING (common:267) - common.LoadFile() - issue loading url: 'http://rawgit.com/ZeroQI/Absolute-Series-Scanner/master/tvdb4.posters.xml', filename: 'tvdb4.posters.xml', Exception: '<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>'

I saw that you didn't use requests but besides that, I didn't looked at your code too much, so I don't have any suggestions for this one.

ZeroQI commented 6 years ago

@KurtzPT

KurtzPT commented 6 years ago

On the first error:

There isn't really a good way to replicate the first error since you have to hope to get a HTTP error on TVDB (which it's random, I just got the error on my first try by coincidence), but the anime in question was One Piece (It can be seen in the first line of the error:

2018-05-10 10:49:27,466 (779c9a563700) : CRITICAL (agentservice:565) - Exception in update for com.plexapp.agents.hama://tvdb4-81797?lang=en (most recent call last)

The hama log doesn't have any errors, it's just the usual start/init process log, but I'll attach it anyway: com.plexapp.agents.hama.log

On the second error:

Changing the URL didn't fix it.

2018-05-10 14:43:33,012 (6eba954ff700) : INFO (common:408) - common.GetMetadata() - tvdb4 mode 2018-05-10 14:43:33,041 (6eba954ff700) : INFO (common:417) - common.GetMetadata() - No 'tvdb4.posters.xml' file detected locally 2018-05-10 14:43:33,042 (6eba954ff700) : DEBUG (common:267) - common.LoadFile() - relativeFilename: 'tvdb4.posters.xml', Data.Exists(relativeFilename): False, fe: False, url: 'http://raw.githubusercontent.com/ZeroQI/Absolute-Series-Scanner/master/tvdb4.posters.xml' does not exists in cache 2018-05-10 14:43:33,063 (6eba954ff700) : WARNING (common:267) - common.LoadFile() - issue loading url: 'http://raw.githubusercontent.com/ZeroQI/Absolute-Series-Scanner/master/tvdb4.posters.xml', filename: 'tvdb4.posters.xml', Exception: '<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>'

ZeroQI commented 6 years ago

Fixed quite few things, please test and tell me if any issue remaining Sven-7 had his issues resolved with that one. the multithread download was missing episode and season which messed things up seasons ans episode metadata seem correct in my tests

KurtzPT commented 6 years ago

Just tested again, it seems error two is fixed. I'll be closing this and report if I find more errors like the first one.

KurtzPT commented 4 years ago

I'm having the 2nd error yet again:

[x] posters ( 1) Sources: tvdb4, TheTVDB, TheMovieDb, FanartTV, (AniDB) Inside: '['AniDB']' Value: '{'http://img7.anidb.net/pics/anime/237552.jpg': ('AniDB/poster/237552.jpg', 25, 'http://img7.anidb.net/pics/anime/thumbs/150/237552.jpg-thumb.jpg')}' common.metadata_download() - Exception: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)>, url: 'http://img7.anidb.net/pics/anime/237552.jpg', filename: 'AniDB/poster/237552.jpg'

KurtzPT commented 4 years ago

The URL http://img7.anidb.net/pics/anime/237552.jpg (that is HTTP) redirects to the following URL with (HTTPS): https://cdn.anidb.net/images/main/237552.jpg

In the ssl_open function, this function doesn't really check if the URL given is redirected to a HTTPS URL:

if url.startswith('https://'): return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1), timeout=timeout).read() else: return urllib2.urlopen(urllib2.Request(url, headers=headers), timeout=timeout).read()

So what I think it's happening is since the first URL is not HTTPS, it tries to open without a SSLContext. But since this URL redirects to a CDN behind SSL, it then gives the error.

Stuxles commented 4 years ago

Having the same issue here, I'm running plex in a linux docker, however when i tried it from my windows server there was no error.

ZeroQI commented 4 years ago

https://github.com/ZeroQI/Hama.bundle/issues/349 another person impacted, anidb poster

@KurtzPT i agree. Note sure if i can provide "context=ssl.SSLContext(ssl.PROTOCOL_TLSv1)," on http without error... Not having the error under windows, please try the following:

Test 1: (brilliant if works as shorter) return urllib2.urlopen(urllib2.Request(url, headers=headers), timeout=timeout).read() Test 2:

if url.startswith('http://'):  return urllib2.urlopen(urllib2.Request(url, headers=headers), timeout=timeout).read()
return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1), timeout=timeout).read()
EndOfLine369 commented 4 years ago

Problem is not the context. It looks to me that the issue is the CA cert chain they decided to use of "Let's Encrypt" is not known to Plex.

EndOfLine369 commented 4 years ago

Maybe not....Intermediate cert "Let's Encrypt Authority X1" was not seen the Plex cacerts file but the root "DST Root CA X3" is.

Got urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed Updated to to always pass ssl context:

def ssl_open(url, headers={}, timeout=20):
  headers = UpdateDict(headers, HEADERS_CORE)
  return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1), timeout=timeout).read()

Got urlopen error [SSL: TLSV1_ALERT_PROTOCOL_VERSION] Updated to use PROTOCOL_TLSv1_2:

def ssl_open(url, headers={}, timeout=20):
  headers = UpdateDict(headers, HEADERS_CORE)
  return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2), timeout=timeout).read()
ZeroQI commented 4 years ago

@EndOfLine369 Thanks for the tests. Not having the issue made it tricky to resolve... If i understood you correctly, you replaced in the file https://github.com/ZeroQI/Hama.bundle/blob/master/Contents/Code/common.py lines 195-197 (3 lines):

  headers = UpdateDict(headers, HEADERS_CORE)
  if url.startswith('https://'): return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1), timeout=timeout).read()
  else: return urllib2.urlopen(urllib2.Request(url, headers=headers), timeout=timeout).read()

with the following two lines:

  headers = UpdateDict(headers, HEADERS_CORE)
  return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2), timeout=timeout).read() 

And it solves it for you ? If so will include in master code as it is also shorter so @KurtzPT and @Stuxles can test if resolved

iCrawl commented 4 years ago

I can confirm switching to return urllib2.urlopen(urllib2.Request(url, headers=headers), context=ssl.SSLContext(ssl.PROTOCOL_TLSv1_2), timeout=timeout).read() fixes the issue.

Old: as seen in #349

After change: image

Refresh metadata was all I did after editing common.py and restarting plex.

KurtzPT commented 4 years ago

I also confirm it works now with @EndOfLine369 two lines.

EndOfLine369 commented 4 years ago

@ZeroQI, just noticed something from looking at this... https://github.com/ZeroQI/Hama.bundle/blob/master/Contents/Code/common.py#L437 Why is HAMA downloading the thumbnails?

Big difference. EX: https://cdn.anidb.net/images/150/226372.jpg-thumb.jpg https://cdn.anidb.net/images/main/226372.jpg Wondered why some times the poster would look fuzzy or details were not as defined as I would see on the site.