Advertisement
Guest User

service.py

a guest
Oct 19th, 2018
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.08 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.  
  22. import re
  23. import os
  24.  
  25. domain = "https://www.planetdp.org"
  26. dptoiso = {
  27.            "tr": "tr",
  28.            "en": "en",
  29.            "sp": "es",
  30.            "gr": "de",
  31.            "fr": "fr",
  32.            }
  33. isotoquery = {
  34.            "tr": u"Türkçe",
  35.            "en": u"İngilizce",
  36.            "es": u"İspanyolca",
  37.            "de": u"Almanca",
  38.            "fr": u"Fransızca",
  39.               }
  40.  
  41. quals = {
  42.          "sub_checked": 5,  # Onaylandı
  43.          "sub_checked_orange": 3,  # Teknik Yapı Hataları, Hafif İmla Hataları, Ekstrem Durum
  44.          "sub_checked_red": 1,  # Teknik Yapı Hataları, Ağır İmla Hataları, Çeviri Zayıf Düzeyde
  45.          }
  46.  
  47. def norm(txt):
  48.     txt = txt.replace(" ", "")
  49.     txt = txt.lower()
  50.     return txt
  51.  
  52.  
  53. class planetdp(sublib.service):
  54.  
  55.     def search(self):
  56.         if self.item.imdb and self.item.imdb.startswith("tt"):
  57.             self.searchimdb()
  58.         if not self.num() and self.item.year:
  59.             self.searchnameyear()
  60.         if not self.num():
  61.             self.searchpredict()
  62.  
  63.     def download(self, link):
  64.         page = self.request(link)
  65.         token = re.search('<input type="hidden" name="_token" value="(.*?)">', page)
  66.         subid = re.search('rel\-id="(.*?)"', page)
  67.         uniqk = re.search('rel\-tag="(.*?)"', page)
  68.  
  69.         if token and subid and uniqk:
  70.             data = {
  71.                     "_token": token.group(1),
  72.                     "subtitle_id": subid.group(1),
  73.                     "uniquekey": uniqk.group(1)
  74.                     }
  75.             remfile = self.request(domain + "/subtitle/download", None, data, link, True)
  76.             fname = remfile.info().getheader("Content-Disposition")
  77.             fname = re.search('filename=(.*)', fname)
  78.             fname = fname.group(1)
  79.             fname = os.path.join(self.path, fname)
  80.             with open(fname, "wb") as f:
  81.                 f.write(remfile.read())
  82.             self.addfile(fname)
  83.  
  84.     def checkpriority(self, txt):
  85.         # this is a very complicated and fuzzy string work
  86.         if self.item.episode < 0 or not self.item.show:
  87.             return False, 0
  88.         ispack = 0
  89.         packmatch = 0
  90.         epmatch = 0
  91.         skip = False
  92.         sb = re.search("S:<b>(.+?)<\/b>[-|,]B:<b>(.+?)<\/b>", txt)
  93.         if not sb:
  94.             sb = re.search(".*?<b>S<\/b>\:(.+?)[-|,]<b>B<\/b>\:(.+)", txt)
  95.         if sb:
  96.             e = sb.group(2).strip().replace(" ", "").lower()
  97.             s = sb.group(1).strip().replace(" ", "").lower()
  98.             # verify season match first
  99.             if s.isdigit() and self.item.season > 0 and \
  100.                     not self.item.season == int(s):
  101.                 return True, 0
  102.             ismultiple = False
  103.             # B: 1,2,3,4 ...
  104.             for m in e.split(","):
  105.                 if m.strip().isdigit():
  106.                     ismultiple = True
  107.                 else:
  108.                     ismultiple = False
  109.                     break
  110.             if ismultiple:
  111.                 # check if in range
  112.                 multiples = [int(x) for x in e.split(",")]
  113.                 if self.item.episode in multiples:
  114.                     packmatch = 2
  115.                 else:
  116.                     skip = True
  117.             # B: 1~4
  118.             if "~" in e:
  119.                 startend = e.split("~")
  120.                 # check if in range
  121.                 if len(startend) == 2 and \
  122.                     startend[0].strip().isdigit() and \
  123.                         startend[1].strip().isdigit():
  124.                     if int(startend[0]) < self.item.episode and \
  125.                             int(startend[1]) > self.item.episode:
  126.                         packmatch = 2
  127.                     else:
  128.                         skip = True
  129.                 else:
  130.                     ispack = 1
  131.             # B: Paket meaning a package
  132.             if "paket" in e:
  133.                 ispack = 1
  134.             # B:1 or B:01
  135.             if e.isdigit():
  136.                 if int(e) == self.item.episode:
  137.                     epmatch = 3
  138.                 else:
  139.                     skip = True
  140.         return skip, ispack + epmatch + packmatch
  141.  
  142.     def scrapesubs(self, page):
  143.         for row in re.findall("<tr(.*?)</tr>", page, re.DOTALL):
  144.             index = 0
  145.             link = None
  146.             name = None
  147.             iso = None
  148.             priority = 0
  149.             quality = 0
  150.             for column in re.findall("<td(.*?)</td>", row, re.DOTALL):
  151.                 index += 1
  152.                 if index == 1:
  153.                     res = re.search('href="(.*?)".*?title="(.*?)">(.*?)<(.*)', column)
  154.                     link = domain + res.group(1)
  155.                     skip, priority = self.checkpriority(res.group(4))
  156.                     if skip:
  157.                         break
  158.                     if self.item.show:
  159.                         name = "%s" % (res.group(2))
  160.                     else:
  161.                         name = "%s: %s" % (res.group(3), res.group(2))
  162.                 if index == 2:
  163.                     res = re.search("<img src='(.*?)'", column)
  164.                     if res:
  165.                         country = res.group(1).split("/")[-1].split(".")[0][-2:]
  166.                         iso = dptoiso[country]
  167.                     else:
  168.                         iso = "tr"
  169.                 if index == 3:
  170.                     res = re.search("<a.*?>(.*?)</a", column)
  171.                     name += " ~ %s" % res.group(1)
  172.                 if index == 9:
  173.                     qual = re.search('<i.*?_cls (.*?)".*<\/i', column)
  174.                     if qual:
  175.                         quality = quals[qual.group(1)]
  176.             if link and iso and name:
  177.                 sub = self.sub(name, iso)
  178.                 sub.download(link)
  179.                 sub.priority = priority
  180.                 sub.rating = quality
  181.                 self.addsub(sub)
  182.  
  183.     def scrapemovie(self, page):
  184.         movieFound = False
  185.         regstr = '<tr(.*?)</tr>'
  186.         for row in re.findall(regstr, page, re.DOTALL):
  187.             index = 0
  188.             if not movieFound:
  189.                 link = None
  190.                 name = None
  191.                 iso = None
  192.                 trans = None
  193.                 priority = 0
  194.                 quality = 0
  195.                 for column in re.findall("<td(.*?)</td>", row, re.DOTALL):
  196.                     skip = None
  197.                     index += 1
  198.                     if index == 1:
  199.                         res = re.search('href="(.*?)".*?>(.*?)\\n', column)
  200.                         if res:
  201.                             link = domain + res.group(1)
  202.                             name = "%s" % res.group(2)
  203. #                            movieFound = True
  204. #                        release = re.search("<span>(.*?)</span>", column)
  205. #                        if release:
  206. #                            release = re.sub("<.*?>", "", release.group(1))
  207. #                            skip, priority = self.checkpriority(release)
  208.                             skip, priority = self.checkpriority(column)
  209.                             if skip:
  210. #                               name = None
  211.                                 break
  212.                             else:
  213.                                 movieFound = True
  214.  
  215.                     if index == 2:
  216.                         res = re.search("src='(.*?)'", column)
  217.                         if res:
  218.                             country = res.group(1).split("/")[-1].split(".")[0][-2:]
  219.                             iso = dptoiso[country]
  220.                         else:
  221.                             iso = "tr"
  222.                     if index == 7:
  223.                         res = re.search('itemprop="translator">\\n(.*?)\\n', column)
  224.                         if res:
  225.                             trans = res.group(1)
  226.                     if index == 9:
  227.                         qual = re.search('<i.*?_cls (.*?)".*<\/i', column)
  228.                         if qual:
  229.                             quality = quals[qual.group(1)]
  230.             else:
  231.                 movieFound = False
  232.                 release = re.search('<strong>(.*?)</strong>', row)
  233.                 if release:
  234.                     release = release.group(1)
  235.                     name += ": %s" % release
  236.                 if link and name and iso:
  237.                     if trans:
  238.                         name += " ~ %s" % trans
  239.                     sub = self.sub(name, iso)
  240.                     sub.download(link)
  241.                     sub.rating = quality
  242.                     self.priority = priority
  243.                     self.addsub(sub)
  244.  
  245.     def scraperesult(self, page):
  246.         # if we are here we must have a year
  247.         divs = re.findall('<div class="col-md-9 col-sm-9 translate_list-right2">(.*?)</span>[\r\n|\r|\n]<a', page, re.DOTALL)
  248.         nname = norm(self.item.title).decode('utf-8')
  249.         for div in divs:
  250.             rlinkname = re.search('<a href="(.*?)"><h4>(.*?)<\/h4><\/a>(\r\n|\r|\n)<h5>.*?([0-9]{4}).*?<\/h5>', div)
  251.             if rlinkname:
  252.                 link = rlinkname.group(1)
  253.                 name = rlinkname.group(2)
  254.                 year = rlinkname.group(4)
  255.                 if year.isdigit():
  256.                     year = int(year)
  257.             else:
  258.                 continue
  259.             akas = []
  260.             aka = re.search('Aka:</span>(.*?)<br>', div)
  261.             if aka:
  262.                 akas = [norm(x.strip()) for x in aka.group(1).split(",")]
  263.  
  264.             if year == self.item.year and \
  265.                     (nname == norm(name) or nname in akas):
  266.                 page = self.request(domain + link)
  267.                 self.scrapemovie(page)
  268.                 break
  269.  
  270.     def searchimdb(self):
  271.         if self.item.season < 0:
  272.             season = ""
  273.         else:
  274.             season = self.item.season
  275.         query = {
  276.                  "title": self.item.imdb,
  277.                  "translator": "",
  278.                  "name": "",
  279.                  "release_info": "",
  280.                  "fps": "",
  281.                  "season": season,
  282.                  "episode": "",
  283.                  "lang": "",
  284.                  }
  285.  
  286.         page = self.request(domain + "/subtitlelist", query)
  287.         return self.scrapesubs(page)
  288.  
  289.     def searchnameyear(self):
  290.         # if we are here we must have a year
  291.         query = {
  292.                  "title": self.item.title,
  293.                  "year_date": self.item.year,
  294.                  "is_serial": int(self.item.show)
  295.                  }
  296.         page = self.request(domain + "/movie/search", query)
  297.         title = re.search("<title>(.*?)</title>", page)
  298.         if title:
  299.             res = title.group(1)
  300.             if title.group(1) == u' Arama Sonuçları ':
  301.                 return self.scraperesult(page)
  302.         return self.scrapemovie(page)
  303.  
  304. #        if ismultiple:
  305. #            return self.scraperesult(page)
  306. #        else:
  307. #            return self.scrapemovie(page)
  308.  
  309.     def searchpredict(self):
  310.         if self.item.season < 0:
  311.             season = ""
  312.         else:
  313.             season = self.item.season
  314.         query = {
  315.                  "title": self.item.title,
  316.                  "translator": "",
  317.                  "name": "",
  318.                  "release_info": "",
  319.                  "fps": "",
  320.                  "season": season,
  321.                  "episode": "",
  322.                  "lang": "",
  323.                  }
  324.         page = self.request(domain + "/subtitlelist", query)
  325.         return self.scrapesubs(page)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement