#!/usr/bin/env python

"""
Class to run the Scraper for www.TED.com.
"""
__title__ = 'ted'
__author__ = 'Rashiq Ahmad'
__license__ = 'GPLv3'

import argparse
from scraper.webscraper import Scraper

class App():


	def __init__(self):
		self.parse_commandline_arguments()
		self.run()


	def parse_commandline_arguments(self):
		
		parser = argparse.ArgumentParser(description='Scrape www.TED.com')

		parser.add_argument('--metadata', '-m', action='store_true',
			help="Download the meta data for all TED talks and dump it in a json file")
		parser.add_argument('--subs', '-s', action='store_true', 
			help="Download Subtitles") 
		parser.add_argument('--video', '-v', action='store_true', 
			help="Download the TED videos in mp4")
		parser.add_argument('--encode', '-e', action='store_true', 
			help="Encode the mp4 videos to webm")
		parser.add_argument('--thumbnail', '-t', action='store_true', 
			help="Resize thumbnails in the build directory")
		parser.add_argument('--render', '-r', action='store_true', 
			help="Render HTML pages for the videos")
		parser.add_argument('--zim', '-z', action='store_true', 
			help="Create the ZIM files")

		self.args = vars(parser.parse_args())


	def run(self):
		scraper = Scraper()

		if not self.args['metadata'] and not self.args['render'] \
			and not self.args['video'] and not self.args['subs']\
			and not self.args['encode'] and not self.args['thumbnail']\
			and not self.args['zim']:
			scraper.extract_all_video_links()
			scraper.dump_data()
			scraper.download_subtitles()
			scraper.download_video_data()
			scraper.render_welcome_page()
			scraper.render_video_pages()
			scraper.copy_files_to_rendering_directory()
			scraper.generate_category_data()
			scraper.encode_videos()
			scraper.resize_thumbnails()
			scraper.create_zims()
		else:
			if self.args['metadata']:
				scraper.extract_all_video_links()
				scraper.dump_data()

			if self.args['video']:
				scraper.download_video_data()

			if self.args['render']:
				scraper.render_welcome_page()
				scraper.render_video_pages()
				scraper.copy_files_to_rendering_directory()
				scraper.generate_category_data()

			if self.args['subs']:
				scraper.download_subtitles()

			if self.args['encode']:
				scraper.encode_videos()

			if self.args['thumbnail']:
				scraper.resize_thumbnails()

			if self.args['zim']:
				scraper.create_zims()


if __name__ == '__main__':
	App()
