Replaced urllib2 in mb, notifiers, postprocessor, searcher and transmission

This commit is contained in:
Bas Stottelaar
2014-04-06 16:16:04 +02:00
parent 55796324b6
commit 9544b0e616
5 changed files with 79 additions and 128 deletions

View File

@@ -67,7 +67,7 @@ def startmb():
else:
musicbrainzngs.hpauth(mbuser,mbpass)
logger.debug('Using the following server values:\nMBHost: %s ; MBPort: %i ; Sleep Interval: %i ' % (mbhost, mbport, sleepytime))
logger.debug('Using the following server values: MBHost: %s, MBPort: %i, Sleep Interval: %i', mbhost, mbport, sleepytime)
return True

View File

@@ -169,66 +169,34 @@ class PROWL:
class XBMC:
def __init__(self):
self.hosts = headphones.XBMC_HOST
self.username = headphones.XBMC_USERNAME
self.password = headphones.XBMC_PASSWORD
def _sendhttp(self, host, command):
username = self.username
password = self.password
url_command = urllib.urlencode(command)
url = host + '/xbmcCmds/xbmcHttp/?' + url_command
req = urllib2.Request(url)
if password:
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
logger.info('XBMC url: %s' % url)
try:
handle = urllib2.urlopen(req)
except Exception, e:
logger.warn('Error opening XBMC url: %s' % e)
return
response = handle.read().decode(headphones.SYS_ENCODING)
return response
if self.password:
return helpers.request_content(url, auth=(self.username, self.password))
else:
return helpers.request_content(url)
def _sendjson(self, host, method, params={}):
data = [{'id': 0, 'jsonrpc': '2.0', 'method': method, 'params': params}]
data = simplejson.JSONEncoder().encode(data)
headers = {'Content-Type': 'application/json'}
url = host + '/jsonrpc'
content = {'Content-Type': 'application/json', 'Content-Length': len(data)}
if self.password:
response = helpers.request_json(req, method="POST", data=simplejson.dumps(data), headers=headers, auth=(self.username, self.password))
else:
response = helpers.request_json(req, method="POST", data=simplejson.dumps(data), headers=headers)
req = urllib2.Request(host+'/jsonrpc', data, content)
if self.username and self.password:
base64string = base64.encodestring('%s:%s' % (self.username, self.password)).replace('\n', '')
req.add_header("Authorization", "Basic %s" % base64string)
try:
handle = urllib2.urlopen(req)
except Exception, e:
logger.warn('Error opening XBMC url: %s' % e)
return
response = simplejson.JSONDecoder().decode(handle.read())
try:
if response:
return response[0]['result']
except:
logger.warn('XBMC returned error: %s' % response[0]['error'])
return
def update(self):
# From what I read you can't update the music library on a per directory or per path basis
# so need to update the whole thing
@@ -364,21 +332,7 @@ class NMA:
self.priority = headphones.NMA_PRIORITY
def _send(self, data):
url_data = urllib.urlencode(data)
url = 'https://www.notifymyandroid.com/publicapi/notify'
req = urllib2.Request(url, url_data)
try:
handle = urllib2.urlopen(req)
except Exception, e:
logger.warn('Error opening NotifyMyAndroid url: ' % e)
return
response = handle.read().decode(headphones.SYS_ENCODING)
return response
return helpers.request_content('https://www.notifymyandroid.com/publicapi/notify', data=data)
def notify(self, artist=None, album=None, snatched_nzb=None):

View File

@@ -19,7 +19,7 @@ import os
import time
import threading
import music_encoder
import urllib, shutil, re
import shutil, re
import uuid
from headphones import notifiers
import beets
@@ -360,9 +360,9 @@ def doPostProcessing(albumid, albumpath, release, tracks, downloaded_track_list,
artwork = None
album_art_path = albumart.getAlbumArt(albumid)
if headphones.EMBED_ALBUM_ART or headphones.ADD_ALBUM_ART:
if album_art_path:
artwork = urllib.urlopen(album_art_path).read()
if not album_art_path or len(artwork) < 100:
artwork = helpers.request_content(album_art_path)
if not album_art_path or not artwor or len(artwork) < 100:
logger.info("No suitable album art found from Amazon. Checking Last.FM....")
artwork = albumart.getCachedArt(albumid)
if not artwork or len(artwork) < 100:

View File

@@ -302,7 +302,7 @@ def searchNZB(album, new=False, losslessOnly=False):
# Request results
logger.info('Parsing results from Headphones Indexer')
headers = { 'User-Agent', USER_AGENT }
headers = { 'User-Agent': USER_AGENT }
params = {
"t": "search",
"cat": categories,
@@ -368,7 +368,7 @@ def searchNZB(album, new=False, losslessOnly=False):
# Request results
logger.info('Parsing results from %s', newznab_host[0])
headers = { 'User-Agent', USER_AGENT }
headers = { 'User-Agent': USER_AGENT }
params = {
"t": "search",
"apikey": newznab_host[1],
@@ -415,7 +415,7 @@ def searchNZB(album, new=False, losslessOnly=False):
# Request results
logger.info('Parsing results from nzbs.org')
headers = { 'User-Agent', USER_AGENT }
headers = { 'User-Agent': USER_AGENT }
params = {
"t": "search",
"apikey": headphones.NZBSORG_HASH,
@@ -464,7 +464,7 @@ def searchNZB(album, new=False, losslessOnly=False):
# Request results
logger.info('Parsing results from NZBsRus')
headers = { 'User-Agent', USER_AGENT }
headers = { 'User-Agent': USER_AGENT }
params = {
"uid": headphones.NZBSRUS_UID,
"key": headphones.NZBSRUS_APIKEY,
@@ -515,7 +515,7 @@ def searchNZB(album, new=False, losslessOnly=False):
# Request results
logger.info('Parsing results from omgwtfnzbs')
headers = { 'User-Agent', USER_AGENT }
headers = { 'User-Agent': USER_AGENT }
params = {
"user": headphones.OMGWTFNZBS_UID,
"api": headphones.OMGWTFNZBS_APIKEY,
@@ -750,7 +750,7 @@ def getresultNZB(result):
auth=(headphones.HPUSER, headphones.HPPASS),
params={"username": headphones.NEWZBIN_UID, "password": headphones.NEWZBIN_PASSWORD, "reportid": result[2]},
headers={'User-Agent': USER_AGENT},
status_pass=[400]
status_pass=400
)
if response.status_code == 400:
@@ -875,10 +875,10 @@ def searchTorrent(album, new=False, losslessOnly=False):
# Process feed
if data:
if not len(d.entries):
if not len(data.entries):
logger.info(u"No results found from %s for %s" % provider, term)
else:
for item in d.entries:
for item in data.entries:
try:
rightformat = True
title = item['title']
@@ -948,11 +948,10 @@ def searchTorrent(album, new=False, losslessOnly=False):
# Process feed
if data:
if not len(d.entries):
if not len(data.entries):
logger.info(u"No results found from %s for %s" % (provider, term))
else:
for item in d.entries:
for item in data.entries:
try:
title = item.title
desc_match = re.search(r"Size: (\d+)<", item.description)
@@ -1128,7 +1127,7 @@ def searchTorrent(album, new=False, losslessOnly=False):
"sort": "seeds"
}
data = request_soup(
data = helpers.request_soup(
url=providerurl + category,
params=params,
timeout=20
@@ -1184,7 +1183,7 @@ def searchTorrent(album, new=False, losslessOnly=False):
# Requesting content
logger.info('Parsing results from ISOHunt')
headers = { 'User-Agent', USER_AGENT }
headers = { 'User-Agent': USER_AGENT }
params = {
"iht": "2",
"sort": "seeds"
@@ -1257,10 +1256,10 @@ def searchTorrent(album, new=False, losslessOnly=False):
# Process feed
if data:
if not len(d.entries):
if not len(data.entries):
logger.info(u"No results found from %s for %s" % (provider, term))
else:
for item in d.entries:
for item in data.entries:
try:
rightformat = True
title = item.title
@@ -1312,6 +1311,8 @@ def preprocess(resultlist):
headers = { 'Referer': 'http://kat.ph/' }
elif result[3] == 'What.cd':
headers = { 'User-Agent': 'Headphones' }
else:
headers = {}
return helpers.request_content(url=result[2], headers=headers), result

View File

@@ -13,30 +13,30 @@
# You should have received a copy of the GNU General Public License
# along with Headphones. If not, see <http://www.gnu.org/licenses/>.
import headphones
from headphones import logger, notifiers
import urllib2
import lib.simplejson as json
import base64
import time
import re
import time
import base64
import headphones
import simplejson as json
from headphones import logger, notifiers, helpers
# This is just a simple script to send torrents to transmission. The
# intention is to turn this into a class where we can check the state
# of the download, set the download dir, etc.
# of the download, set the download dir, etc.
# TODO: Store the session id so we don't need to make 2 calls
# Store torrent id so we can check up on it
def addTorrent(link):
method = 'torrent-add'
arguments = {'filename': link, 'download-dir':headphones.DOWNLOAD_TORRENT_DIR}
arguments = {'filename': link, 'download-dir': headphones.DOWNLOAD_TORRENT_DIR}
response = torrentAction(method,arguments)
if not response:
return False
if response['result'] == 'success':
if 'torrent-added' in response['arguments']:
name = response['arguments']['torrent-added']['name']
@@ -75,38 +75,38 @@ def addTorrent(link):
pushalot.notify(name,"Download started")
return retid
else:
logger.info('Transmission returned status %s' % response['result'])
return False
def getTorrentFolder(torrentid):
method = 'torrent-get'
arguments = { 'ids': torrentid, 'fields': ['name','percentDone']}
response = torrentAction(method, arguments)
percentdone = response['arguments']['torrents'][0]['percentDone']
torrent_folder_name = response['arguments']['torrents'][0]['name']
tries = 1
while percentdone == 0 and tries <10:
tries+=1
time.sleep(5)
response = torrentAction(method, arguments)
percentdone = response['arguments']['torrents'][0]['percentDone']
torrent_folder_name = response['arguments']['torrents'][0]['name']
return torrent_folder_name
def torrentAction(method, arguments):
host = headphones.TRANSMISSION_HOST
username = headphones.TRANSMISSION_USERNAME
password = headphones.TRANSMISSION_PASSWORD
sessionid = None
if not host.startswith('http'):
host = 'http://' + host
@@ -131,36 +131,32 @@ def torrentAction(method, arguments):
logger.error('Transmission port missing')
return
request = urllib2.Request(host)
# Retrieve session id
if username and password:
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
opener = urllib2.build_opener()
try:
data = opener.open(request).read()
except urllib2.HTTPError, e:
if e.code == 409:
sessionid = e.hdrs['x-transmission-session-id']
else:
logger.error('Could not connect to Transmission. Error: ' + str(e))
except Exception, e:
logger.error('Could not connect to Transmission. Error: ' + str(e))
response = helpers.request_response(host, auth=(username, password), status_pass=409)
else:
response = helpers.request_response(host, status, status_pass=409)
if not response:
logger.error("Error gettings Transmission session ID")
return
# Parse session id
if response.status_code == 409:
sessionid = response.headers['x-transmission-session-id']
if not sessionid:
logger.error("Error getting Session ID from Transmission")
return
request.add_header('x-transmission-session-id', sessionid)
postdata = json.dumps({ 'method': method,
'arguments': arguments })
request.add_data(postdata)
try:
response = json.loads(opener.open(request).read())
except Exception, e:
logger.error("Error sending torrent to Transmission: " + str(e))
# Prepare next request
headers = { 'x-transmission-session-id': sessionid }
data = { 'method': method, 'arguments': arguments }
response = helpers.request_content(host, data=json.dumps(data), headers=headers)
if not response:
logger.error("Error sending torrent to Transmission")
return
return response