<commit_before># coding: utf8
from couchpotato.core.helpers.encoding import simplifyString
from couchpotato.core.helpers.variable import getIdentifier
from couchpotato.core.logger import CPLog
from couchpotato.core.media._base.providers.torrent.base import TorrentProvider
from couchpotato.core.media.movie.providers.base import MovieProvider
from couchpotato.environment import Env
import datetime
import json
import requests
import traceback
import urllib

class T411(TorrentProvider, MovieProvider):
    """
    Couchpotato plugin to search movies torrents using T411 APIs. More information about T411 APIs on 
    https://api.t411.ch.
    """

    urlProtocol = 'https'
    basePathApi = 'api.t411.ch'
    basePathWww = 'www.t411.ch'
    tokenTTL = 90 # T411 authentication token TTL = 90 days
    tokenTimestamp = None
    headers = {}
    proxies = {}
    urls = {}
    log = CPLog(__name__)

    def __init__(self):
        """
        Default constructor
        """
        TorrentProvider.__init__(self)
        MovieProvider.__init__(self)
        self.urls['login'] = self.urlProtocol+'://'+self.basePathApi+'/auth'
        if(Env.setting('use_proxy')):
            proxy_server = Env.setting('proxy_server')
            proxy_username = Env.setting('proxy_username')
            proxy_password = Env.setting('proxy_password')
            if(proxy_server):
                loc = '{0}:{1}@{2}'.format(proxy_username, proxy_password, proxy_server) if proxy_username else proxy_server
                self.proxies = {
                    "http": "http://"+loc,
                    "https": "https://"+loc,
                }
            else:
                self.proxies = urllib.getproxies()

    def urlopen(self, method, url, params=None, headers=None, proxies=None, data=None):
        """
        Proxyfi request to T411. T411 API reject all requests with a 'User-Agent' HTTP header, it's why we don't use 
        the couchpotato.core.plugins.base.py#Plugin.urlopen(...) method. Furthermore Plugin.urlopen(...) don't let
        the caller handle the HTTP method. We also don't use the session hide behind the Env.get('http_opener') 
        function, T411 API calls are stateless.
        """
        response = method(url, params=params, headers=headers, proxies=proxies, data=data, timeout=30)
        response.raise_for_status()
        error = response.json().get('error')
        if(error is not None):
            code = response.json().get('code')
            raise T411Error(code, error)
        return response

    def loginDownload(self, url='', nzb_id=''):
        """
        Override couchpotato.core.media._base.providers.base.py#YarrProvider.loginDownload(...) method.
        It log to the T411 torrents provider and retrieve the torrent file researched as binary data.
        """
        result = None
        try:
            if(self.login()):
                result = self.urlopen(requests.get, url, headers=self.headers, proxies=self.proxies).content
        except:
            self.log.error('Failed getting release from %s: %s', (self.getName(), traceback.format_exc()))
        return result

    def getToken(self):
        """
        Get T411 HTTP authentication header.
        """
        now = datetime.datetime.now()
        if(self.tokenTimestamp is None) or ((now - self.tokenTimestamp).days >= self.tokenTTL):
            login = {
                'username': self.conf('username'),<commit_msg>Don't check HTTP response when downloading<commit_after># coding: utf8
from couchpotato.core.helpers.encoding import simplifyString
from couchpotato.core.helpers.variable import getIdentifier
from couchpotato.core.logger import CPLog
from couchpotato.core.media._base.providers.torrent.base import TorrentProvider
from couchpotato.core.media.movie.providers.base import MovieProvider
from couchpotato.environment import Env
import datetime
import json
import requests
import traceback
import urllib

class T411(TorrentProvider, MovieProvider):
    """
    Couchpotato plugin to search movies torrents using T411 APIs. More information about T411 APIs on 
    https://api.t411.ch.
    """

    urlProtocol = 'https'
    basePathApi = 'api.t411.ch'
    basePathWww = 'www.t411.ch'
    tokenTTL = 90 # T411 authentication token TTL = 90 days
    tokenTimestamp = None
    headers = {}
    proxies = {}
    urls = {}
    log = CPLog(__name__)

    def __init__(self):
        """
        Default constructor
        """
        TorrentProvider.__init__(self)
        MovieProvider.__init__(self)
        self.urls['login'] = self.urlProtocol+'://'+self.basePathApi+'/auth'
        if(Env.setting('use_proxy')):
            proxy_server = Env.setting('proxy_server')
            proxy_username = Env.setting('proxy_username')
            proxy_password = <PASSWORD>.setting('proxy_password')
            if(proxy_server):
                loc = '{0}:{1}@{2}'.format(proxy_username, proxy_password, proxy_server) if proxy_username else proxy_server
                self.proxies = {
                    "http": "http://"+loc,
                    "https": "https://"+loc,
                }
            else:
                self.proxies = urllib.getproxies()

    def urlopen(self, method, url, params=None, headers=None, proxies=None, data=None, check=True):
        """
        Proxyfi request to T411. T411 API reject all requests with a 'User-Agent' HTTP header, it's why we don't use 
        the couchpotato.core.plugins.base.py#Plugin.urlopen(...) method. Furthermore Plugin.urlopen(...) don't let
        the caller handle the HTTP method. We also don't use the session hide behind the Env.get('http_opener') 
        function, T411 API calls are stateless.
        """
        response = method(url, params=params, headers=headers, proxies=proxies, data=data, timeout=30)
        response.raise_for_status()
        if(check):
            error = response.json().get('error')
            if(error):
                code = response.json().get('code')
                raise T411Error(code, error)
        return response

    def loginDownload(self, url='', nzb_id=''):
        """
        Override couchpotato.core.media._base.providers.base.py#YarrProvider.loginDownload(...) method.
        It log to the T411 torrents provider and retrieve the torrent file researched as binary data.
        """
        result = None
        try:
            if(self.login()):
                result = self.urlopen(requests.get, url, headers=self.headers, proxies=self.proxies, check=False).content
        except:
            self.log.error('Failed getting release from %s: %s', (self.getName(), traceback.format_exc()))
        return result

    def getToken(self):
        """
        Get T411 HTTP authentication header.
        """
        now = datetime.datetime.now()
        if(self.tokenTimestamp is None) or ((now - self.tokenTimestamp).days >= self.tokenTTL):
            login = {
                'username': self.conf('username'),
{'fraction_non_alphanumeric': 0.11238532110091744, 'fraction_numerical': 0.01404816513761468, 'mean_word_length': 2.5237373737373736, 'pattern_counts': {'":': 4, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2701121', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1923, 'n_words': 500}