unbundle requests and use requests.get instead of urllib.urlretrieve#5138
unbundle requests and use requests.get instead of urllib.urlretrieve#5138jamalex merged 3 commits intolearningequality:0.16.xfrom
Conversation
| # (this is done in this file because putting it in __init__.py causes circular imports, | ||
| # and putting it in urls.py doesn't get loaded by management commands) | ||
| base_headers = requests.defaults.defaults["base_headers"] | ||
| base_headers["User-Agent"] = ("ka-lite/%s " % VERSION) + base_headers["User-Agent"] |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
This is where the requests library is used currently, I don't see any reason of concern, but should we change the User-Agent on any of these requests @jamalex ? |
|
Confirmed working on Raspberry Pi with m2crypto 0.21.1 installed as a dependency of ka-lite-raspberry-pi. |
|
|
||
| response = None | ||
| last_error = None | ||
| for retries in range(1, 1 + max_retries): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Most of the ones you list there don't seem important (as they're not hitting our central server, so we won't be getting logs with the user agent in it for those), but that doesn't seem to be an exhaustive list. Here are some that are more important: Might be good to pull out this header calculation logic into a reusable function: |
| "Content-Length" not in response or | ||
| not str(len(open(thumb_filepath, "rb").read())) == response["Content-Length"]): | ||
| "content-length" not in response.headers or | ||
| not len(open(thumb_filepath, "rb").read()) == int(response.headers['content-length'])): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
They already did when kalite was monkey-patching requests :) |
|
Thanks for searching through fle_utils btw, adding that change as well. |
Kind of soft dependency; implicit, and optional. They would still function fine without kalite. More like a "hook" style "dependency". :D |
Cool -- there were a bunch of other uses of |
If the intention was to have correct user agent stats, you would be failing but much more severely than a circular import, you'd have wrong stats and think they were right! So all this functionality really depends on the monkey patch firing. |
|
Ok, looks good to me! Merging now, but please make sure data syncing also still works (with a basic check -- registering and creating a local record and syncing it up, with no errors), in addition to the video downloading, as both depend on requests. |
|
Tested:
|
|
💥 👍 ⚡ |

Summary
Fixes issues with m2crypto not able to handle https connections.
TODO
If not all TODOs are marked, this PR is considered WIP (work in progress)
Reviewer guidance
If there's any concern about including this PR in 0.16.6, please state it in the discussion, but I would caution that the old download method was probably less reliable than using the latest version of
requests@jamalexIssues addressed
#5126