Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions ctyparser/bigcty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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
Copy link
Member

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

page = session.get(update_url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably check for status before trying to parse the content

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down
1 change: 1 addition & 0 deletions devrequirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ sphinx
# Dependencies
feedparser
requests
lxml
Copy link
Member

Choose a reason for hiding this comment

The 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 lxml should also be added to setup.py (setup.py being what defines dependencies to install when someone install ctyparser)