Advertisement
Guest User

ryandaniels

a guest
Jan 5th, 2011
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.56 KB | None | 0 0
  1. #Author: ryandaniels
  2. #Email: find_ryan_daniels@yahoo.com
  3. #Feel free to use/ redist/ modify/ steal from.
  4.  
  5. #If you are a smarter man/ woman/ child than I, you will probably quickly
  6. #realize that this software can list all sorts of things beyond just movies.
  7. #Therefore, I encourage you to ignore the naming in this script and use it for
  8. #whatever you want
  9.  
  10. #Note that I use the term "movie" in this software not because I am an inbred
  11. #hick who makes a point of never watching a "film", but that this list will
  12. #contain movies, and calling a movie a film is not something I would
  13. #cognitivley do /refinement
  14.  
  15. import os
  16. import sys
  17. import re
  18. import time
  19. import string
  20.  
  21.  
  22. def escaped_split(target, token):
  23.     splits= token.split(target)
  24.     final_splits= []
  25.     glue_together= ""
  26.     for split in splits:
  27.         if(split== ""):
  28.             pass
  29.         elif(glue_together!= ""):
  30.             final_splits.append(glue_together+ target+ split)
  31.             glue_together= ""
  32.         elif(split[-1]== "\\"):
  33.             glue_together= split
  34.         else:
  35.             final_splits.append(split)
  36.     return final_splits
  37.  
  38. def sanitize(token):
  39.     return token.replace(",", "\,").replace(":", "\:").replace(";", "\;")
  40.  
  41. def dirtify(token):
  42.     return token.replace("\,", ",").replace("\:", ":").replace("\;", ";")
  43.  
  44. def save(movie_list):
  45.     movie_list.sort(key=string.lower)
  46.     list_file= open(list_filename, "w")
  47.     first= True
  48.     for movie in movie_list:
  49.         if not first:
  50.             list_file.write(",")
  51.         else:
  52.             first= False
  53.         list_file.write(movie)
  54.     list_file.close()
  55.  
  56. def print_detail(movie_list, title= None, index= None):#only need one
  57.     if title:
  58.         index= index_of_movie(movie_list, title)
  59.     if index!= None:
  60.         if(index>= 0) & (index< len(movie_list)):
  61.             print ""
  62.             list_title, list_rating, list_notes= split_movie_data(movie_list[index])
  63.             print "Title:\t", dirtify(list_title)
  64.             print "Rating:\t", dirtify(list_rating)
  65.             print "Notes:",
  66.             for note in escaped_split(";", list_notes):
  67.                 print "\t"+ dirtify(note)
  68.             print ""
  69.         else:
  70.             print "Given index out of range."
  71.  
  72. def index_of_movie(movie_list, title):
  73.     index= None
  74.     for movie in movie_list:
  75.         if title.lower()== dirtify(escaped_split(":", movie)[0]).lower():
  76.             index= movie_list.index(movie)
  77.     return index
  78.  
  79. def formatted_date():
  80.     local_time= time.localtime()
  81.     date= "("+ str(local_time.tm_yday)+ "/"+ str(local_time.tm_year)+ ")"
  82.     return date
  83.  
  84. def split_movie_data(movie):
  85.     try:
  86.         list_title, list_rating, list_notes= escaped_split(":", movie)
  87.     except ValueError:
  88.         list_title, list_notes= escaped_split("::", movie)
  89.         list_rating= ""
  90.     return list_title, list_rating, list_notes
  91.  
  92.  
  93. help= ("\nScript requires exactly 2 arguments:"+
  94.     "\n\t(1) List filename e.g. \"movies.txt\", and"+
  95.     "\n\t(2) Operation e.g. \"add\", \"remove\", or \"print\".")
  96.  
  97. if len(sys.argv)== 3:
  98.     list_filename= sys.argv[1]
  99.     if not os.access(list_filename, os.F_OK):
  100.         print "File not found, creating new file..."
  101.         list_file= open(list_filename, "w")
  102.         list_file.close()
  103.     list_file= open(list_filename, "r")
  104.     movie_list= escaped_split(",", list_file.read())
  105.     list_file.close()
  106.  
  107.     operation= sys.argv[2]
  108.     if operation== "add":
  109.         title= None
  110.         while title!= "":
  111.             if title!= None:
  112.                 duplicate= False
  113.                 index= index_of_movie(movie_list, title)
  114.                 if index!= None:
  115.                     duplicate= True
  116.                     list_title, list_rating, list_notes= split_movie_data(movie_list[index])
  117.                     if not rating:
  118.                         rating= list_rating
  119.                     if notes:
  120.                         new_list_notes= ""
  121.                         for note in escaped_split(";", list_notes):
  122.                             new_list_notes= new_list_notes+ note+ ";"
  123.                         new_list_notes= new_list_notes+ formatted_date()+ " "+ notes
  124.                         notes= new_list_notes
  125.                     else:
  126.                         notes= list_notes
  127.                     movie_list[index]= title+ ":"+ rating+ ":"+ notes
  128.                 if not duplicate:
  129.                     movie_list.append(title+ ":" + (rating if rating else "")+ ":"+ formatted_date()+ " "+ (notes if notes else ""))
  130.                 list_file= open(list_filename, "w")
  131.                 first= True
  132.          
  133.             title= sanitize(raw_input("Enter title (nothing to quit): "))
  134.             if title== "":
  135.                 break
  136.             rating= sanitize(raw_input("Enter rating: "))
  137.             if rating== "":
  138.                 rating= None
  139.             notes= sanitize(raw_input("Enter notes: "))
  140.             if notes== "":
  141.                 notes= None
  142.         save(movie_list)
  143.  
  144.     elif operation== "remove":
  145.         print ""
  146.         title= None
  147.         while title!= "":
  148.             if title!= None:
  149.                 title_matched= False
  150.                 for movie in movie_list:
  151.                     if title.lower()== dirtify(escaped_split(":", movie)[0]).lower():
  152.                         print_detail(movie_list, title= escaped_split(":", movie)[0])
  153.                         decision= raw_input("Are you sure? (y/n): ")
  154.                         if decision== "y":
  155.                             movie_list.remove(movie)
  156.                             print "Movie removed."
  157.                         else:
  158.                             print "Removal canceled."
  159.                         title_matched= True
  160.                 if not title_matched:
  161.                     print "Movie not found."
  162.             title= raw_input("Enter title to REMOVE (nothing to quit): ")
  163.         save(movie_list)
  164.  
  165.     elif operation== "print":
  166.         print ""
  167.         count= 0
  168.         for movie in movie_list:
  169.             print count, ":", "\t"+ dirtify(escaped_split(":", movie)[0])
  170.             count= count+ 1
  171.         print ""
  172.  
  173.         detail= None
  174.         while detail!= "":
  175.             if detail!= None:  
  176.                 print_detail(movie_list, index= detail)
  177.             detail= raw_input("Detail? (nothing to quit): ")
  178.             try:
  179.                 detail= int(detail)
  180.             except ValueError:
  181.                 if detail!= "":
  182.                     print "Improper input, please enter index of desired movie."
  183.                     detail= None
  184.     else:
  185.         print help
  186.  
  187. else:
  188.     print help
  189.  
  190. print ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement