dosage/scripts/universal.py

98 lines
3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2013-01-09 21:21:19 +00:00
# Copyright (C) 2012-2013 Bastian Kleineidam
"""
Script to get universal comics and save the info in a JSON file for further processing.
"""
from __future__ import print_function
import re
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from dosagelib.util import getPageContent, asciify, unescape
from dosagelib.scraper import get_scrapers
2013-01-09 21:20:03 +00:00
from scriptutil import contains_case_insensitive, capfirst, save_result, load_result, truncate_name
json_file = __file__.replace(".py", ".json")
#<li><a href="/comics/strip/9chickweedlane">9 Chickweed Lane</a>
url_matcher = re.compile(r'<li><a href="(/comics/[^"]+)">([^<]+)</a>')
2012-11-29 05:46:58 +00:00
# names of comics to exclude
exclude_comics = [
2012-12-05 20:52:52 +00:00
"BusinessAndFinance", # not a comic
"ComicPanel", # not a comic
"ComicsAZ", # not a comic
"ComicStrip", # not a comic
"Espaol", # not a comic
"Family", # not a comic
"ForKids", # not a comic
"JamesBond", # not a comic
"Men", # not a comic
"NEA", # not a comic
2012-12-13 20:05:27 +00:00
"PeanutsPortuguese", # not found
2012-12-05 20:52:52 +00:00
"Pets", # not a comic
"SundayOnly", # not a comic
"WebExclusive", # not a comic
"Women", # not a comic
2012-11-29 05:46:58 +00:00
]
def handle_url(url, res):
"""Parse one search result page."""
print("Parsing", url, file=sys.stderr)
try:
data, baseUrl = getPageContent(url)
except IOError as msg:
print("ERROR:", msg, file=sys.stderr)
return
for match in url_matcher.finditer(data):
shortname = match.group(1)
name = unescape(match.group(2))
name = asciify(name.replace('&', 'And').replace('@', 'At'))
2012-12-12 16:41:29 +00:00
name = capfirst(name)
2012-11-29 05:46:58 +00:00
if name in exclude_comics:
continue
if contains_case_insensitive(res, name):
# we cannot handle two comics that only differ in case
2012-12-19 19:42:53 +00:00
print("INFO: skipping possible duplicate", name, file=sys.stderr)
continue
res[name] = shortname
def get_results():
"""Parse all search result pages."""
# store info in a dictionary {name -> shortname}
res = {}
handle_url('http://www.universaluclick.com/comics/list', res)
2012-12-19 19:42:53 +00:00
save_result(res, json_file)
def has_comic(name):
2013-01-09 21:26:00 +00:00
"""Check if comic name already exists."""
cname = ("Creators/%s" % name).lower()
gname = ("GoComics/%s" % name).lower()
for scraperclass in get_scrapers():
lname = scraperclass.get_name().lower()
if lname == cname or lname == gname:
return True
return False
2012-11-29 05:46:58 +00:00
def print_results(args):
"""Print all comics that have at least the given number of minimum comic strips."""
2013-01-09 21:20:03 +00:00
for name, shortname in sorted(load_result(json_file).items()):
2012-11-29 05:46:58 +00:00
if name in exclude_comics:
continue
if has_comic(name):
prefix = '#'
else:
prefix = ''
2013-01-09 21:20:03 +00:00
print("%sadd(%r, %r)" % (prefix, str(truncate_name(name)), str(shortname)))
if __name__ == '__main__':
if len(sys.argv) > 1:
print_results(sys.argv[1:])
else:
get_results()