mps-youtube / yewtube

yewtube, forked from mps-youtube , is a Terminal based YouTube player and downloader. No Youtube API key required.
GNU General Public License v3.0
8.12k stars 642 forks source link

ssl.SSLError on lastfm_connect #973

Open DmitrySandalov opened 5 years ago

DmitrySandalov commented 5 years ago

Issue / Suggestion

  1. Open mpsyt
  2. Set up valid last.fm user/password/token/secret
  3. Execute lastfm_connect

Expected: mpsyt successfully connects to last.fm

Observed: app crash with exit code 1

Traceback (most recent call last):
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/pylast/__init__.py", line 944, in _download_response
    conn.request(method="POST", url=host_subdir, body=data, headers=headers)
  File "/usr/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/lib/python3.6/http/client.py", line 1400, in connect
    server_hostname=server_hostname)
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 817, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1077, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/mps_youtube/commands/lastfm.py", line 36, in init_network
    password_hash=password)
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/pylast/__init__.py", line 716, in __init__
    "user": "user/%(name)s",
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/pylast/__init__.py", line 219, in __init__
    self.session_key = sk_gen.get_session_key(self.username, self.password_hash)
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/pylast/__init__.py", line 1092, in get_session_key
    doc = request.execute()
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/pylast/__init__.py", line 965, in execute
    response = self._download_response()
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/pylast/__init__.py", line 946, in _download_response
    raise NetworkError(self.network, e)
pylast.NetworkError: NetworkError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:847)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/mpsyt", line 10, in <module>
    sys.exit(main.main())
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/mps_youtube/main.py", line 148, in main
    if matchfunction(i.function, i.regex, userinput):
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/mps_youtube/main.py", line 65, in matchfunction
    func(*matches)
  File "/home/user/.virtualenvs/mps-youtube/lib/python3.6/site-packages/mps_youtube/commands/lastfm.py", line 41, in init_network
    g.message = "Last.fm connection error: %s" % (str(e))
NameError: name 'e' is not defined

My employer blocks access to last.fm:

  % curl -v last.fm
* Rebuilt URL to: last.fm/
*   Trying 146.112.61.106...
* TCP_NODELAY set
* Connected to last.fm (146.112.61.106) port 80 (#0)
> GET / HTTP/1.1
> Host: last.fm
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 403 Forbidden
< Server: openresty/1.9.7.3
< Date: Fri, 19 Apr 2019 12:33:08 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
<
<html><head><script type="text/javascript">location.replace("https://block.opendns.com/?url=77668485157178&ablock&server=fra15&prefs=&tagging=&nref");</script></head></html>
* Connection #0 to host last.fm left intact

Seems like adding last.fm to /etc/hosts would reproduce the issue.

I'm suggesting to add ssl.SSLError handling in commands.lastfm.init_network() except block.

Your Environment

mpsyt version      : 0.2.8
   notes           : released 17 February 2018
pafy version       : 0.5.4 (youtube-dl backend)
youtube-dl version : 2019.04.17
Python version     : 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0]
Processor          : x86_64
Machine type       : x86_64
Architecture       : 64bit, ELF
Platform           : Linux-4.15.0-47-generic-x86_64-with-Ubuntu-18.04-bionic
sys.stdout.enc     : UTF-8
default enc        : utf-8
Config dir         : /home/user/.config/mps-youtube
env:TERM           : xterm-256color
env:SHELL          : /usr/bin/zsh
env:LANG           : en_US.UTF-8
  % pip freeze
mps-youtube==0.2.8
pafy==0.5.4
pylast==3.1.0
youtube-dl==2019.4.17
  % python -V
Python 3.6.7