-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamically extract the download URL from the feed #47
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import feedparser | ||
|
||
from typing import Union | ||
from lxml import html | ||
|
||
|
||
default_feed = "http://www.country-files.com/category/big-cty/feed/" | ||
|
@@ -172,13 +173,17 @@ def update(self) -> bool: | |
|
||
with tempfile.TemporaryDirectory() as temp: | ||
path = pathlib.PurePath(temp) | ||
dl_url = f'http://www.country-files.com/bigcty/download/{update_date[:4]}/bigcty-{update_date}.zip' # TODO: Issue #10 | ||
page = session.get(update_url) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably check for status before trying to parse the content There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed |
||
if page.status_code != 200: | ||
raise Exception(f"Unable to find and download bigcty-{update_date}.zip") | ||
tree = html.fromstring(page.content) | ||
link_urls = tree.xpath("//a[contains(@href,'zip')]/@href") | ||
if len(link_urls) == 0: | ||
raise Exception(f"Unable to find link to bigcty-{update_date}.zip") | ||
dl_url = link_urls[0] | ||
rq = session.get(dl_url) | ||
if rq.status_code == 404: | ||
dl_url = f'http://www.country-files.com/bigcty/download/bigcty-{update_date}.zip' | ||
rq = session.get(dl_url) | ||
if rq.status_code != 200: | ||
raise Exception(f"Unable to find and download bigcty-{update_date}.zip") | ||
if rq.status_code != 200: | ||
raise Exception(f"Unable to find and download bigcty-{update_date}.zip") | ||
with open(path / 'cty.zip', 'wb+') as file: | ||
file.write(rq.content) | ||
zipfile.ZipFile(file).extract('cty.dat', path=str(path)) # Force cast as str because mypy | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ sphinx | |
# Dependencies | ||
feedparser | ||
requests | ||
lxml | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is lxml not needed as a normal dependency? This file is only used for development, so |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be good to keep that URL as a fallback, like the other URL format already is