Advertisement
Guest User

service.py

a guest
Apr 7th, 2018
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.37 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.     "rps r9": "HDRip",
  48.     ""      : "N/A"
  49. }
  50.  
  51. tranTypes = {
  52.     "cps c1": "DVDRip",
  53.     "cps c2": "?c2",
  54.     "cps c3": "TVRip"
  55. }
  56.  
  57. def norm(txt):
  58.     txt = txt.replace(" ", "")
  59.     txt = txt.lower()
  60.     return txt
  61.  
  62.  
  63. def striphtml(txt):
  64.     txt = re.sub("<.*?>", "", txt)
  65.     txt = re.sub(r'\\t', "", txt)
  66.     txt = re.sub("\n", "", txt)
  67.     txt = txt.strip()
  68.     #txt = txt.replace("  ", " ")
  69.     return txt
  70.  
  71.  
  72. def elementsrc(element, exclude=[]):
  73.     if element is None:
  74.         return ""
  75.     if element in exclude:
  76.         return ""
  77.     text = element.text or ''
  78.     for subelement in element:
  79.         text += elementsrc(subelement, exclude)
  80.     text += element.tail or ''
  81.     return striphtml(text)
  82.  
  83.  
  84. class turkcealtyazi(sublib.service):
  85.  
  86.     def search(self):
  87.         self.found = False
  88.         if self.item.imdb:
  89.             self.find(self.item.imdb)
  90.         if not self.num() and not self.item.show and self.item.year:
  91.             self.find("%s %s" % (self.item.title, self.item.year))
  92.         self._subs = []
  93.         if not self.num():
  94.             self.find(self.item.title)
  95.  
  96.     def checkpriority(self, txt):
  97.         # this is a very complicated and fuzzy string work
  98.         txt = txt.lower().replace(" ", "")
  99.         cd = re.search("([0-9])cd", txt)
  100.         # less the number of cds higher the priority
  101.         if cd:
  102.             return False, - int(cd.group(1))
  103.         # rest is for episodes, if movie then return lowest prio.
  104.         if self.item.episode < 0 or not self.item.show:
  105.             return False, -100
  106.         ispack = 0
  107.         packmatch = 0
  108.         epmatch = 0
  109.         skip = False
  110.         se = re.search("s(.+?)\|e(.+)", txt)
  111.         if not se:
  112.             se = re.search("s(.+?)(paket)", txt)
  113.         if se:
  114.             e = se.group(2)
  115.             s = se.group(1)
  116.             # verify season match first
  117.             if s.isdigit() and self.item.season > 0 and \
  118.                     not self.item.season == int(s):
  119.                 return True, 0
  120.             ismultiple = False
  121.             # e: 1,2,3,4 ...
  122.             for m in e.split(","):
  123.                 if m.strip().isdigit():
  124.                     ismultiple = True
  125.                 else:
  126.                     ismultiple = False
  127.                     break
  128.             if ismultiple:
  129.                 # check if in range
  130.                 multiples = [int(x) for x in e.split(",")]
  131.                 if self.item.episode in multiples:
  132.                     packmatch = 2
  133.                 else:
  134.                     skip = True
  135.             # e: 1~4
  136.             if "~" in e:
  137.                 startend = e.split("~")
  138.                 # check if in range
  139.                 if len(startend) == 2 and \
  140.                     startend[0].strip().isdigit() and \
  141.                         startend[1].strip().isdigit():
  142.                     if int(startend[0]) < self.item.episode and \
  143.                             int(startend[1]) > self.item.episode:
  144.                         packmatch = 2
  145.                     else:
  146.                         skip = True
  147.                 else:
  148.                     ispack = 1
  149.             # e: Paket meaning a package
  150.             if e == "paket":
  151.                 ispack = 1
  152.             # e:1 or e:01
  153.             if e.isdigit():
  154.                 if int(e) == self.item.episode:
  155.                     epmatch = 3
  156.                 else:
  157.                     skip = True
  158.         return skip, ispack + epmatch + packmatch
  159.  
  160.     def scraperesults(self, page, tree, query=None):
  161.         for row in tree.findall(".//div[@class='nblock']/div/div[2]"):
  162.             a = row.find(".//a")
  163.             if a is None:
  164.                 continue
  165.             link = a.get("href")
  166.             name = a.get("title")
  167.             years = row.findall(".//span")
  168.             if len(years) > 1:
  169.                 ryear = re.search("([0-9]{4})", years[1].text)
  170.                 if ryear:
  171.                     year = int(ryear.group(1))
  172.             if len(years) <= 1 or not ryear:
  173.                 year = "-1"
  174.             if norm(name) == norm(self.item.title) and \
  175.                 (self.item.show or
  176.                     (self.item.year is None or self.item.year == year)):
  177.                 self.found = True
  178.                 p = self.request(domain + link)
  179.                 e = htmlement.fromstring(p)
  180.                 self.scrapepage(p, e)
  181.                 break
  182.         if query and not self.found:
  183.             pages = tree.findall(".//div[@class='pagin']/a")
  184.             for page in pages:
  185.                 if "sonra" in page.text.lower():
  186.                     if self.found:
  187.                         break
  188.                     query = dict(urlparse.parse_qsl(urlparse.urlparse(page.get("href")).query))
  189.                     self.scraperesults(self.request(domain + "/find.php", query))
  190.  
  191.     def scrapepage(self, page, tree):
  192.         subs = tree.findall(".//div[@id='altyazilar']/div/div")
  193.         for s in subs:
  194.             desc = s.find(".//div[@class='ripdiv']")
  195.             xname = s.find(".//div[@class='fl']/a")
  196.             alcd = s.find(".//div[@class='alcd']")
  197.             if xname is None:
  198.                 continue
  199.             if alcd is None:
  200.                 continue
  201.             if desc is None:
  202.                 continue
  203.             alcd = elementsrc(alcd)
  204.             name = xname.find(".//strong").text
  205.             link = xname.get("href")
  206.             ripType = desc.find(".//span")
  207.             if ripType is not None:
  208.                 ripType = ripType.get("class")
  209.                 if ripType in ripTypes:
  210.                     ripType = ripTypes[ripType]
  211.                 else:
  212.                     ripType = "?"
  213.             else:
  214.                 ripType = ""
  215.             desc = ripType + ' ' + elementsrc(desc)
  216.             skip, priority = self.checkpriority(alcd)
  217.             if skip:
  218.                 continue
  219.  
  220.             alcevirmen = s.find(".//div[@class='alcevirmen']")
  221.             tran = ""
  222.             alcevirmenA = alcevirmen.findall(".//a")
  223.             alcevirmenSpan = alcevirmen.findall(".//span")
  224.             tranCount = len(alcevirmenSpan) + 1
  225.             if alcevirmenA:
  226.                 for alc in alcevirmenA:
  227.                     strong = alc.find(".//strong")
  228.                     span = strong.find(".//span")
  229.                     if strong is not None:
  230.                         if strong.text is not None:
  231.                             tran += strong.text
  232.                         elif span is not None:
  233.                             tran += tranTypes[span.get("class")]
  234.                             tranCount -= 1
  235.                         tranCount -= 1
  236.                         if tranCount > 0:
  237.                             tran += ' & '
  238.             elif alcevirmenSpan:
  239.                 tran = tranTypes[alcevirmenSpan[0].get("class")]
  240.             else:
  241.                 tran = alcevirmen.text
  242.  
  243.             iso = "tr"
  244.             qualrate = "4"
  245.             aldil = s.find(".//div[@class='aldil']/span")
  246.             if aldil is not None:
  247.                 cls = aldil.get("class")
  248.                 riso = re.search('flag([a-z]{2})', cls)
  249.                 if riso is not None:
  250.                     iso = riso.group(1)
  251.             qual = s.find(".//div[@class='fl']/span")
  252.             if qual is not None:
  253.                 qual = qual.get("class")
  254.                 if isinstance(qual, (str, unicode)):
  255.                     qual = qual.replace("kal", "")
  256.                     if qual.isdigit():
  257.                         qualrate = qual
  258.             namestr = "%s: %s, %s ~ %s" % (name, alcd, desc, tran)
  259.             sub = self.sub(namestr, iso)
  260.             sub.download(domain + link)
  261.             sub.priority = priority
  262.             if qual:
  263.                 sub.rating = quals[qualrate]
  264.             self.addsub(sub)
  265.  
  266.     def find(self, query):
  267.         q = {"cat": "sub", "find": query}
  268.         page = self.request(domain + "/find.php", q)
  269.         tree = htmlement.fromstring(page)
  270.         title = tree.find(".//title")
  271.         if title is not None:
  272.             if "arama" in title.text.lower():
  273.                 self.scraperesults(page, tree, q)
  274.             else:
  275.                 self.scrapepage(page, tree)
  276.         else:
  277.             self.scrapepage(page, tree)
  278.  
  279.     def download(self, link):
  280.         page = self.request(link)
  281.         tree = htmlement.fromstring(page)
  282.         idid = tree.find(".//input[@name='idid']").get("value")
  283.         alid = tree.find(".//input[@name='altid']").get("value")
  284.         sdid = tree.find(".//input[@name='sidid']").get("value")
  285.         data = {
  286.                "idid": idid,
  287.                "altid": alid,
  288.                "sidid": sdid
  289.                }
  290.         remfile = self.request(domain + "/ind", None,
  291.                                data,
  292.                                domain,
  293.                                True,
  294.                                )
  295.         fname = remfile.info().getheader("Content-Disposition")
  296.         fname = re.search('filename=(.*)', fname)
  297.         fname = fname.group(1)
  298.         fname = os.path.join(self.path, fname)
  299.         with open(fname, "wb") as f:
  300.             f.write(remfile.read())
  301.         self.addfile(fname)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement