Save some intermediate JSONs to files when running in DEBUG mode

This commit is contained in:
Yuri Bochkarev 2016-06-08 20:57:40 +03:00
parent eedcb7ad6d
commit 2d527c391b
2 changed files with 24 additions and 2 deletions

View file

@ -69,7 +69,7 @@ from .define import (CLASS_URL, ABOUT_URL, PATH_CACHE,
OPENCOURSE_CONTENT_URL)
from .downloaders import get_downloader
from .utils import (clean_filename, get_anchor_format, mkdir_p, fix_url,
decode_input, BeautifulSoup)
decode_input, BeautifulSoup, is_debug_run)
from .network import get_page, get_page_and_url
from .api import CourseraOnDemand, OnDemandCourseMaterialItems
@ -310,6 +310,7 @@ def parse_on_demand_syllabus(session, page, reverse=False, intact_fnames=False,
"""
dom = json.loads(page)
course_name = dom['slug']
logging.info('Parsing syllabus of on-demand course. '
'This may take some time, be patient ...')
@ -317,7 +318,13 @@ def parse_on_demand_syllabus(session, page, reverse=False, intact_fnames=False,
json_modules = dom['courseMaterial']['elements']
course = CourseraOnDemand(session=session, course_id=dom['id'])
ondemand_material_items = OnDemandCourseMaterialItems.create(
session=session, course_name=dom['slug'])
session=session, course_name=course_name)
if is_debug_run():
with open('%s-syllabus-raw.json' % course_name, 'w') as file_object:
json.dump(dom, file_object, indent=4)
with open('%s-course-material-items.json' % course_name, 'w') as file_object:
json.dump(ondemand_material_items._items, file_object, indent=4)
for module in json_modules:
module_slug = module['slug']
@ -1026,6 +1033,10 @@ def download_on_demand_class(args, class_name):
args.subtitle_language,
args.video_resolution)
if is_debug_run():
with open('%s-syllabus-parsed.json' % class_name, 'w') as file_object:
json.dump(modules, file_object, indent=4)
downloader = get_downloader(session, class_name, args)
# obtain the resources

View file

@ -10,6 +10,7 @@ import random
import re
import string
import sys
import logging
import six
from bs4 import BeautifulSoup as BeautifulSoup_
@ -48,6 +49,16 @@ else:
return x
def is_debug_run():
"""
Check whether we're running with DEBUG loglevel.
@return: True if running with DEBUG loglevel.
@rtype: bool
"""
return logging.getLogger().isEnabledFor(logging.DEBUG)
def random_string(length):
"""
Return a pseudo-random string of specified length.