Advertisement
Guest User

service.py

a guest
Apr 6th, 2018
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.25 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. '''
  3.    Author    : Huseyin BIYIK <husenbiyik at hotmail>
  4.    Year      : 2016
  5.    License   : GPL
  6.  
  7.    This program is free software: you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation, either version 3 of the License, or
  10.    (at your option) any later version.
  11.  
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19. '''
  20. import sublib
  21. import htmlement
  22. import urlparse
  23.  
  24. import re
  25. import os
  26.  
  27. domain = "http://www.turkcealtyazi.org"
  28.  
  29. quals = {
  30.          "1": 5,  # good quality
  31.          "2": 4,  # enough quality
  32.          "3": 0,  # bad quality
  33.          "4": 2,  # not rated yet
  34.          "5": 1,  # waiting for source
  35.          "6": 3,  # archived
  36.          }
  37.  
  38. ripTypes = {
  39.     "rps r1": "HD",
  40.     "rps r2": "DVDRip",
  41.     "rps r3": "?r3",
  42.     "rps r4": "?r4",
  43.     "rps r5": "?r5",
  44.     "rps r6": "WEBRIP",
  45.     "rps r7": "BDRip",
  46.     "rps r8": "WEB-DL",
  47.     ""      : "N/A"
  48. }
  49.  
  50. tranTypes = {
  51.     "cps c1": "DVDRip",
  52.     "cps c2": "?c2",
  53.     "cps c3": "TVRip"
  54. }
  55.  
  56. def norm(txt):
  57.     txt = txt.replace(" ", "")
  58.     txt = txt.lower()
  59.     return txt
  60.  
  61.  
  62. def striphtml(txt):
  63.     txt = re.sub("<.*?>", "", txt)
  64.     txt = re.sub(r'\\t', "", txt)
  65.     txt = re.sub("\n", "", txt)
  66.     txt = txt.strip()
  67.     #txt = txt.replace("  ", " ")
  68.     return txt
  69.  
  70.  
  71. def elementsrc(element, exclude=[]):
  72.     if element is None:
  73.         return ""
  74.     if element in exclude:
  75.         return ""
  76.     text = element.text or ''
  77.     for subelement in element:
  78.         text += elementsrc(subelement, exclude)
  79.     text += element.tail or ''
  80.     return striphtml(text)
  81.  
  82.  
  83. class turkcealtyazi(sublib.service):
  84.  
  85.     def search(self):
  86.         self.found = False
  87.         if self.item.imdb:
  88.             self.find(self.item.imdb)
  89.         if not self.num() and not self.item.show and self.item.year:
  90.             self.find("%s %s" % (self.item.title, self.item.year))
  91.         self._subs = []
  92.         if not self.num():
  93.             self.find(self.item.title)
  94.  
  95.     def checkpriority(self, txt):
  96.         # this is a very complicated and fuzzy string work
  97.         txt = txt.lower().replace(" ", "")
  98.         cd = re.search("([0-9])cd", txt)
  99.         # less the number of cds higher the priority
  100.         if cd:
  101.             return False, - int(cd.group(1))
  102.         # rest is for episodes, if movie then return lowest prio.
  103.         if self.item.episode < 0 or not self.item.show:
  104.             return False, -100
  105.         ispack = 0
  106.         packmatch = 0
  107.         epmatch = 0
  108.         skip = False
  109.         se = re.search("s(.+?)\|e(.+)", txt)
  110.         if not se:
  111.             se = re.search("s(.+?)(paket)", txt)
  112.         if se:
  113.             e = se.group(2)
  114.             s = se.group(1)
  115.             # verify season match first
  116.             if s.isdigit() and self.item.season > 0 and \
  117.                     not self.item.season == int(s):
  118.                 return True, 0
  119.             ismultiple = False
  120.             # e: 1,2,3,4 ...
  121.             for m in e.split(","):
  122.                 if m.strip().isdigit():
  123.                     ismultiple = True
  124.                 else:
  125.                     ismultiple = False
  126.                     break
  127.             if ismultiple:
  128.                 # check if in range
  129.                 multiples = [int(x) for x in e.split(",")]
  130.                 if self.item.episode in multiples:
  131.                     packmatch = 2
  132.                 else:
  133.                     skip = True
  134.             # e: 1~4
  135.             if "~" in e:
  136.                 startend = e.split("~")
  137.                 # check if in range
  138.                 if len(startend) == 2 and \
  139.                     startend[0].strip().isdigit() and \
  140.                         startend[1].strip().isdigit():
  141.                     if int(startend[0]) < self.item.episode and \
  142.                             int(startend[1]) > self.item.episode:
  143.                         packmatch = 2
  144.                     else:
  145.                         skip = True
  146.                 else:
  147.                     ispack = 1
  148.             # e: Paket meaning a package
  149.             if e == "paket":
  150.                 ispack = 1
  151.             # e:1 or e:01
  152.             if e.isdigit():
  153.                 if int(e) == self.item.episode:
  154.                     epmatch = 3
  155.                 else:
  156.                     skip = True
  157.         return skip, ispack + epmatch + packmatch
  158.  
  159.     def scraperesults(self, page, tree, query=None):
  160.         for row in tree.findall(".//div[@class='nblock']/div/div[2]"):
  161.             a = row.find(".//a")
  162.             if a is None:
  163.                 continue
  164.             link = a.get("href")
  165.             name = a.get("title")
  166.             years = row.findall(".//span")
  167.             if len(years) > 1:
  168.                 ryear = re.search("([0-9]{4})", years[1].text)
  169.                 if ryear:
  170.                     year = int(ryear.group(1))
  171.             if len(years) <= 1 or not ryear:
  172.                 year = "-1"
  173.             if norm(name) == norm(self.item.title) and \
  174.                 (self.item.show or
  175.                     (self.item.year is None or self.item.year == year)):
  176.                 self.found = True
  177.                 p = self.request(domain + link)
  178.                 e = htmlement.fromstring(p)
  179.                 self.scrapepage(p, e)
  180.                 break
  181.         if query and not self.found:
  182.             pages = tree.findall(".//div[@class='pagin']/a")
  183.             for page in pages:
  184.                 if "sonra" in page.text.lower():
  185.                     if self.found:
  186.                         break
  187.                     query = dict(urlparse.parse_qsl(urlparse.urlparse(page.get("href")).query))
  188.                     self.scraperesults(self.request(domain + "/find.php", query))
  189.  
  190.     def scrapepage(self, page, tree):
  191.         subs = tree.findall(".//div[@id='altyazilar']/div/div")
  192.         for s in subs:
  193.             desc = s.find(".//div[@class='ripdiv']")
  194.             xname = s.find(".//div[@class='fl']/a")
  195.             alcd = s.find(".//div[@class='alcd']")
  196.             if xname is None:
  197.                 continue
  198.             if alcd is None:
  199.                 continue
  200.             if desc is None:
  201.                 continue
  202.             alcd = elementsrc(alcd)
  203.             name = xname.find(".//strong").text
  204.             link = xname.get("href")
  205.             ripType = desc.find(".//span")
  206.             if ripType is not None:
  207.                 ripType = ripType.get("class")
  208.                 ripType = ripTypes[ripType]
  209.             else:
  210.                 ripType = ""
  211.             desc = ripType + ' ' + elementsrc(desc)
  212.             skip, priority = self.checkpriority(alcd)
  213.             if skip:
  214.                 continue
  215.  
  216.             alcevirmen = s.find(".//div[@class='alcevirmen']")
  217.             tran = ""
  218.             alcevirmenA = alcevirmen.findall(".//a")
  219.             alcevirmenSpan = alcevirmen.findall(".//span")
  220.             tranCount = len(alcevirmenSpan) + 1
  221.             if alcevirmenA:
  222.                 for alc in alcevirmenA:
  223.                     strong = alc.find(".//strong")
  224.                     span = strong.find(".//span")
  225.                     if strong is not None:
  226.                         if strong.text is not None:
  227.                             tran += strong.text
  228.                         elif span is not None:
  229.                             tran += tranTypes[span.get("class")]
  230.                             tranCount -= 1
  231.                         tranCount -= 1
  232.                         if tranCount > 0:
  233.                             tran += ' & '
  234.             elif alcevirmenSpan:
  235.                 tran = tranTypes[alcevirmenSpan[0].get("class")]
  236.             else:
  237.                 tran = alcevirmen.text
  238.  
  239.             iso = "tr"
  240.             qualrate = "4"
  241.             aldil = s.find(".//div[@class='aldil']/span")
  242.             if aldil is not None:
  243.                 cls = aldil.get("class")
  244.                 riso = re.search('flag([a-z]{2})', cls)
  245.                 if riso is not None:
  246.                     iso = riso.group(1)
  247.             qual = s.find(".//div[@class='fl']/span")
  248.             if qual is not None:
  249.                 qual = qual.get("class")
  250.                 if isinstance(qual, (str, unicode)):
  251.                     qual = qual.replace("kal", "")
  252.                     if qual.isdigit():
  253.                         qualrate = qual
  254.             namestr = "%s: %s, %s ~ %s" % (name, alcd, desc, tran)
  255.             sub = self.sub(namestr, iso)
  256.             sub.download(domain + link)
  257.             sub.priority = priority
  258.             if qual:
  259.                 sub.rating = quals[qualrate]
  260.             self.addsub(sub)
  261.  
  262.     def find(self, query):
  263.         q = {"cat": "sub", "find": query}
  264.         page = self.request(domain + "/find.php", q)
  265.         tree = htmlement.fromstring(page)
  266.         title = tree.find(".//title")
  267.         if title is not None:
  268.             if "arama" in title.text.lower():
  269.                 self.scraperesults(page, tree, q)
  270.             else:
  271.                 self.scrapepage(page, tree)
  272.         else:
  273.             self.scrapepage(page, tree)
  274.  
  275.     def download(self, link):
  276.         page = self.request(link)
  277.         tree = htmlement.fromstring(page)
  278.         idid = tree.find(".//input[@name='idid']").get("value")
  279.         alid = tree.find(".//input[@name='altid']").get("value")
  280.         sdid = tree.find(".//input[@name='sidid']").get("value")
  281.         data = {
  282.                "idid": idid,
  283.                "altid": alid,
  284.                "sidid": sdid
  285.                }
  286.         remfile = self.request(domain + "/ind", None,
  287.                                data,
  288.                                domain,
  289.                                True,
  290.                                )
  291.         fname = remfile.info().getheader("Content-Disposition")
  292.         fname = re.search('filename=(.*)', fname)
  293.         fname = fname.group(1)
  294.         fname = os.path.join(self.path, fname)
  295.         with open(fname, "wb") as f:
  296.             f.write(remfile.read())
  297.         self.addfile(fname)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement