dosage/scripts/gocomics.py

89 lines
3 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2014-01-05 15:50:57 +00:00
# Copyright (C) 2012-2014 Bastian Kleineidam
"""
2012-12-12 16:41:29 +00:00
Script to get a list of gocomics and save the info in a JSON file for further processing.
"""
from __future__ import print_function
2013-05-22 20:29:03 +00:00
import codecs
import re
import sys
import os
2013-02-12 20:53:57 +00:00
import requests
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from dosagelib.util import tagre, getPageContent, asciify, unescape
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")
#<a href="/shortname" class="alpha_list updated">name</a>
url_matcher = re.compile(tagre("a", "href", r'(/[^"]+)', after="alpha_list") + r"([^<]+)</a>")
2012-11-29 05:46:58 +00:00
# names of comics to exclude
exclude_comics = [
"Angryprogrammer", # unavailable
"Complex", # "coming soon"
"Guinness", # "coming soon"
"Jabberwoncky", # "coming soon"
"KickyBrand", # unavailable
"Penmanship", # unavailable
"RandysRationale", # "coming soon"
"SaturdayMorningBreakfastCereal", # duplicate
"SignsOfOurTimes", # "coming soon"
"TheGagwriter", # "coming soon"
"Yaoyao", # "coming soon"
2012-11-29 05:46:58 +00:00
]
2013-02-12 20:53:57 +00:00
def handle_url(url, session, res):
"""Parse one search result page."""
print("Parsing", url, file=sys.stderr)
try:
data = getPageContent(url, session)
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
2013-03-12 19:47:11 +00:00
print("INFO: skipping possible duplicate", repr(name), file=sys.stderr)
continue
res[name] = shortname
def get_results():
"""Parse all search result pages."""
# store info in a dictionary {name -> shortname}
res = {}
2013-02-12 20:53:57 +00:00
session = requests.Session()
handle_url('http://www.gocomics.com/features', session, res)
handle_url('http://www.gocomics.com/explore/espanol', session, res)
2013-02-12 20:53:57 +00:00
handle_url('http://www.gocomics.com/explore/editorial_list', session, res)
handle_url('http://www.gocomics.com/explore/sherpa_list', session, res)
2012-12-19 19:42:53 +00:00
save_result(res, json_file)
def print_results(args):
"""Print all comics that have at least the given number of minimum comic strips."""
2013-05-22 20:29:03 +00:00
min_comics, filename = args
with codecs.open(filename, 'a', 'utf-8') as fp:
for name, shortname in sorted(load_result(json_file).items()):
if name in exclude_comics:
print("Excluded " + name)
2013-05-22 20:29:03 +00:00
continue
fp.write(u"add(%r, %r)\n" % (
str(truncate_name(name)), str(shortname))
)
if __name__ == '__main__':
if len(sys.argv) > 1:
print_results(sys.argv[1:])
else:
get_results()