Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switched from http.client to urllib for fetching external resources -> moved from old pull request #174 #180

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 8 additions & 20 deletions lib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import math
import subprocess
import time
import urllib.request

logger = logging.getLogger('')

Expand Down Expand Up @@ -56,32 +57,19 @@ def dt2ts(self, dt):
return time.mktime(dt.timetuple())

def fetch_url(self, url, username=None, password=None, timeout=2):
headers = {'Accept': 'text/plain'}
plain = True
if url.startswith('https'):
plain = False
lurl = url.split('/')
host = lurl[2]
purl = '/' + '/'.join(lurl[3:])
if plain:
conn = http.client.HTTPConnection(host, timeout=timeout)
else:
conn = http.client.HTTPSConnection(host, timeout=timeout)
req = urllib.request.Request(url)
req.add_header = ('Accept','text/plain')
if username and password:
headers['Authorization'] = ('Basic '.encode() + base64.b64encode((username + ':' + password).encode()))
try:
conn.request("GET", purl, headers=headers)
except Exception as e:
logger.warning("Problem fetching {0}: {1}".format(url, e))
conn.close()
return False
resp = conn.getresponse()
req.add_header = ('Authorization', 'Basic '.encode() + base64.b64encode((username + ':' + password).encode()))

resp = urllib.request.urlopen(req, timeout=timeout)

if resp.status == 200:
content = resp.read()
else:
logger.warning("Problem fetching {0}: {1} {2}".format(url, resp.status, resp.reason))
content = False
conn.close()
return content

def rel2abs(self, t, rf):
Expand Down
17 changes: 9 additions & 8 deletions plugins/prowl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import logging
import urllib.parse
import http.client
import urllib.request

logger = logging.getLogger('Prowl')


class Prowl():
_host = 'api.prowlapp.com'
_api = '/publicapi/add'

def __init__(self, smarthome, apikey=None):
self._apikey = apikey
self._sh = smarthome
Expand All @@ -42,7 +40,6 @@ def stop(self):

def __call__(self, event='', description='', priority=None, url=None, apikey=None, application='SmartHome'):
data = {}
headers = {'User-Agent': "SmartHome.py", 'Content-Type': "application/x-www-form-urlencoded"}
data['event'] = event[:1024].encode()
data['description'] = description[:10000].encode()
data['application'] = application[:256].encode()
Expand All @@ -55,10 +52,14 @@ def __call__(self, event='', description='', priority=None, url=None, apikey=Non
if url:
data['url'] = url[:512]
try:
conn = http.client.HTTPSConnection(self._host, timeout=4)
conn.request("POST", self._api, urllib.parse.urlencode(data), headers)
resp = conn.getresponse()
conn.close()
apiurl='https://api.prowlapp.com/publicapi/add'
data = urllib.parse.urlencode(data)
data = data.encode('utf-8')
req = urllib.request.Request(apiurl, data)
req.add_header = ('User-Agent','SmartHome.py')
req.add_header = ('Content-Type','application/x-www-form-urlencoded;charset=utf-8')

resp = urllib.request.urlopen(req,timeout=4)
if resp.status != 200:
raise Exception("{} {}".format(resp.status, resp.reason))
except Exception as e:
Expand Down