Guest User

youiv_search.py

a guest
Nov 5th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.02 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import requests
  4. import re
  5. import sys
  6. import codecs
  7. import os
  8. import time
  9.  
  10. class G:
  11.     cache_dir_exists = False
  12.     post_index_exists = False
  13.     post_index = ""
  14.     index_index_exists = False
  15.     index_index = ""
  16.    
  17. G.cache_dir_exists = os.path.isdir("youiv cache")
  18. if G.cache_dir_exists:
  19.     G.post_index_exists = os.path.isfile("youiv cache/post/index.txt")
  20.     G.index_index_exists = os.path.isfile("youiv cache/index/index.txt")
  21.  
  22. headers = {
  23.     'User-agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0)'
  24.     ' Gecko/20100101 Firefox/29.0'
  25. }
  26.  
  27. def request(url, headers):
  28.     while True:
  29.         r = requests.get(url, headers=headers)
  30.         if r.status_code == 200:
  31.             return r.text
  32.         else:
  33.             #print "HTTP ERROR -> ", str(r.status_code) + " (" + url + ")"
  34.             #time.sleep(10)
  35.             return "HTTP ERROR -> " + str(r.status_code)
  36.            
  37. def get_post_text(suburl, headers):
  38.     if not G.cache_dir_exists:
  39.         os.makedirs("youiv cache")
  40.         os.makedirs("youiv cache/index")
  41.         os.makedirs("youiv cache/post")
  42.         G.cache_dir_exists = True
  43.        
  44.     subsuburl_list = suburl.split("-", 2)
  45.     subsuburl = subsuburl_list[0] + "-" + subsuburl_list[1]
  46.        
  47.     url = "https://youiv.tv/" + suburl
  48.        
  49.     index_file_exists = False
  50.     fname = suburl
  51.    
  52.     if G.post_index_exists:
  53.         f = open("youiv cache/post/index.txt", "r")
  54.         lines = f.read().split("\n")
  55.         for l in lines:
  56.             if l.find(subsuburl) >= 0:
  57.                 index_file_exists = True
  58.                 fname = l
  59.                 break
  60.         f.close()
  61.    
  62.     if not os.path.isfile("youiv cache/post/" + fname):
  63.         text = request(url, headers)
  64.        
  65.         f = open("youiv cache/post/" + fname, "wb")
  66.         f.write(text.encode("utf-8"))
  67.         f.close()
  68.        
  69.         if not G.post_index_exists:
  70.             f = open("youiv cache/post/index.txt", "w")
  71.             f.write(fname + "\n")
  72.             f.close()
  73.             G.post_index_exists = True
  74.         else:
  75.             f = open("youiv cache/post/index.txt", "a")
  76.             f.write(fname + "\n")
  77.             f.close()
  78.        
  79.         return text
  80.     else:
  81.         f = open("youiv cache/post/" + fname, encoding='utf-8')
  82.         text = f.read()
  83.         f.close()
  84.         return text
  85.        
  86. def get_index_text(page_num, headers):
  87.     if not G.cache_dir_exists:
  88.         os.makedirs("youiv cache")
  89.         os.makedirs("youiv cache/index")
  90.         os.makedirs("youiv cache/post")
  91.         G.cache_dir_exists = True
  92.        
  93.     url = "https://youiv.tv/u15-279-" + str(page_num) + ".html"
  94.     last_time = time.time()
  95.        
  96.     lines = []
  97.     index_file_exists = False
  98.     if G.index_index_exists:
  99.         f = open("youiv cache/index/index.txt", "r")
  100.         lines = f.read().split("\n")
  101.        
  102.         line_num = 0
  103.         while line_num < len(lines):
  104.             line = lines[line_num].split()
  105.             if len(line) >= 2 and line[1] == str(page_num):
  106.                 index_file_exists = True
  107.                 last_time = float(line[0])
  108.                 break
  109.             line_num += 1
  110.            
  111.         f.close()
  112.    
  113.     if not os.path.isfile("youiv cache/index/" + str(page_num)) or time.time() - last_time >= 86400: # If not cached or page in cache is over a day old
  114.         text = request(url, headers)
  115.        
  116.         f = open("youiv cache/index/" + str(page_num), "wb")
  117.         f.write(text.encode("utf-8"))
  118.         f.close()
  119.        
  120.         if not G.index_index_exists:
  121.             f = open("youiv cache/index/index.txt", "w")
  122.             f.write(str(time.time()) + " " + str(page_num) + "\n")
  123.             f.close()
  124.             G.index_index_exists = True
  125.         else:
  126.             write_lines = []
  127.            
  128.             f = open("youiv cache/index/index.txt", "r")
  129.            
  130.             line_num = 0
  131.             while line_num < len(lines):
  132.                 line = lines[line_num].split()
  133.                 if not (len(line) >= 2 and line[1] == str(page_num)):
  134.                     write_lines.append(" ".join(line))
  135.                 line_num += 1
  136.                    
  137.             f.close()
  138.            
  139.             f = open("youiv cache/index/index.txt", "w")
  140.             f.write("\n".join(write_lines))
  141.             f.write(str(time.time()) + " " + str(page_num) + "\n")
  142.             f.close()
  143.        
  144.         return text
  145.     else:
  146.         f = open("youiv cache/index/" + str(page_num), encoding='utf-8')
  147.         text = f.read()
  148.         f.close()
  149.         return text
  150.            
  151. def search_post(suburl, prod_company, code_number):
  152.     page_text = get_post_text(suburl, headers)
  153.     #m = re.search("(" + prod_company + "|" + prod_company.upper() + "|" + prod_company[0].upper() + prod_company[1:] + ")\\s*(\\-)?\\s*" + code_number, page_text)
  154.     m = re.search(prod_company + "\\s*(\\-)?\\s*" + code_number, page_text.lower())
  155.    
  156.     return True if m else False
  157.  
  158. print("1. Search by title")
  159. print("2. Search by video code (much slower)")
  160. print("3. Search by both (about as fast as 1 but not as accurate as 2)")
  161. option = input("Option: ")
  162.  
  163. prod_company = ""
  164. code_number = ""
  165.  
  166. if option == "1":
  167.     query = input("Type in title or sub-phrase: ")
  168. elif option == "2":
  169.     query = input("Type in the video code (Ex. TSBS-81085): ")
  170.     video_code = "".join(query.split()).lower().replace("-", "")
  171.     prod_company = re.search("[a-z]+", video_code).group(0)
  172.     code_number = re.search("[0-9]+", video_code).group(0)
  173. else:
  174.     query = input("Type in title or sub-phrase: ")
  175.     query2 = input("Type in the video code (Ex. TSBS-81085): ")
  176.     video_code = "".join(query2.split()).lower().replace("-", "")
  177.     prod_company = re.search("[a-z]+", video_code).group(0)
  178.     code_number = re.search("[0-9]+", video_code).group(0)
  179.    
  180. start_page = int(input("Starting page #: "))
  181. max_page = int(input("Search up to page # (Type 0 to search all pages): "))
  182.  
  183. page_text = get_index_text(1, headers)
  184.  
  185. m = re.search("\\<a href\\=\\\"([a-z]|[A-Z]|[0-9]|(\\-))+\\.html\\\" class=\\\"last\\\"\\>\\.\\.\\. [0-9]+\\<\\/a\\>", page_text)
  186. subtext = m.group(0)
  187. last_page = int(re.search("\\.\\.\\.( |\\t|\\n|\\r)*[0-9]+", subtext).group(0)[3:].strip())
  188. #print(last_page)
  189.  
  190. if max_page > 0:
  191.     last_page = min(last_page, max_page)
  192.  
  193. results = []
  194.  
  195. for i in range(max(2, start_page + 1), last_page + 2):
  196. #for i in range(2, 3):
  197.     print("Searching page " + str(i - 1) + "...")
  198.    
  199.     find_start = 0
  200.     found = page_text.find("<li style", find_start)
  201.    
  202.     while found >= 0:
  203.         find_start = found + len("<li style")
  204.         url_start = page_text.find("<a href=", find_start) + len("<a href=") + 1
  205.         url_end = page_text.find("\"", url_start)
  206.         suburl = page_text[url_start:url_end]
  207.         url = "https://youiv.tv/" + suburl
  208.        
  209.         title_start = page_text.find("title=\"", url_end) + len("title=\"")
  210.         title_end = page_text.find("\"", title_start)
  211.         title = page_text[title_start:title_end]
  212.        
  213.         if option == "1":
  214.             if "".join(title.split()).lower().find("".join(query.split()).lower()) >= 0:
  215.                 results.append((url, title))
  216.         elif option == "2":
  217.             if search_post(suburl, prod_company, code_number):
  218.                 print("\nFOUND: " + url + " - " + title + "\n")
  219.                 answer = input("Continue searching? y/n: ")
  220.                 if answer != "y":
  221.                     sys.exit(0)
  222.                 results.append((url, title))
  223.         else:
  224.             if "".join(title.split()).lower().find("".join(query.split()).lower()) >= 0:
  225.                 if search_post(suburl, prod_company, code_number):
  226.                     print("\nFOUND: " + url + " - " + title + "\n")
  227.                     answer = input("Continue searching? y/n: ")
  228.                     if answer != "y":
  229.                         sys.exit(0)
  230.                     results.append((url, title))
  231.        
  232.         found = page_text.find("<li style", find_start)
  233.    
  234.     if i <= last_page:
  235.         page_text = get_index_text(i, headers)
  236.        
  237. print("\nRESULTS:\n")
  238.  
  239. if len(results) == 0:
  240.     print("- No results :( - ")
  241. else:
  242.     for r in results:
  243.         print(r[0] + " - " + r[1])
Advertisement
Add Comment
Please, Sign In to add comment