Commit 8cb591d1 authored by Alexandre Beloin's avatar Alexandre Beloin

Normalize all the line endings

parent 6bbe4fd5
# Set the default behavior, in case people don't have core.autocrlf set.
# Handle line endings automatically for files detected as text
# and leave all files detected as binary untouched.
* text=auto
#
# The above will handle all files NOT found below
#
#
## These files are text and should be normalized (Convert crlf => lf)
#
# git config
.gitattributes text
.gitignore text
# Documentation
*.md text
CHANGES text
# Startup script
init.* text
# Various
*.ini text
*.txt text
*.less text
*.h text
*.in text
# Python Source files
*.pxd text
*.py text
*.py3 text
*.pyw text
*.pyx text
# Cheetah template
*.tmpl text
# Web file
*.htm text
*.html text
*.css text
*.js text
*.xml text
#
## These files are binary and should be left untouched
#
# Python Binary files
*.db binary
*.p binary
*.pkl binary
*.pyc binary
*.pyd binary
*.pyo binary
# These files are binary and should be left untouched
# (binary is a macro for -text -diff)
*.png binary
*.jpg binary
*.jpeg binary
*.gif binary
*.ico binary
*.swf binary
*.gz binary
*.zip binary
*.7z binary
*.ttf binary
*.svg binary
*.woff binary
*.eot binary
*.rar binary
*.dll binary
*.lib
import re
import time
from hashlib import sha1
from base64 import b16encode, b32decode
import sickbeard
from sickbeard import logger
from sickbeard.exceptions import ex
from sickbeard.clients import http_error_code
from lib.bencode import bencode, bdecode
from lib import requests
from lib.requests import exceptions
from lib.bencode.BTL import BTFailure
class GenericClient(object):
def __init__(self, name, host=None, username=None, password=None):
self.name = name
self.username = sickbeard.TORRENT_USERNAME if username is None else username
self.password = sickbeard.TORRENT_PASSWORD if password is None else password
self.host = sickbeard.TORRENT_HOST if host is None else host
self.rpcurl = sickbeard.TORRENT_RPCURL
self.url = None
self.response = None
self.auth = None
self.last_time = time.time()
self.session = requests.session()
self.session.auth = (self.username, self.password)
def _request(self, method='get', params={}, data=None, files=None):
if time.time() > self.last_time + 1800 or not self.auth:
self.last_time = time.time()
self._get_auth()
logger.log(
self.name + u': Requested a ' + method.upper() + ' connection to url ' + self.url + ' with Params= ' + str(
params) + ' Data=' + str(data if data else 'None')[0:99] + (
'...' if len(data if data else 'None') > 200 else ''), logger.DEBUG)
logger.log(
self.name + u': Requested a ' + method.upper() + ' connection to url ' + self.url + ' with Params= ' + str(
params) + (
(' Data=' + str(data)[0:100] + ('...' if len(data) > 100 else '')) if data is not None else ""),
logger.DEBUG)
if not self.auth:
logger.log(self.name + u': Authentication Failed', logger.ERROR)
return False
try:
self.response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files,
timeout=120, verify=False)
except requests.exceptions.ConnectionError, e:
logger.log(self.name + u': Unable to connect ' + str(e), logger.ERROR)
return False
except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
logger.log(self.name + u': Invalid Host', logger.ERROR)
return False
except requests.exceptions.HTTPError, e:
logger.log(self.name + u': Invalid HTTP Request ' + str(e), logger.ERROR)
return False
except requests.exceptions.Timeout, e:
logger.log(self.name + u': Connection Timeout ' + str(e), logger.ERROR)
return False
except Exception, e:
logger.log(self.name + u': Unknown exception raised when send torrent to ' + self.name + ': ' + str(e),
logger.ERROR)
return False
if self.response.status_code == 401:
logger.log(self.name + u': Invalid Username or Password, check your config', logger.ERROR)
return False
if self.response.status_code in http_error_code.keys():
logger.log(self.name + u': ' + http_error_code[self.response.status_code], logger.DEBUG)
return False
logger.log(self.name + u': Response to ' + method.upper() + ' request is ' + self.response.text, logger.DEBUG)
return True
def _get_auth(self):
"""
This should be overridden and should return the auth_id needed for the client
"""
return None
def _add_torrent_uri(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is added via url (magnet or .torrent link)
"""
return False
def _add_torrent_file(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is added via result.content (only .torrent file)
"""
return False
def _set_torrent_label(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with label
"""
return True
def _set_torrent_ratio(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with ratio
"""
return True
def _set_torrent_seed_time(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with a seed time
"""
return True
def _set_torrent_priority(self, result):
"""
This should be overriden should return the True/False from the client
when a torrent is set with result.priority (-1 = low, 0 = normal, 1 = high)
"""
return True
def _set_torrent_path(self, torrent_path):
"""
This should be overridden should return the True/False from the client
when a torrent is set with path
"""
return True
def _set_torrent_pause(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with pause
"""
return True
def _get_torrent_hash(self, result):
if result.url.startswith('magnet'):
result.hash = re.findall('urn:btih:([\w]{32,40})', result.url)[0]
if len(result.hash) == 32:
result.hash = b16encode(b32decode(result.hash)).lower()
else:
if not result.content:
logger.log('Torrent without content', logger.ERROR)
raise Exception('Torrent without content')
try:
torrent_bdecode = bdecode(result.content)
except BTFailure as e:
logger.log('Unable to bdecode torrent', logger.ERROR)
logger.log('Torrent bencoded data: {0}'.format(str(result.content)), logger.DEBUG)
raise
try:
info = torrent_bdecode["info"]
except Exception as e:
logger.log('Unable to find info field in torrent', logger.ERROR)
raise
result.hash = sha1(bencode(info)).hexdigest()
return result
def sendTORRENT(self, result):
r_code = False
logger.log(u'Calling ' + self.name + ' Client', logger.DEBUG)
if not self._get_auth():
logger.log(self.name + u': Authentication Failed', logger.ERROR)
return r_code
try:
# Sets per provider seed ratio
result.ratio = result.provider.seedRatio()
# lazy fix for now, I'm sure we already do this somewhere else too
result = self._get_torrent_hash(result)
if result.url.startswith('magnet'):
r_code = self._add_torrent_uri(result)
else:
r_code = self._add_torrent_file(result)
if not r_code:
logger.log(self.name + u': Unable to send Torrent: Return code undefined', logger.ERROR)
return False
if not self._set_torrent_pause(result):
logger.log(self.name + u': Unable to set the pause for Torrent', logger.ERROR)
if not self._set_torrent_label(result):
logger.log(self.name + u': Unable to set the label for Torrent', logger.ERROR)
if not self._set_torrent_ratio(result):
logger.log(self.name + u': Unable to set the ratio for Torrent', logger.ERROR)
if not self._set_torrent_seed_time(result):
logger.log(self.name + u': Unable to set the seed time for Torrent', logger.ERROR)
if not self._set_torrent_path(result):
logger.log(self.name + u': Unable to set the path for Torrent', logger.ERROR)
if result.priority != 0 and not self._set_torrent_priority(result):
logger.log(self.name + u': Unable to set priority for Torrent', logger.ERROR)
except Exception, e:
logger.log(self.name + u': Failed Sending Torrent', logger.ERROR)
logger.log(self.name + u': Exception raised when sending torrent: ' + str(result) + u'. Error: ' + str(e), logger.DEBUG)
return r_code
return r_code
def testAuthentication(self):
try:
self.response = self.session.get(self.url, timeout=120, verify=False)
except requests.exceptions.ConnectionError, e:
return False, 'Error: ' + self.name + ' Connection Error'
except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
return False, 'Error: Invalid ' + self.name + ' host'
if self.response.status_code == 401:
return False, 'Error: Invalid ' + self.name + ' Username or Password, check your config!'
try:
self._get_auth()
if self.response.status_code == 200 and self.auth:
return True, 'Success: Connected and Authenticated'
else:
return False, 'Error: Unable to get ' + self.name + ' Authentication, check your config!'
except Exception:
return False, 'Error: Unable to connect to ' + self.name
import re
import time
from hashlib import sha1
from base64 import b16encode, b32decode
import sickbeard
from sickbeard import logger
from sickbeard.exceptions import ex
from sickbeard.clients import http_error_code
from lib.bencode import bencode, bdecode
from lib import requests
from lib.requests import exceptions
from lib.bencode.BTL import BTFailure
class GenericClient(object):
def __init__(self, name, host=None, username=None, password=None):
self.name = name
self.username = sickbeard.TORRENT_USERNAME if username is None else username
self.password = sickbeard.TORRENT_PASSWORD if password is None else password
self.host = sickbeard.TORRENT_HOST if host is None else host
self.rpcurl = sickbeard.TORRENT_RPCURL
self.url = None
self.response = None
self.auth = None
self.last_time = time.time()
self.session = requests.session()
self.session.auth = (self.username, self.password)
def _request(self, method='get', params={}, data=None, files=None):
if time.time() > self.last_time + 1800 or not self.auth:
self.last_time = time.time()
self._get_auth()
logger.log(
self.name + u': Requested a ' + method.upper() + ' connection to url ' + self.url + ' with Params= ' + str(
params) + ' Data=' + str(data if data else 'None')[0:99] + (
'...' if len(data if data else 'None') > 200 else ''), logger.DEBUG)
logger.log(
self.name + u': Requested a ' + method.upper() + ' connection to url ' + self.url + ' with Params= ' + str(
params) + (
(' Data=' + str(data)[0:100] + ('...' if len(data) > 100 else '')) if data is not None else ""),
logger.DEBUG)
if not self.auth:
logger.log(self.name + u': Authentication Failed', logger.ERROR)
return False
try:
self.response = self.session.__getattribute__(method)(self.url, params=params, data=data, files=files,
timeout=120, verify=False)
except requests.exceptions.ConnectionError, e:
logger.log(self.name + u': Unable to connect ' + str(e), logger.ERROR)
return False
except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
logger.log(self.name + u': Invalid Host', logger.ERROR)
return False
except requests.exceptions.HTTPError, e:
logger.log(self.name + u': Invalid HTTP Request ' + str(e), logger.ERROR)
return False
except requests.exceptions.Timeout, e:
logger.log(self.name + u': Connection Timeout ' + str(e), logger.ERROR)
return False
except Exception, e:
logger.log(self.name + u': Unknown exception raised when send torrent to ' + self.name + ': ' + str(e),
logger.ERROR)
return False
if self.response.status_code == 401:
logger.log(self.name + u': Invalid Username or Password, check your config', logger.ERROR)
return False
if self.response.status_code in http_error_code.keys():
logger.log(self.name + u': ' + http_error_code[self.response.status_code], logger.DEBUG)
return False
logger.log(self.name + u': Response to ' + method.upper() + ' request is ' + self.response.text, logger.DEBUG)
return True
def _get_auth(self):
"""
This should be overridden and should return the auth_id needed for the client
"""
return None
def _add_torrent_uri(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is added via url (magnet or .torrent link)
"""
return False
def _add_torrent_file(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is added via result.content (only .torrent file)
"""
return False
def _set_torrent_label(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with label
"""
return True
def _set_torrent_ratio(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with ratio
"""
return True
def _set_torrent_seed_time(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with a seed time
"""
return True
def _set_torrent_priority(self, result):
"""
This should be overriden should return the True/False from the client
when a torrent is set with result.priority (-1 = low, 0 = normal, 1 = high)
"""
return True
def _set_torrent_path(self, torrent_path):
"""
This should be overridden should return the True/False from the client
when a torrent is set with path
"""
return True
def _set_torrent_pause(self, result):
"""
This should be overridden should return the True/False from the client
when a torrent is set with pause
"""
return True
def _get_torrent_hash(self, result):
if result.url.startswith('magnet'):
result.hash = re.findall('urn:btih:([\w]{32,40})', result.url)[0]
if len(result.hash) == 32:
result.hash = b16encode(b32decode(result.hash)).lower()
else:
if not result.content:
logger.log('Torrent without content', logger.ERROR)
raise Exception('Torrent without content')
try:
torrent_bdecode = bdecode(result.content)
except BTFailure as e:
logger.log('Unable to bdecode torrent', logger.ERROR)
logger.log('Torrent bencoded data: {0}'.format(str(result.content)), logger.DEBUG)
raise
try:
info = torrent_bdecode["info"]
except Exception as e:
logger.log('Unable to find info field in torrent', logger.ERROR)
raise
result.hash = sha1(bencode(info)).hexdigest()
return result
def sendTORRENT(self, result):
r_code = False
logger.log(u'Calling ' + self.name + ' Client', logger.DEBUG)
if not self._get_auth():
logger.log(self.name + u': Authentication Failed', logger.ERROR)
return r_code
try:
# Sets per provider seed ratio
result.ratio = result.provider.seedRatio()
# lazy fix for now, I'm sure we already do this somewhere else too
result = self._get_torrent_hash(result)
if result.url.startswith('magnet'):
r_code = self._add_torrent_uri(result)
else:
r_code = self._add_torrent_file(result)
if not r_code:
logger.log(self.name + u': Unable to send Torrent: Return code undefined', logger.ERROR)
return False
if not self._set_torrent_pause(result):
logger.log(self.name + u': Unable to set the pause for Torrent', logger.ERROR)
if not self._set_torrent_label(result):
logger.log(self.name + u': Unable to set the label for Torrent', logger.ERROR)
if not self._set_torrent_ratio(result):
logger.log(self.name + u': Unable to set the ratio for Torrent', logger.ERROR)
if not self._set_torrent_seed_time(result):
logger.log(self.name + u': Unable to set the seed time for Torrent', logger.ERROR)
if not self._set_torrent_path(result):
logger.log(self.name + u': Unable to set the path for Torrent', logger.ERROR)
if result.priority != 0 and not self._set_torrent_priority(result):
logger.log(self.name + u': Unable to set priority for Torrent', logger.ERROR)
except Exception, e:
logger.log(self.name + u': Failed Sending Torrent', logger.ERROR)
logger.log(self.name + u': Exception raised when sending torrent: ' + str(result) + u'. Error: ' + str(e), logger.DEBUG)
return r_code
return r_code
def testAuthentication(self):
try:
self.response = self.session.get(self.url, timeout=120, verify=False)
except requests.exceptions.ConnectionError, e:
return False, 'Error: ' + self.name + ' Connection Error'
except (requests.exceptions.MissingSchema, requests.exceptions.InvalidURL):
return False, 'Error: Invalid ' + self.name + ' host'
if self.response.status_code == 401:
return False, 'Error: Invalid ' + self.name + ' Username or Password, check your config!'
try:
self._get_auth()
if self.response.status_code == 200 and self.auth:
return True, 'Success: Connected and Authenticated'
else:
return False, 'Error: Unable to get ' + self.name + ' Authentication, check your config!'
except Exception:
return False, 'Error: Unable to connect to ' + self.name
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
# Author: Nic Wolfe <[email protected]>
# URL: http://code.google.com/p/sickbeard/
#
# This file is part of SickRage.
#
# SickRage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SickRage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
import urllib
import re
try:
import xml.etree.cElementTree as etree
except ImportError:
import elementtree.ElementTree as etree
import sickbeard
import generic
from sickbeard.common import Quality