2015-07-29 03:48:33 +00:00
|
|
|
"""
|
|
|
|
|
main.py
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import multiprocessing
|
|
|
|
|
import importlib
|
|
|
|
|
import os.path
|
|
|
|
|
import timeit
|
|
|
|
|
|
|
|
|
|
import cProfile
|
|
|
|
|
|
|
|
|
|
import tsscraper
|
|
|
|
|
|
|
|
|
|
class Application(object):
|
|
|
|
|
thread_count = 8
|
|
|
|
|
|
|
|
|
|
threads = None
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
target_directory = None
|
|
|
|
|
target_exporter = None
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
def print_usage(self):
|
2016-04-21 22:32:47 +00:00
|
|
|
print("Usage: '%s <exporter> <output directory> <target directories...>'" % sys.argv[0])
|
2015-07-29 03:48:33 +00:00
|
|
|
print("Or: '%s exporters' for a list of known exporters." % sys.argv[0])
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
def get_available_exporters(self):
|
|
|
|
|
exporters = { }
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
for root, dirs, files in os.walk("exporters"):
|
|
|
|
|
for filename in files:
|
|
|
|
|
module_name, extension = os.path.splitext(filename)
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
if (module_name == "__init__"):
|
|
|
|
|
continue
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
try:
|
|
|
|
|
module = importlib.import_module('exporters.%s' % (module_name))
|
|
|
|
|
exporters[module_name] = module
|
|
|
|
|
except ImportError as e:
|
|
|
|
|
print(e)
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
return exporters
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
def main(self):
|
|
|
|
|
"""
|
|
|
|
|
The main entry point of the application. This is equivalent to
|
|
|
|
|
the main() method in C and C++.
|
|
|
|
|
"""
|
|
|
|
|
if (len(sys.argv) < 2):
|
|
|
|
|
self.print_usage()
|
|
|
|
|
return
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
exporters = self.get_available_exporters()
|
2016-04-21 22:32:47 +00:00
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
if (sys.argv[1] == "exporters"):
|
|
|
|
|
print("Available Exporters: ")
|
2016-04-21 22:32:47 +00:00
|
|
|
|
|
|
|
|
for exporter in exporters:
|
2015-07-29 03:48:33 +00:00
|
|
|
print("\t- %s" % exporter)
|
|
|
|
|
print("\t- None")
|
2016-04-21 22:32:47 +00:00
|
|
|
return
|
|
|
|
|
elif(len(sys.argv) < 4):
|
2015-07-29 03:48:33 +00:00
|
|
|
self.print_usage()
|
|
|
|
|
return
|
2016-04-21 22:32:47 +00:00
|
|
|
|
|
|
|
|
self.target_directory = sys.argv[3]
|
|
|
|
|
self.output_directory = sys.argv[2]
|
2015-07-29 03:48:33 +00:00
|
|
|
self.target_exporter = sys.argv[1]
|
|
|
|
|
self.run()
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
exporter = None
|
|
|
|
|
if (self.target_exporter.lower() != "none"):
|
|
|
|
|
exporters = self.get_available_exporters()
|
|
|
|
|
try:
|
|
|
|
|
exporter = exporters[self.target_exporter]
|
|
|
|
|
except KeyError as e:
|
|
|
|
|
print("Error: No such exporter '%s'." % self.target_exporter)
|
|
|
|
|
self.print_usage()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
scraper = tsscraper.TSScraper(self.target_directory, self.thread_count)
|
|
|
|
|
results = scraper.process()
|
|
|
|
|
|
2016-04-21 22:32:47 +00:00
|
|
|
# Init the exporter
|
|
|
|
|
if (exporter is not None):
|
|
|
|
|
# Ensure that the output directory at least exists
|
|
|
|
|
os.mkdir(self.output_directory)
|
|
|
|
|
|
|
|
|
|
output = exporter.Exporter(results, self.target_directory)
|
|
|
|
|
output.write(self.output_directory)
|
|
|
|
|
|
2015-07-29 03:48:33 +00:00
|
|
|
if __name__ == "__main__":
|
2016-04-21 22:32:47 +00:00
|
|
|
print("Operation Completion-----------------------\n%f Seconds" % timeit.timeit("Application().main()", number=1, setup="from __main__ import Application"))
|