2012-11-28 17:15:12 +00:00
|
|
|
#!/usr/bin/env python
|
2016-04-11 22:36:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-04-12 22:52:16 +00:00
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
|
|
|
# Copyright (C) 2015-2016 Tobias Gruetzmacher
|
2012-11-28 17:15:12 +00:00
|
|
|
"""
|
2016-04-12 22:52:16 +00:00
|
|
|
Script to get a list of gocomics and save the info in a JSON file for further
|
|
|
|
processing.
|
2012-11-28 17:15:12 +00:00
|
|
|
"""
|
2016-04-11 22:36:33 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2013-05-22 20:29:03 +00:00
|
|
|
import codecs
|
2012-11-28 17:15:12 +00:00
|
|
|
import sys
|
|
|
|
import os
|
2016-04-12 22:52:16 +00:00
|
|
|
|
2013-02-12 20:53:57 +00:00
|
|
|
import requests
|
2016-04-11 22:36:33 +00:00
|
|
|
from lxml import html
|
2012-11-28 17:15:12 +00:00
|
|
|
|
2016-04-11 22:36:33 +00:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), "..")) # noqa
|
|
|
|
from dosagelib.util import get_page
|
|
|
|
from scriptutil import contains_case_insensitive, format_name, save_result, load_result, truncate_name
|
2012-11-28 17:15:12 +00:00
|
|
|
|
2016-04-11 22:36:33 +00:00
|
|
|
json_file = __file__.replace(".py", ".json")
|
2012-11-28 17:15:12 +00:00
|
|
|
|
2012-11-29 05:46:58 +00:00
|
|
|
# names of comics to exclude
|
|
|
|
exclude_comics = [
|
2016-04-11 22:36:33 +00:00
|
|
|
# "coming soon"
|
|
|
|
"Angryprogrammer",
|
|
|
|
"Guinness",
|
|
|
|
"Jabberwoncky",
|
|
|
|
"RandysRationale"
|
|
|
|
"SignsOfOurTimes",
|
|
|
|
"TheGagwriter",
|
|
|
|
"Yaoyao",
|
|
|
|
|
|
|
|
# duplicate
|
|
|
|
"SaturdayMorningBreakfastCereal",
|
2012-11-29 05:46:58 +00:00
|
|
|
]
|
|
|
|
|
2012-11-28 17:15:12 +00:00
|
|
|
|
2013-02-12 20:53:57 +00:00
|
|
|
def handle_url(url, session, res):
|
2012-11-28 17:15:12 +00:00
|
|
|
"""Parse one search result page."""
|
|
|
|
print("Parsing", url, file=sys.stderr)
|
|
|
|
try:
|
2016-04-11 22:36:33 +00:00
|
|
|
data = html.document_fromstring(get_page(url, session).text)
|
2012-11-28 17:15:12 +00:00
|
|
|
except IOError as msg:
|
|
|
|
print("ERROR:", msg, file=sys.stderr)
|
|
|
|
return
|
2016-04-11 22:36:33 +00:00
|
|
|
|
|
|
|
for comiclink in data.cssselect('a.alpha_list'):
|
|
|
|
link = comiclink.attrib['href']
|
|
|
|
name = format_name(comiclink.text)
|
2012-11-28 17:15:12 +00:00
|
|
|
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)
|
2012-11-28 17:15:12 +00:00
|
|
|
continue
|
2016-04-11 22:36:33 +00:00
|
|
|
res[name] = link
|
2012-11-28 17:15:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_results():
|
|
|
|
"""Parse all search result pages."""
|
2016-04-11 22:36:33 +00:00
|
|
|
# store info in a dictionary {name -> uri}
|
2012-11-28 17:15:12 +00:00
|
|
|
res = {}
|
2013-02-12 20:53:57 +00:00
|
|
|
session = requests.Session()
|
|
|
|
handle_url('http://www.gocomics.com/features', session, res)
|
2015-04-18 00:04:31 +00:00
|
|
|
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)
|
2012-11-28 17:15:12 +00:00
|
|
|
|
|
|
|
|
2016-04-11 22:36:33 +00:00
|
|
|
def first_lower(x):
|
|
|
|
return x[0].lower()
|
|
|
|
|
|
|
|
|
2012-11-28 17:15:12 +00:00
|
|
|
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:
|
2016-04-11 22:36:33 +00:00
|
|
|
data = load_result(json_file)
|
|
|
|
for name, uri in sorted(data.items(), key=first_lower):
|
2013-05-22 20:29:03 +00:00
|
|
|
if name in exclude_comics:
|
2015-04-18 00:04:31 +00:00
|
|
|
print("Excluded " + name)
|
2013-05-22 20:29:03 +00:00
|
|
|
continue
|
2016-04-11 22:36:33 +00:00
|
|
|
fp.write(u"\n\nclass GC%s(_GoComics%s):\n path = %r\n" % (
|
|
|
|
truncate_name(name), 'Es' if 'espanol/' in uri else '',
|
|
|
|
uri[1:]))
|
2012-11-28 17:15:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
print_results(sys.argv[1:])
|
|
|
|
else:
|
|
|
|
get_results()
|