-
-
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
6fa485b
commit 94a9ce9
Showing
3 changed files
with
42 additions
and
34 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
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 "" |