Created
April 7, 2014 12:55
-
-
Save ntlk/10019785 to your computer and use it in GitHub Desktop.
Counting appearances of Hannibal Lecter in films the easy way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import json | |
import datetime | |
from termcolor import colored | |
print('Hannibal Lecter appearance counter') | |
title = input('Please enter the film title: ') | |
occurrences = [] | |
recording = False | |
current_start_time = '' | |
def start_recording(start_time): | |
global recording | |
global current_start_time | |
current_start_time = start_time.rstrip() | |
print(colored('Recorded start time as %s.' % current_start_time, 'blue')) | |
print('Type in the scene end time to finish.') | |
recording = True | |
def stop_recording(end_time): | |
global recording | |
global current_start_time | |
time_total = calculate_ocurrence_legth(current_start_time, end_time) | |
save_occurrence_of_length(time_total) | |
recording = False | |
def calculate_ocurrence_legth(start_time, end_time): | |
start_time = convert_time_to_datetime(start_time) | |
end_time = convert_time_to_datetime(end_time) | |
time_total = end_time - start_time | |
print(colored('Scene length recorded as %s.' % time_total, 'green')) | |
return time_total.total_seconds() | |
def convert_time_to_struct(given_time): | |
return time.strptime('2014-04-07 ' + given_time.rstrip(), "%Y-%m-%d %M:%S") | |
def convert_time_to_datetime(given_time): | |
return datetime.datetime.fromtimestamp(time.mktime(convert_time_to_struct(given_time))) | |
def save_occurrence_of_length(length): | |
occurrences.append(length) | |
def error_message(): | |
if recording: | |
print(colored('Sorry, you need to add scene finish time first.', 'red')) | |
else: | |
print(colored('Sorry, didn\'t understand that. Add start time or type \'the end\' to finish.')) | |
def finish(): | |
total = sum(occurrences) | |
count = len(occurrences) | |
print_to_file(total, count, occurrences) | |
print('Tallying up the results!') | |
print("Registered %(count)s occurrences, totalling %(total)s seconds." % locals()) | |
sys.exit() | |
def print_to_file(total, count, items): | |
f = open(title_to_filename(title), 'a') | |
json.dump({'total': total, 'count': count, 'items': items}, f, indent=2) | |
f.close() | |
def title_to_filename(title): | |
title = title.replace(' ', '_') | |
date = datetime.datetime.now().strftime("%Y%m%d%H%M%S") | |
filename = "%(title)s_%(date)s.txt" % locals() | |
return filename | |
print('Type in the scene start time to begin. Type \'the end\' when done.') | |
for line in sys.stdin: | |
if line == 'the end\n': | |
if recording: | |
error_message() | |
else: | |
finish() | |
else: | |
if recording: | |
stop_recording(line) | |
else: | |
start_recording(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment