dosage/scripts/keenspot.py

141 lines
4.1 KiB
Python
Raw Normal View History

2013-03-11 21:03:17 +00:00
#!/usr/bin/env python
2016-04-12 22:52:16 +00:00
# -*- coding: utf-8 -*-
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
2014-01-05 15:50:57 +00:00
# Copyright (C) 2012-2014 Bastian Kleineidam
2016-04-12 22:52:16 +00:00
# Copyright (C) 2015-2016 Tobias Gruetzmacher
2013-03-11 21:03:17 +00:00
"""
Script to get a list of KeenSpot comics and save the info in a
JSON file for further processing.
"""
2016-04-12 22:52:16 +00:00
from __future__ import absolute_import, division, print_function
2013-05-22 20:29:03 +00:00
import codecs
2013-03-11 21:03:17 +00:00
import re
import sys
import os
2016-04-12 22:52:16 +00:00
2013-03-11 21:03:17 +00:00
import requests
2016-04-12 22:52:16 +00:00
sys.path.append(os.path.join(os.path.dirname(__file__), "..")) # noqa
from dosagelib.util import get_page, tagre, check_robotstxt
from dosagelib.scraper import get_scrapers
2016-04-12 22:52:16 +00:00
from scriptutil import (contains_case_insensitive, save_result, load_result,
truncate_name, format_name)
2013-03-11 21:03:17 +00:00
json_file = __file__.replace(".py", ".json")
2016-04-12 22:52:16 +00:00
2013-03-11 21:03:17 +00:00
url_matcher = re.compile(
tagre("td", "onmouseover", r'([^"]+)') +
tagre("a", "href", r'([^"]+\.keenspot\.com/)[^"]*') +
r"(?:<b>)?([^<]+)(?:</b>)?</a>"
)
2016-04-12 22:52:16 +00:00
2013-03-11 21:03:17 +00:00
# names of comics to exclude
exclude_comics = [
"BrawlintheFamily", # non-standard navigation
"CrowScare", # non-standard navigation
"Dreamless", # non-standard navigation
"EV", # non-standard navigation
"Exposure", # non-standard navigation
"Flipside", # non-standard navigation
"HerobyNight", # non-standard navigation
2013-07-09 20:21:12 +00:00
"JadeWarriors", # non-standard navigation
"LastBlood", # non-standard navigation
"MysticRevolution", # non-standard navigation
"NoRoomForMagic", # non-standard navigation
"PunchanPie", # non-standard navigation
"RoadWaffles", # non-standard navigation
"Shadowbinders", # non-standard navigation
"ShockwaveDarkside", # non-standard navigation
"Supernovas", # non-standard navigation
"Twokinds", # non-standard navigation
"WisdomofMoo", # non-standard navigation
"Yirmumah", # non-standard navigation
"YouDamnKid", # non-standard navigation
2013-03-11 21:03:17 +00:00
]
2016-04-12 22:52:16 +00:00
2013-03-11 21:03:17 +00:00
# links to last valid strips
url_overrides = {
}
2016-04-12 22:52:16 +00:00
2013-03-11 21:03:17 +00:00
def handle_url(url, session, res):
"""Parse one search result page."""
print("Parsing", url, file=sys.stderr)
try:
2016-04-12 22:52:16 +00:00
data = get_page(url, session).text
2013-03-11 21:03:17 +00:00
except IOError as msg:
print("ERROR:", msg, file=sys.stderr)
return
for match in url_matcher.finditer(data):
comicurl = match.group(2)
2016-04-12 22:52:16 +00:00
name = format_name(match.group(3))
2013-03-11 21:03:17 +00:00
if name in exclude_comics:
continue
if contains_case_insensitive(res, name):
# we cannot handle two comics that only differ in case
print("INFO: skipping possible duplicate", repr(name), file=sys.stderr)
continue
try:
if "/d/" not in comicurl:
2016-04-12 22:52:16 +00:00
check_robotstxt(comicurl + "d/", session)
else:
check_robotstxt(comicurl, session)
except IOError:
2013-11-07 06:28:47 +00:00
print("INFO: robots.txt denied for keenspot", repr(name))
2013-03-11 21:03:17 +00:00
continue
res[name] = comicurl
2013-03-11 21:03:17 +00:00
def get_results():
"""Parse all search result pages."""
# store info in a dictionary {name -> shortname}
res = {}
session = requests.Session()
base = 'http://keenspot.com/'
handle_url(base, session, res)
save_result(res, json_file)
def has_comic(name):
"""Check if comic name already exists."""
names = [
("Creators/%s" % name).lower(),
("GoComics/%s" % name).lower(),
("ComicGenesis/%s" % name).lower(),
]
for scraperobj in get_scrapers():
lname = scraperobj.name.lower()
2013-03-11 21:03:17 +00:00
if lname in names:
return True
return False
def print_results(args):
"""Print all comics."""
2013-05-22 20:29:03 +00:00
min_comics, filename = args
with codecs.open(filename, 'a', 'utf-8') as fp:
for name, entry in sorted(load_result(json_file).items()):
if name in exclude_comics:
continue
url = entry
2013-05-22 20:29:03 +00:00
if has_comic(name):
prefix = u'#'
else:
prefix = u''
name = truncate_name(name)
fp.write(u"%sadd(%r, %r)\n" % (
prefix, str(name), str(url))
2013-05-22 20:29:03 +00:00
)
2013-03-11 21:03:17 +00:00
if __name__ == '__main__':
if len(sys.argv) > 1:
print_results(sys.argv[1:])
else:
get_results()