1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mpsytを実行すると日ごと制限エラー

Last updated at Posted at 2019-08-25

快適な音楽ライフを楽しもうと、Linux(Linux Mint 18.3)のターミナルで動作するYouTubeプレーヤ「mpsyt(mps-youtube)」を実行したところ、以下のようなエラーが出力された。

$ mpsyt
Traceback (most recent call last):
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/pafy/util.py", line 34, in call_gdata
    data = g.opener.open(url).read().decode('utf-8')
  File "/usr/lib/python3.5/urllib/request.py", line 472, in open
    response = meth(req, response)
  File "/usr/lib/python3.5/urllib/request.py", line 582, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python3.5/urllib/request.py", line 510, in error
    return self._call_chain(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.5/urllib/request.py", line 590, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pochy9n/.venv/bin/mpsyt", line 6, in <module>
    from mps_youtube import main
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/mps_youtube/__init__.py", line 8, in <module>
    init.init()
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/mps_youtube/init.py", line 58, in init
    cache.load()
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/mps_youtube/cache.py", line 34, in load
    pafy.load_cache(cached['pafy'])
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/pafy/pafy.py", line 184, in load_cache
    set_categories(newcache.get('categories', {}))
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/pafy/pafy.py", line 171, in set_categories
    catinfo = call_gdata('videoCategories', query)
  File "/home/pochy9n/.venv/lib/python3.5/site-packages/pafy/util.py", line 42, in call_gdata
    raise GdataError(errmsg)
pafy.util.GdataError: Youtube Error 403: Daily Limit Exceeded. 
The quota will be reset at midnight Pacific Time (PT). 
You may monitor your quota usage and adjust limits in the API Console: 
https://console.developers.google.com/apis/api/youtube.googleapis.com/quotas?project=422897895433

上記は「Daily Limit Exceeded」が示す通り、1日で使えるクォータを使い切ったと言う意味のメッセージであるが、pafyと言うライブラリに埋め込まれているGoogle APIキーを世界中で共有しているのだから、発生して当然のエラーであることが分かった。
市井の情報を検索しても、これと決め手となる情報が見つからなかったので、自分で理解した解決策を共有する。

問題点

上記エラーを回避するためには、自分のGoogle APIキーを取得して差し替えれば良いのだが、以下の理由で簡単には差し替えられない。

  • mpsytは実行時に既設のAPIキーで認証するので、その時点で必ずエラーとなって終了する
  • そのため、mpsytのプロンプトでAPIキーを差し替える「set api_key」コマンドを実行できない
  • 「set api_key」に指定したAPIキーは ~/.config/mps-youtube/config に保存されるが、そのconfigはバイナリファイルなので迂闊に変更できない

まさに、にわとりたまご問題が発生している状況である。

解決策

以下の手順で「set api_key」により ~/.config/mps-youtube/config に自分のAPIキーを保存する。
なお、ここではAPIキーの取得方法について説明しないので、別途調べて欲しい。

  • lib/*/site-packages/pafyに移動し cp -a g.py g.py.sav で退避する
  • g.pyのapi_keyの内容を自分のAPIキーに変更する
  • mpsytを実行し、プロンプトで set api_key <自分のAPIキー> を実行する
  • lib/*/site-packages/pafyに移動し mv g.py.sav g.py で復旧する

以上

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?