-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce utils.py module to store small helpers.
- Loading branch information
1 parent
e2cd6f4
commit 57da2b2
Showing
5 changed files
with
47 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# vim: ai ts=4 sts=4 et sw=4 nu | ||
|
||
import pkg_resources | ||
from bs4 import BeautifulSoup | ||
|
||
|
||
def get_version(): | ||
return pkg_resources.get_distribution("warc2zim").version | ||
|
||
|
||
def get_record_url(record): | ||
"""Check if record has url converted from POST/PUT, and if so, use that | ||
otherwise return the target url""" | ||
if hasattr(record, "urlkey"): | ||
return record.urlkey | ||
return record.rec_headers["WARC-Target-URI"] | ||
|
||
|
||
def get_record_mime_type(record): | ||
if record.http_headers: | ||
# if the record has HTTP headers, use the Content-Type from those | ||
# (eg. 'response' record) | ||
content_type = record.http_headers["Content-Type"] | ||
else: | ||
# otherwise, use the Content-Type from WARC headers | ||
content_type = record.rec_headers["Content-Type"] | ||
|
||
mime = content_type or "" | ||
return mime.split(";")[0] | ||
|
||
|
||
def parse_title(content): | ||
try: | ||
soup = BeautifulSoup(content, "html.parser") | ||
return soup.title.text or "" | ||
except Exception: | ||
return "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters