Advertisement
Guest User

rolling

a guest
Feb 18th, 2009
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.74 KB | None | 0 0
  1. #automated nzb downloader based on imdb ratings
  2. #this script is designed to download nzb files from nzbs.org (requires registration)
  3. #original script created by popcorn, modified by binhex
  4. #ver 1.0
  5.  
  6. import urllib
  7. import xml.dom.minidom
  8. import re
  9. import time
  10. import os
  11. import traceback
  12. import logging
  13. import socket
  14.  
  15. #put your own folder names below as specified in sabnzbd+
  16. #double slashes for windows users essential
  17. watch_dir="J:\\downloads\\nzb"
  18. nzb_dir="J:\\downloads\\nzbbackup"
  19. completed_dir="E:\\Warez"
  20.  
  21. #put your own folder names below to specify the location of your movie collection
  22. movies_dir1="E:\\Warez\\Movies"
  23.  
  24. #put your own folder and filenames below to specify location of log files
  25. mainlog="J:\\sabnzbd+\\Logs\\Scriptlogs\\autodl_hd_main.log"
  26. tracelog="J:\\sabnzbd+\\Logs\\Scriptlogs\\autodl_hd_traceback.log"
  27.  
  28. #put your own notification message process below for error logging or use the default
  29. notifylog="cmd /c msg console /TIME:43200 Script Error, Check Logs"
  30.  
  31. #send failure messages to log file
  32. main_logger = logging.getLogger('myapp1')
  33. main_handler = logging.FileHandler(mainlog)
  34. main_formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
  35. main_handler.setFormatter(main_formatter)
  36. main_logger.addHandler(main_handler)
  37. main_logger.setLevel(logging.INFO)
  38.  
  39. #sets timeout period for urlretrieve (in seconds)
  40. socket.setdefaulttimeout(240)
  41.  
  42. #un-comment for x264 movies
  43. #movie_category="catid=4"
  44.  
  45. #un-comment for dvdrips only
  46. #movie_format="&q=dvdrip.xvid"
  47.  
  48. #un-comment for XviD movies
  49. movie_category="catid=2"
  50.  
  51. #put your userid for nzbs.org website below (register and then go to http://www.nzbs.org/index.php?action=rss)
  52. userid="&i=11111&h=e1111111111111111111111111111111"
  53.  
  54. #un-comment for 720p resolution only
  55. #movie_format="&q=720p"
  56.  
  57. #un-comment for 1080p resolution only
  58. #movie_format="&q=1080p"
  59.  
  60. #un-comment for all formats in selected category
  61. movie_format=""
  62.  
  63. #rss feed for nzbs.org
  64. nzbs_org_feed="http://www.nzbs.org/rss.php?" + movie_category + userid + "&dl=1" + movie_format
  65.  
  66. ##############
  67. # os filters #
  68. ##############
  69.  
  70. try:
  71.         def isInWatched(check_watched_folder):
  72.        
  73.                 #this is set to download only if the nzb file doesn't exist in the watch folder (watch_dir)
  74.                 if os.path.exists(watch_dir + "\\" + check_watched_folder + ".nzb"):
  75.                
  76.                         return 0
  77.                 else:
  78.                         return 1
  79.    
  80.         def isInNZB(check_nzb_folder):
  81.        
  82.                 #this is set to download only if the nzb file doesn't exist in the nzb folder (nzb_dir)
  83.                 if os.path.exists(nzb_dir + "\\" + check_nzb_folder + ".nzb.gz"):
  84.                          
  85.                         return 0
  86.                 else:
  87.                         return 1
  88.    
  89.         def isCompleted(check_completed_folder):
  90.        
  91.                 #this is set to download only if the movie doesn't exist in the completed folder (completed_dir)
  92.                 if os.path.exists(completed_dir + "\\" + check_completed_folder):
  93.                  
  94.                         return 0
  95.                 else:
  96.                         return 1
  97.  
  98.         def isDownloaded(check_downloaded_folder):
  99.        
  100.                 #this is set to download only movies which don't exist in the movies folders (movies_dir1/2)
  101.                 if os.path.exists(movies_dir1 + "\\" + check_downloaded_folder):
  102.                    
  103.                         return 0
  104.                 else:
  105.                         return 1
  106.  
  107. except:
  108.         print("Error: OS Filter Error")
  109.         main_logger.error("OS Filter Error")
  110.         traceback.print_exc(file=open(tracelog,"a"))
  111.         os.system(notifylog)
  112.  
  113. ################
  114. # imdb filters #
  115. ################
  116.  
  117. try:
  118.         def isGoodRatings(imdb_html):
  119.        
  120.                 #this is set to download only movies with a rating of 6.5 or more
  121.                 if re.compile("<b>6\.[5-9]/10</b>|<b>[7-9]\.[0-9]/10</b>").search(imdb_html):
  122.                
  123.                         return 1
  124.                 else:
  125.                         return 0
  126.            
  127.         def isGoodVotes(imdb_html):
  128.        
  129.                 #this is set to download only movies with 5,000 votes or more
  130.                 if re.compile(">[0-9][0-9]+,[0-9]+ votes<|>[5-9],[0-9]+ votes<", re.IGNORECASE).search(imdb_html):
  131.                
  132.                         return 1
  133.                 else:
  134.                         return 0
  135.        
  136.         def isGoodLang(imdb_html):
  137.        
  138.                 #this is set to download movies that contain language "English" (movie may contain other languages)
  139.                 if re.compile("Languages/English", re.IGNORECASE).search(imdb_html):
  140.                
  141.                         return 1
  142.                 else:
  143.                         return 0
  144.            
  145.         def isGoodCountry(imdb_html):
  146.        
  147.                 #this is set to download movies from countries that have english as their native language
  148.                 if re.compile("Countries/UK|Countries/USA|Countries/Canada|Countries/Australia|Countries/Ireland|Countries/SouthAfrica|Countries/NewZealand", re.IGNORECASE).search(imdb_html):
  149.                
  150.                         return 1
  151.                 else:
  152.                         return 0
  153.            
  154.         def isBadDate(imdb_html):
  155.        
  156.                 #this is set to download only movies with a release date of 1970 and newer
  157.                 movie_year = re.compile("<title>.+</title>").search(imdb_html)
  158.  
  159.                 if movie_year is not None:
  160.                        
  161.                         if re.search("19[0-6][0-9]", movie_year.group()):
  162.  
  163.                                 return 0
  164.                         else:
  165.                                 return 1
  166.  
  167.         def isBadGenre(imdb_html):
  168.        
  169.                 #this is set to download only movies that DO NOT match certain genre's
  170.                 if re.compile(">Documentary<|>Musical<|>Music<", re.IGNORECASE).search(imdb_html):
  171.  
  172.                         return 0
  173.                 else:
  174.                         return 1
  175.                
  176. except:
  177.         print("Error: IMDB Filter Error")
  178.         main_logger.error("IMDB Filter Error")
  179.         traceback.print_exc(file=open(tracelog,"a"))
  180.         os.system(notifylog)
  181.  
  182. #######################
  183. # nzb release filters #
  184. #######################
  185.  
  186. try:
  187.         def isGoodSize(check_movie_size):
  188.        
  189.                 #this is set to download movies with file sizes from 700 MB to 2.59 GB (xvid format)
  190.                 if re.compile("[7-9][0-9][0-9].[0-9][0-9] MB|1.[0-9][0-9] GB|2.[0-5][0-9] GB", re.IGNORECASE).search(check_movie_size):
  191.  
  192.                         return 1
  193.                 else:
  194.                         return 0
  195.  
  196.         def isBadGroup(check_group):
  197.        
  198.                 #this is set to skip movies with the following release group names (currently disabled)
  199.                 if re.compile("-BadGroup", re.IGNORECASE).search(check_group):
  200.                  
  201.                         return 0
  202.                 else:
  203.                         return 1
  204.  
  205.         def isGoodMovie(check_movie):
  206.        
  207.                 #this is set to ignore filters for certain movie titles
  208.                 if re.compile("good movie", re.IGNORECASE).search(check_movie):
  209.                  
  210.                         return 1
  211.                 else:
  212.                         return 0
  213.  
  214. except:
  215.         print("Error: NZB Filter Error")
  216.         main_logger.error("NZB Filter Error")
  217.         traceback.print_exc(file=open(tracelog,"a"))
  218.         os.system(notifylog)
  219.  
  220. try:      
  221.         def download(report_id):
  222.        
  223.                 #this downloads the nzb from the link in the rss feed.
  224.                 print("")
  225.                 print("Status: Downloading Movie.....")
  226.                 print("IMDB Movie Title: " + imdb_movie_title + " (" + imdb_link + ")")
  227.                 print("Release Movie Title: " + movie_title)
  228.                 urllib.urlretrieve(msgid, watch_dir + "\\" + imdb_movie_title + ".nzb")
  229.  
  230. except:
  231.         print("Error: Download Of NZB Failed")
  232.         main_logger.error("Download Of Movie " + imdb_movie_title + " Failed")
  233.         traceback.print_exc(file=open(tracelog,"a"))
  234.         os.system(notifylog)
  235.  
  236.         removeRetryCount = 0
  237.         while removeRetryCount < 3:
  238.                 time.sleep(10)
  239.                        
  240.                 #sleep and retry to delete partial download 3 times
  241.                 try:
  242.                         os.remove(watch_dir + "\\" + imdb_movie_title + ".nzb")
  243.                         break
  244.                 except:
  245.                         removeRetryCount += 1
  246.         else:
  247.                 print("Error: Deletion Of Partial NZB Failed")
  248.                 main_logger.error("Deletion Of NZB " + imdb_movie_title + " Failed")
  249.                 traceback.print_exc(file=open(tracelog,"a"))
  250.                 os.system(notifylog)
  251.          
  252. #this reads the content of the rss feed from nzbs.org
  253. try:
  254.         dom=xml.dom.minidom.parse(urllib.urlopen(nzbs_org_feed))
  255. except:
  256.         print("Error: RSS Feed Error")
  257.         main_logger.error("RSS Feed Error")
  258.         traceback.print_exc(file=open(tracelog,"a"))
  259.         os.system(notifylog)
  260.  
  261. #this breaks down the rss feed page into tag sections
  262. for node in dom.getElementsByTagName("item"):
  263.    
  264.         movie_title = node.getElementsByTagName("title")[0].childNodes[0].data
  265.         report_description = node.getElementsByTagName("description")[0].childNodes[0].data
  266.         movie_desc = re.search("IMDB:.+\/\"\>", report_description)
  267.            
  268.         if movie_desc:
  269.                        
  270.                 try:
  271.                         movie_link = re.search("http.+\/",movie_desc.group())
  272.                         imdb_link=movie_link.group()
  273.                         sock = urllib.urlopen(imdb_link)
  274.                         htmlSource = sock.read()                    
  275.                         sock.close()
  276.                 except:
  277.                         print("Error: RSS Feed Tag Error")
  278.                         main_logger.error("RSS Feed Tag Error")
  279.                         traceback.print_exc(file=open(tracelog,"a"))
  280.                         os.system(notify)
  281.  
  282.                         ######################
  283.                         # IMDB Title Filters #
  284.                         ######################
  285.                 try:        
  286.                         #search imdb page for movie title
  287.                         imdb_movie_title_search = re.compile("<title>.+</title>").search(htmlSource)
  288.                        
  289.                         if imdb_movie_title_search is not None:
  290.                                
  291.                                 imdb_movie_title = imdb_movie_title_search.group()
  292.  
  293.                                 #removes title tags from movie title
  294.                                 imdb_movie_title = re.sub("</?title>?","" ,imdb_movie_title)
  295.  
  296.                                 #removes year from movie title
  297.                                 imdb_movie_title = re.sub(" \(.*\)","" ,imdb_movie_title)
  298.                        
  299.                                 #removes colons from movie title - e.g. "the movie: big movie" converts to "the movie big movie"
  300.                                 imdb_movie_title = re.sub("\:","" ,imdb_movie_title)
  301.  
  302.                                 #removes question marks from movie title - e.g. "the movie?" converts to "the movie"
  303.                                 imdb_movie_title = re.sub("\?","" ,imdb_movie_title)
  304.  
  305.                                 #removes end period from movie title - e.g. "the movie b.m." converts to "the movie b.m"
  306.                                 imdb_movie_title = re.sub("\.$","" ,imdb_movie_title)
  307.  
  308.                                 #removes apostrophe from movie title - e.g. "the movie big movie's" converts to "the movie big movies"
  309.                                 imdb_movie_title = re.sub("'","" ,imdb_movie_title)
  310.  
  311.                                 #replace xml & with "and" - e.g. "the movie & big movie" converts to "the movie and big movie"
  312.                                 imdb_movie_title = re.sub("\&amp\;|\&\#38\;","and" ,imdb_movie_title)
  313.  
  314.                                 #replace roman numerals to numerics - e.g. "the movie V" converts to "the movie 5"
  315.                                 imdb_movie_title = re.sub("V(?![a-zA-Z0-9_])","5", imdb_movie_title, re.I)
  316.                        
  317.                                 #replace roman numerals to numerics - e.g. "the movie IV" converts to "the movie 4"
  318.                                 imdb_movie_title = re.sub("IV(?![a-zA-Z0-9_])","4", imdb_movie_title, re.I)
  319.  
  320.                                 #replace roman numerals to numerics - e.g. "the movie III" converts to "the movie 3"
  321.                                 imdb_movie_title = re.sub("III(?![a-zA-Z0-9_])","3" ,imdb_movie_title, re.I)
  322.  
  323.                                 #replace roman numerals to numerics - e.g. "the movie II" converts to "the movie 2"
  324.                                 imdb_movie_title = re.sub("II(?![a-zA-Z0-9_])","2" ,imdb_movie_title, re.I)
  325.  
  326.                                 #converts first letter of each word in the movie title to caps e.g. "the movie" converts to "The Movie"
  327.                                 imdb_movie_title = imdb_movie_title.title()
  328.  
  329.                 except:
  330.                         print("Error: Title Modification Error")
  331.                         main_logger.error("Title Modification Error")
  332.                         traceback.print_exc(file=open(tracelog,"a"))
  333.                         os.system(notifylog)
  334.                
  335.                 try:
  336.                         #######################
  337.                         # check filter values #
  338.                         #######################
  339.  
  340.                         if (isGoodRatings(htmlSource) == 1):
  341.  
  342.                                 VisGoodRatings = 1
  343.                         else:
  344.                                 VisGoodRatings = 0
  345.  
  346.                         if (isGoodVotes(htmlSource) == 1):
  347.  
  348.                                 VisGoodVotes = 1
  349.                         else:
  350.                                 VisGoodVotes = 0
  351.  
  352.                         if (isDownloaded(imdb_movie_title) == 1):
  353.  
  354.                                 VisDownloaded = 1
  355.                         else:
  356.                                 VisDownloaded = 0
  357.  
  358.                         if (isInNZB(imdb_movie_title) == 1):
  359.  
  360.                                 VisInNZB = 1
  361.                         else:
  362.                                 VisInNZB = 0
  363.  
  364.                         if (isInWatched(imdb_movie_title) == 1):
  365.  
  366.                                 VisInWatched = 1
  367.                         else:
  368.                                 VisInWatched = 0
  369.  
  370.                         if (isCompleted(imdb_movie_title) == 1):
  371.  
  372.                                 VisCompleted = 1
  373.                         else:
  374.                                 VisCompleted = 0
  375.  
  376.                         if (isBadDate(htmlSource) == 1):
  377.  
  378.                                 VisBadDate = 1
  379.                         else:
  380.                                 VisBadDate = 0
  381.  
  382.                         if (isBadGenre(htmlSource) == 1):
  383.  
  384.                                 VisBadGenre = 1
  385.                         else:
  386.                                 VisBadGenre = 0
  387.  
  388.                         if (isBadGroup(movie_title) == 1):
  389.  
  390.                                 VisBadGroup = 1
  391.                         else:
  392.                                 VisBadGroup = 0
  393.  
  394.                         if (isGoodLang(htmlSource) == 1):
  395.  
  396.                                 VisGoodLang = 1
  397.                         else:
  398.                                 VisGoodLang = 0
  399.  
  400.                         if (isGoodCountry(htmlSource) == 1):
  401.  
  402.                                 VisGoodCountry = 1
  403.                         else:
  404.                                 VisGoodCountry = 0
  405.  
  406.                         if (isGoodSize(report_description) == 1):
  407.  
  408.                                 VisGoodSize = 1
  409.                         else:
  410.                                 VisGoodSize = 0
  411.                                
  412.                         if (isGoodMovie(imdb_movie_title) == 1):
  413.  
  414.                                 VisGoodMovie = 1
  415.                         else:
  416.                                 VisGoodMovie = 0
  417.  
  418.                         if ((VisDownloaded == 1) and (VisInNZB == 1) and (VisInWatched == 1) and (VisCompleted == 1)) and (((VisGoodRatings == 1) and (VisGoodVotes == 1) and (VisBadDate == 1) and (VisBadGenre == 1) and (VisBadGroup == 1) and (VisGoodLang == 1) and (VisGoodCountry == 1) and (VisGoodSize == 1)) or (VisGoodMovie == 1)):
  419.                                 msgid=node.getElementsByTagName("link")[0].childNodes[0].data
  420.                                 download(msgid)
  421.  
  422.                         else:
  423.                                 print("")
  424.                                 print("Status: Skipping Movie.....")
  425.                                 print("IMDB Movie Title: " + imdb_movie_title + " (" + imdb_link + ")")
  426.                                 print("Release Movie Title: " + movie_title)
  427.                 except:
  428.                         print("Error: Filter Value Check Error")
  429.                         main_logger.error("Filter Value Check Error")
  430.                         traceback.print_exc(file=open(tracelog,"a"))
  431.                         os.system(notifylog)
  432.  
  433.                 #############
  434.                 # Debugging #
  435.                 #############
  436.  
  437.                 if (VisGoodMovie == 1):
  438.                         print("Debug: Movie In Ignore Filters List")
  439.  
  440.                 if (VisGoodRatings == 0):
  441.                         print("Debug: Rating < 6.5")
  442.  
  443.                 if (VisGoodVotes == 0):
  444.                         print("Debug: Votes < 5,000")
  445.                        
  446.                 if (VisDownloaded == 0):
  447.                         print("Debug: Movie Exists In Downloaded Folder")
  448.  
  449.                 if (VisInNZB == 0):
  450.                         print("Debug: NZB Exists In NZB Folder")
  451.  
  452.                 if (VisInWatched == 0):
  453.                         print("Debug: NZB Exists In Watch Folder")
  454.  
  455.                 if (VisCompleted == 0):
  456.                         print("Debug: Movie Exists In Completed Folder")
  457.  
  458.                 if (VisBadDate == 0):
  459.                         print("Debug: Release Date Is Before 1970")
  460.  
  461.                 if (VisBadGenre == 0):
  462.                         print("Debug: Genre Is Unsuitable")
  463.  
  464.                 if (VisBadGroup == 0):
  465.                         print("Debug: Bad Release Group")
  466.  
  467.                 if (VisGoodLang == 0):
  468.                         print("Debug: Language Of Movie Is NOT English")
  469.  
  470.                 if (VisGoodCountry == 0):
  471.                         print("Debug: Language Of Country Is NOT English")
  472.  
  473.                 if (VisGoodSize == 0):
  474.                         print("Debug: Size Is Too Small/Large")
  475.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement