Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Author: ryandaniels
- #Email: find_ryan_daniels@yahoo.com
- #Feel free to use/ redist/ modify/ steal from.
- #If you are a smarter man/ woman/ child than I, you will probably quickly
- #realize that this software can list all sorts of things beyond just movies.
- #Therefore, I encourage you to ignore the naming in this script and use it for
- #whatever you want
- #Note that I use the term "movie" in this software not because I am an inbred
- #hick who makes a point of never watching a "film", but that this list will
- #contain movies, and calling a movie a film is not something I would
- #cognitivley do /refinement
- import os
- import sys
- import re
- import time
- import string
- def escaped_split(target, token):
- splits= token.split(target)
- final_splits= []
- glue_together= ""
- for split in splits:
- if(split== ""):
- pass
- elif(glue_together!= ""):
- final_splits.append(glue_together+ target+ split)
- glue_together= ""
- elif(split[-1]== "\\"):
- glue_together= split
- else:
- final_splits.append(split)
- return final_splits
- def sanitize(token):
- return token.replace(",", "\,").replace(":", "\:").replace(";", "\;")
- def dirtify(token):
- return token.replace("\,", ",").replace("\:", ":").replace("\;", ";")
- def save(movie_list):
- movie_list.sort(key=string.lower)
- list_file= open(list_filename, "w")
- first= True
- for movie in movie_list:
- if not first:
- list_file.write(",")
- else:
- first= False
- list_file.write(movie)
- list_file.close()
- def print_detail(movie_list, title= None, index= None):#only need one
- if title:
- index= index_of_movie(movie_list, title)
- if index!= None:
- if(index>= 0) & (index< len(movie_list)):
- print ""
- list_title, list_rating, list_notes= split_movie_data(movie_list[index])
- print "Title:\t", dirtify(list_title)
- print "Rating:\t", dirtify(list_rating)
- print "Notes:",
- for note in escaped_split(";", list_notes):
- print "\t"+ dirtify(note)
- print ""
- else:
- print "Given index out of range."
- def index_of_movie(movie_list, title):
- index= None
- for movie in movie_list:
- if title.lower()== dirtify(escaped_split(":", movie)[0]).lower():
- index= movie_list.index(movie)
- return index
- def formatted_date():
- local_time= time.localtime()
- date= "("+ str(local_time.tm_yday)+ "/"+ str(local_time.tm_year)+ ")"
- return date
- def split_movie_data(movie):
- try:
- list_title, list_rating, list_notes= escaped_split(":", movie)
- except ValueError:
- list_title, list_notes= escaped_split("::", movie)
- list_rating= ""
- return list_title, list_rating, list_notes
- help= ("\nScript requires exactly 2 arguments:"+
- "\n\t(1) List filename e.g. \"movies.txt\", and"+
- "\n\t(2) Operation e.g. \"add\", \"remove\", or \"print\".")
- if len(sys.argv)== 3:
- list_filename= sys.argv[1]
- if not os.access(list_filename, os.F_OK):
- print "File not found, creating new file..."
- list_file= open(list_filename, "w")
- list_file.close()
- list_file= open(list_filename, "r")
- movie_list= escaped_split(",", list_file.read())
- list_file.close()
- operation= sys.argv[2]
- if operation== "add":
- title= None
- while title!= "":
- if title!= None:
- duplicate= False
- index= index_of_movie(movie_list, title)
- if index!= None:
- duplicate= True
- list_title, list_rating, list_notes= split_movie_data(movie_list[index])
- if not rating:
- rating= list_rating
- if notes:
- new_list_notes= ""
- for note in escaped_split(";", list_notes):
- new_list_notes= new_list_notes+ note+ ";"
- new_list_notes= new_list_notes+ formatted_date()+ " "+ notes
- notes= new_list_notes
- else:
- notes= list_notes
- movie_list[index]= title+ ":"+ rating+ ":"+ notes
- if not duplicate:
- movie_list.append(title+ ":" + (rating if rating else "")+ ":"+ formatted_date()+ " "+ (notes if notes else ""))
- list_file= open(list_filename, "w")
- first= True
- title= sanitize(raw_input("Enter title (nothing to quit): "))
- if title== "":
- break
- rating= sanitize(raw_input("Enter rating: "))
- if rating== "":
- rating= None
- notes= sanitize(raw_input("Enter notes: "))
- if notes== "":
- notes= None
- save(movie_list)
- elif operation== "remove":
- print ""
- title= None
- while title!= "":
- if title!= None:
- title_matched= False
- for movie in movie_list:
- if title.lower()== dirtify(escaped_split(":", movie)[0]).lower():
- print_detail(movie_list, title= escaped_split(":", movie)[0])
- decision= raw_input("Are you sure? (y/n): ")
- if decision== "y":
- movie_list.remove(movie)
- print "Movie removed."
- else:
- print "Removal canceled."
- title_matched= True
- if not title_matched:
- print "Movie not found."
- title= raw_input("Enter title to REMOVE (nothing to quit): ")
- save(movie_list)
- elif operation== "print":
- print ""
- count= 0
- for movie in movie_list:
- print count, ":", "\t"+ dirtify(escaped_split(":", movie)[0])
- count= count+ 1
- print ""
- detail= None
- while detail!= "":
- if detail!= None:
- print_detail(movie_list, index= detail)
- detail= raw_input("Detail? (nothing to quit): ")
- try:
- detail= int(detail)
- except ValueError:
- if detail!= "":
- print "Improper input, please enter index of desired movie."
- detail= None
- else:
- print help
- else:
- print help
- print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement