You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.1 KiB
154 lines
5.1 KiB
5 years ago
|
# -*- coding: utf-8 -*-
|
||
|
from __future__ import absolute_import
|
||
|
import logging
|
||
|
import io
|
||
|
import os
|
||
|
|
||
2 years ago
|
from requests import Session
|
||
5 years ago
|
from guessit import guessit
|
||
|
from subliminal_patch.providers import Provider
|
||
4 years ago
|
from subliminal_patch.subtitle import Subtitle, guess_matches
|
||
2 years ago
|
from subliminal.subtitle import SUBTITLE_EXTENSIONS, fix_line_ending
|
||
2 years ago
|
from subliminal.video import Episode, Movie
|
||
|
from subzero.language import Language
|
||
|
import urllib
|
||
|
import zipfile
|
||
5 years ago
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
class RegieLiveSubtitle(Subtitle):
|
||
|
"""RegieLive Subtitle."""
|
||
|
provider_name = 'regielive'
|
||
|
hash_verifiable = False
|
||
|
|
||
|
def __init__(self, filename, video, link, rating, language):
|
||
|
super(RegieLiveSubtitle, self).__init__(language)
|
||
|
self.filename = filename
|
||
|
self.page_link = link
|
||
|
self.video = video
|
||
|
self.rating = rating
|
||
|
self.language = language
|
||
3 years ago
|
self.release_info = filename
|
||
5 years ago
|
|
||
|
@property
|
||
|
def id(self):
|
||
|
return self.page_link
|
||
|
|
||
|
def get_matches(self, video):
|
||
4 years ago
|
type_ = "movie" if isinstance(video, Movie) else "episode"
|
||
5 years ago
|
matches = set()
|
||
2 years ago
|
subtitle_filename = self.filename.lower()
|
||
5 years ago
|
|
||
|
# episode
|
||
4 years ago
|
if type_ == "episode":
|
||
5 years ago
|
# already matched in search query
|
||
|
matches.update(['title', 'series', 'season', 'episode', 'year'])
|
||
|
# movie
|
||
4 years ago
|
else:
|
||
5 years ago
|
# already matched in search query
|
||
|
matches.update(['title', 'year'])
|
||
|
|
||
|
if video.release_group and video.release_group.lower() in subtitle_filename:
|
||
2 years ago
|
matches.update(['release_group', 'hash'])
|
||
5 years ago
|
|
||
4 years ago
|
matches |= guess_matches(video, guessit(self.filename, {"type": type_}))
|
||
5 years ago
|
|
||
|
return matches
|
||
|
|
||
|
|
||
|
class RegieLiveProvider(Provider):
|
||
|
"""RegieLive Provider."""
|
||
|
languages = {Language(l) for l in ['ron']}
|
||
|
language = list(languages)[0]
|
||
3 years ago
|
video_types = (Episode, Movie)
|
||
5 years ago
|
SEARCH_THROTTLE = 8
|
||
2 years ago
|
hash_verifiable = False
|
||
5 years ago
|
|
||
|
def __init__(self):
|
||
|
self.initialize()
|
||
|
|
||
|
def initialize(self):
|
||
|
self.session = Session()
|
||
2 years ago
|
self.url = 'https://api.regielive.ro/bazarr/search.php'
|
||
|
self.api = 'API-BAZARR-YTZ-SL'
|
||
5 years ago
|
self.headers = {'RL-API': self.api}
|
||
|
|
||
|
def terminate(self):
|
||
|
self.session.close()
|
||
|
|
||
|
def query(self, video, language):
|
||
2 years ago
|
payload = {}
|
||
|
if isinstance(video, Episode):
|
||
|
payload['nume'] = video.series
|
||
|
payload['sezon'] = video.season
|
||
|
payload['episod'] = video.episode
|
||
|
elif isinstance(video, Movie):
|
||
|
payload['nume'] = video.title
|
||
|
payload['an'] = video.year
|
||
|
|
||
|
response = self.session.get(
|
||
|
self.url + "?" + urllib.parse.urlencode(payload),
|
||
|
data=payload, headers=self.headers)
|
||
5 years ago
|
|
||
2 years ago
|
subtitles = []
|
||
|
if response.json()['cod'] == 200:
|
||
|
results_subs = response.json()['rezultate']
|
||
|
for film in results_subs:
|
||
|
for sub in results_subs[film]['subtitrari']:
|
||
|
subtitles.append(
|
||
|
RegieLiveSubtitle(
|
||
|
results_subs[film]['subtitrari'][sub]['titlu'],
|
||
|
video,
|
||
|
results_subs[film]['subtitrari'][sub]['url'],
|
||
|
results_subs[film]['subtitrari'][sub]['rating']['nota'],
|
||
|
language))
|
||
5 years ago
|
return subtitles
|
||
|
|
||
|
def list_subtitles(self, video, languages):
|
||
|
return self.query(video, self.language)
|
||
|
|
||
|
def download_subtitle(self, subtitle):
|
||
2 years ago
|
session = self.session
|
||
5 years ago
|
_addheaders = {
|
||
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:72.0) Gecko/20100101 Firefox/72.0',
|
||
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||
2 years ago
|
'Origin': 'https://subtitrari.regielive.ro',
|
||
|
'Accept-Language': 'en-US,en;q=0.5',
|
||
|
'Referer': 'https://subtitrari.regielive.ro',
|
||
5 years ago
|
'Pragma': 'no-cache',
|
||
|
'Cache-Control': 'no-cache'
|
||
|
}
|
||
|
session.headers.update(_addheaders)
|
||
2 years ago
|
res = session.get('https://subtitrari.regielive.ro')
|
||
|
cookies = res.cookies
|
||
5 years ago
|
_zipped = session.get(subtitle.page_link, cookies=cookies)
|
||
|
if _zipped:
|
||
|
if _zipped.text == '500':
|
||
|
raise ValueError('Error 500 on server')
|
||
|
archive = zipfile.ZipFile(io.BytesIO(_zipped.content))
|
||
|
subtitle_content = self._get_subtitle_from_archive(archive)
|
||
|
subtitle.content = fix_line_ending(subtitle_content)
|
||
|
|
||
|
return subtitle
|
||
|
raise ValueError('Problems conecting to the server')
|
||
|
|
||
|
def _get_subtitle_from_archive(self, archive):
|
||
|
# some files have a non subtitle with .txt extension
|
||
|
_tmp = list(SUBTITLE_EXTENSIONS)
|
||
|
_tmp.remove('.txt')
|
||
|
_subtitle_extensions = tuple(_tmp)
|
||
|
|
||
|
for name in archive.namelist():
|
||
|
# discard hidden files
|
||
|
if os.path.split(name)[-1].startswith('.'):
|
||
|
continue
|
||
|
|
||
|
# discard non-subtitle files
|
||
|
if not name.lower().endswith(_subtitle_extensions):
|
||
|
continue
|
||
|
|
||
|
return archive.read(name)
|
||
|
|
||
|
raise APIThrottled('Can not find the subtitle in the compressed file')
|