Skip to content

Instantly share code, notes, and snippets.

@livibetter

livibetter/COPYING

Last active Aug 1, 2019
Embed
What would you like to do?
extract-xml.py

nytbs

Scripts I wrote to extract Wikipedia XML source for New York Times Fiction Best Sellers

Usage

$ ./fix-xml <Wikipedia.xml> <Fixed.xml>
$ ./extract-xml <Fixed.xml>  # generates nytbs.csv
$ ./nytbs nytbs.csv  # generates some files, see Generated Files

Generated Files

most_weeks, most_weeks.csv
List order by most weeks on #1 spot.
c_most_weeks, c_most_weeks.csv
List order by most consecutive weeks on #1 spot.
c_chron, c_chron.csv
List order by dates with the numbers of most consecutive weeks.
most_weeks_heatmap.csv
Heatmap of number of books staying on #1 spot for # weeks by decade.
weeks_books.csv
By decade, combined weeks and number of books, average weeks per book, average books per year.

Some generated files on this Gist.

Copyright

The scripts are licensed under the MIT License.

Copyright (c) 2017 Yu-Jie Lin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/usr/bin/env python3
# Copyright (c) 2017 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# https://en.wikipedia.org/wiki/Special:Export
# Category: "The New York Times Best Seller list"
#
# Note:
# There are many weeks which are labeled as
# Not published due to Newspaper Strike
# unknown; not clear from source
# But some of those weeks have records from http://www.hawes.com/pastlist.htm
# This script respects the Wikipedia and do not adjust with hawes.com's data.
import argparse
import logging as log
import re
from pyquery import PyQuery as pq
LOG_FORMAT = ('%(asctime)s.%(msecs)03d %(levelname)8s '
'%(funcName)s:%(lineno)d: %(message)s')
LOG_DATEFMT = '%H:%M:%S'
log.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATEFMT, level='DEBUG')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('XML')
args = parser.parse_args()
ns = {'namespaces': {'wpe': 'http://www.mediawiki.org/xml/export-0.10/'}}
RE = re.compile('The New York Times (.+) Best Sellers of (\d+)')
MONTHS = ('January|February|March|April|May|June|July|August|September|'
'October|November|December')
# 1942–2015 only has MM DD / 2016 uses {{dts|MM DD}}
# 2017 uses one cell per line
re_d = r'\|\s*(\{{\{{dts\|)?(?P<month>{}) (?P<day>\d+)(\}}\}})?\s*.*'
re_d = re_d.format(MONTHS)
RE_D = re.compile(re_d)
MONTHS = MONTHS.split('|')
RE_ROWSPAN = re.compile(r'...span=[^|]+\|\s*')
RE_LINK = re.compile(r'\[\[(.+\|)?(?P<title>.+?)\]\]')
CSV = '"{}","{}","{}","{}"'
tops = []
d = pq(filename=args.XML, parser='xml', **ns)
pages = d('wpe|page', **ns)
for page in pages.items():
title = page('wpe|title', **ns).text()
m = RE.match(title)
if not m:
log.warning('page title not matched: {}'.format(title))
genre, year = m.groups()
if genre not in ('Fiction', 'Non-Fiction'):
continue
log.info('parsing {} of {}'.format(genre, year))
for line in page('wpe|text', **ns).text().split('\n'):
line = RE_ROWSPAN.sub('', line)
try:
assert('span=' not in line)
except AssertionError:
log.debug(line)
raise
line = line.replace("''", '')
m = RE_D.match(line)
if not m:
continue
month = MONTHS.index(m.group('month')) + 1
day = int(m.group('day'))
date = '{}-{:02d}-{:02d}'.format(year, month, day)
if '||' in line:
ba = line.split('||')
book_title = ba[1]
author = '' if len(ba) < 3 else ba[2]
book_title = RE_LINK.sub('\g<title>', book_title)
book_title = book_title.replace('"', '""').strip()
try:
assert('[' not in book_title)
except AssertionError:
log.debug(line)
log.debug(book_title)
raise
if book_title in ('Slow Waltz in Cedar Bend',
'The Bridges of Madison County'):
author = 'Robert James Waller'
elif book_title == 'Harry Potter and the Chamber of Secrets':
author = 'J. K. Rowling'
author = RE_LINK.sub('\g<title>', author)
author = author.replace('<br>', '')
# fixing author names, checked in top.py
author = author.replace('A.J. Cronin', 'A. J. Cronin')
author = author.replace('Thomas Costain', 'Thomas B. Costain')
author = author.replace('"', '""').strip()
try:
assert('[' not in author)
except AssertionError:
log.debug(line)
log.debug(book_title)
log.debug(author)
raise
if 'Robert Galbraith' in author:
author = 'Robert Galbraith (pseudonym for J.K. Rowling)'
if 'Ben Ames William' in author:
author = 'Ben Ames Williams'
if author:
tops.append(CSV.format(genre, date, book_title, author))
else:
log.debug('no author for book title: {}'.format(book_title))
tops.sort()
log.info('{:,} books in total'.format(len(tops)))
with open('nytbs.csv', 'w') as f:
f.write('"Genre","Date","Title","Author"\n')
f.write('\n'.join(tops))
if __name__ == '__main__':
main()
#!/usr/bin/env python3
# Copyright (c) 2017 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Fixing multiline row to single line
# |-
# |MM DD
# |Title
# |Author
# |-
# to
# |-
# |MM DD||Title||Author
# |-
import argparse
import logging as log
import re
LOG_FORMAT = ('%(asctime)s.%(msecs)03d %(levelname)8s '
'%(funcName)s:%(lineno)d: %(message)s')
LOG_DATEFMT = '%H:%M:%S'
log.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATEFMT, level='DEBUG')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('INXML')
parser.add_argument('OUTXML')
args = parser.parse_args()
with open(args.INXML) as f:
lines = f.readlines()
RE_S = re.compile(r'^\|[-}]\s*$')
lno = 0
with open(args.OUTXML, 'w') as f:
while lno < len(lines):
line = lines[lno]
lno += 1
f.write(line)
m = RE_S.match(line)
if not m:
continue
line = lines[lno]
rline = line.rstrip('\n')
# start of row, find the next row
nno = lno + 1
while nno < len(lines):
nline = lines[nno]
m = RE_S.match(nline)
if m:
break
rline += '|' if nline.startswith('|') else ' '
rline += nline.rstrip('\n')
nno += 1
f.write(rline + '\n')
lno = nno
if __name__ == '__main__':
main()
#!/usr/bin/env python3
# Copyright (c) 2017 Yu-Jie Lin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import argparse
import csv
import logging as log
from itertools import groupby
LOG_FORMAT = ('%(asctime)s.%(msecs)03d %(levelname)8s '
'%(funcName)s:%(lineno)d: %(message)s')
LOG_DATEFMT = '%H:%M:%S'
log.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATEFMT, level='DEBUG')
def write(basename, records, r_dates, n_books):
with open(basename, 'w') as f:
f.write(r_dates + '\n')
f.write(n_books + '\n')
f.write('\n')
with open(basename + '.csv', 'w') as fcsv:
fcsv.write(('"Ranking","Consecutive Weeks","Start","End","Title",'
'"Author"\n'))
for key, weeks, ranking in records:
title, author = key
args = [ranking, len(weeks), weeks[0], weeks[-1], title,
author]
f.write('{:5,d} {:2d} weeks ({}–{}): {} by {}\n'.format(*args))
args[4] = title.replace('"', '""')
args[5] = author.replace('"', '""')
fcsv.write('"{}","{}","{}","{}","{}","{}"\n'.format(*args))
log.info(basename + ' written')
log.info(basename + '.csv written')
def add_ranking(records):
nrs = []
rank = 0
oweeks = None
for n, item in enumerate(records):
key, weeks = item
if oweeks != len(weeks):
rank = n + 1
oweeks = len(weeks)
nrs.append((key, weeks, rank))
return nrs
def calc_consecutive(fiction, r_dates, n_books):
records = {}
okey = (None, None)
weeks = []
for week, title, author in fiction:
key = (title, author)
if key != okey:
if okey != (None, None):
# new top 1
oweeks = records.get(okey, [])
if len(weeks) > len(oweeks):
# new record for the title
records[okey] = weeks
okey = key
weeks = [week]
else:
weeks.append(week)
else:
oweeks = records.get(okey, [])
if len(weeks) > len(oweeks):
# new record for the title
records[okey] = weeks
# checking title with different author
kf = lambda item: item[0][0]
by_title = sorted(records.items(), key=kf)
for k, g in groupby(by_title, key=kf):
books = list(g)
if len(books) == 1:
continue
title = books[0][0][0]
msg = '"{}" has been topped with different authors:'
log.warning(msg.format(title))
for book in books:
author = book[0][1]
log.warning(' {}'.format(author))
kf = lambda item: len(item[1])
c_most_weeks = sorted(records.items(), key=kf, reverse=True)
c_most_weeks = add_ranking(c_most_weeks)
write('c_most_weeks', c_most_weeks, r_dates, n_books)
kf = lambda item: item[1][0]
c_chron = sorted(c_most_weeks, key=kf)
write('c_chron', c_chron, r_dates, n_books)
def calc(fiction, r_dates, n_books):
#######################
# most weeks by title #
#######################
kf = lambda item: (item[1], item[2])
by_title = sorted(fiction, key=kf)
most_weeks = {}
for k, g in groupby(by_title, key=kf):
nweeks = [w[0] for w in g]
most_weeks[k] = nweeks
kf = lambda item: len(item[1])
most_weeks = sorted(most_weeks.items(), key=kf, reverse=True)
most_weeks = add_ranking(most_weeks)
write('most_weeks', most_weeks, r_dates, n_books)
#####################
# heatmap by decade #
#####################
decize = lambda s: '{}s'.format(int(s[:4]) // 10 * 10)
wks_dec = [(len(b[1]), decize(b[1][0])) for b in most_weeks]
kf = lambda item: item[1]
wks_dec.sort(key=kf)
bins_by_dec = {}
weeks_books = {}
max_weeks = len(most_weeks[0][1])
for dec, weeks in groupby(wks_dec, key=kf):
bins = [0] * max_weeks
weeks = list(weeks)
total_books = len(weeks)
weeks = sorted(w[0] for w in weeks)
total_weeks = sum(weeks)
for w, count in groupby(weeks):
bins[w - 1] = sum(1 for c in count)
bins_by_dec[dec] = bins
weeks_books[dec] = (total_weeks, total_books)
fn = 'most_weeks_heatmap.csv'
with open(fn, 'w') as f:
nums = ['"{}"'.format(w) for w in range(1, max_weeks + 1)]
hline = '"Decade",' + ','.join(nums)
f.write(hline + '\n')
for dec in sorted(bins_by_dec.keys()):
bins = bins_by_dec[dec]
f.write('"{}",'.format(dec))
f.write(','.join(['"{}"'.format(b) for b in bins]))
f.write('\n')
log.info(fn + ' written')
fn = 'weeks_books.csv'
with open(fn, 'w') as f:
hline = ('"Decade","Weeks","Books","Average Weeks/Book",'
'"Average Books/Year"\n')
f.write(hline)
fmt = ','.join(['"{}"'] * 5) + '\n'
for dec in sorted(weeks_books.keys()):
weeks, books = weeks_books[dec]
f.write(fmt.format(dec, weeks, books, weeks / books, books / 10))
log.info(fn + ' written')
def main():
parser = argparse.ArgumentParser()
parser.add_argument('CSV')
args = parser.parse_args()
fiction = []
with open(args.CSV) as f:
cr = csv.reader(f)
for r in cr:
if r[0] != 'Fiction':
continue
fiction.append(r[1:])
msg = '{:,} weekly lists ({}–{}) published'
r_dates = msg.format(len(fiction), fiction[0][0], fiction[-1][0])
log.info(r_dates)
kf = lambda item: '{} by {}'.format(*item[1:])
books = sum(1 for b in groupby(sorted(fiction, key=kf), key=kf))
n_books = '{:5,} books topped the lists'.format(books)
log.info(n_books)
calc_consecutive(fiction, r_dates, n_books)
calc(fiction, r_dates, n_books)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment