kopia lustrzana https://github.com/jcarbaugh/python-webfinger
remove whitespace
rodzic
142bba7a6d
commit
aa748195bf
|
@ -21,36 +21,36 @@ class WebFingerExpection(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class WebFingerResponse(object):
|
class WebFingerResponse(object):
|
||||||
|
|
||||||
def __init__(self, xrd, insecure):
|
def __init__(self, xrd, insecure):
|
||||||
self.insecure = insecure
|
self.insecure = insecure
|
||||||
self._xrd = xrd
|
self._xrd = xrd
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
if name in RELS:
|
if name in RELS:
|
||||||
return self._xrd.find_link(RELS[name], attr='href')
|
return self._xrd.find_link(RELS[name], attr='href')
|
||||||
return getattr(self._xrd, name)
|
return getattr(self._xrd, name)
|
||||||
|
|
||||||
class WebFingerClient(object):
|
class WebFingerClient(object):
|
||||||
|
|
||||||
def __init__(self, host):
|
def __init__(self, host):
|
||||||
self._host = host
|
self._host = host
|
||||||
self._opener = urllib2.build_opener(urllib2.HTTPRedirectHandler())
|
self._opener = urllib2.build_opener(urllib2.HTTPRedirectHandler())
|
||||||
self._opener.addheaders = [('User-agent', 'python-webfinger')]
|
self._opener.addheaders = [('User-agent', 'python-webfinger')]
|
||||||
|
|
||||||
def _hm_hosts(self, xrd):
|
def _hm_hosts(self, xrd):
|
||||||
return [e.value for e in xrd.elements if e.name == 'hm:Host']
|
return [e.value for e in xrd.elements if e.name == 'hm:Host']
|
||||||
|
|
||||||
def xrd(self, url, raw=False):
|
def xrd(self, url, raw=False):
|
||||||
conn = self._opener.open(url)
|
conn = self._opener.open(url)
|
||||||
response = conn.read()
|
response = conn.read()
|
||||||
conn.close()
|
conn.close()
|
||||||
return response if raw else XRD.parse(response)
|
return response if raw else XRD.parse(response)
|
||||||
|
|
||||||
def hostmeta(self, protocol):
|
def hostmeta(self, protocol):
|
||||||
hostmeta_url = "%s://%s/.well-known/host-meta" % (protocol, self._host)
|
hostmeta_url = "%s://%s/.well-known/host-meta" % (protocol, self._host)
|
||||||
return self.xrd(hostmeta_url)
|
return self.xrd(hostmeta_url)
|
||||||
|
|
||||||
def finger(self, username):
|
def finger(self, username):
|
||||||
try:
|
try:
|
||||||
hm = self.hostmeta('https')
|
hm = self.hostmeta('https')
|
||||||
|
@ -60,16 +60,16 @@ class WebFingerClient(object):
|
||||||
insecure = True
|
insecure = True
|
||||||
|
|
||||||
hm_hosts = self._hm_hosts(hm)
|
hm_hosts = self._hm_hosts(hm)
|
||||||
|
|
||||||
if self._host not in hm_hosts:
|
if self._host not in hm_hosts:
|
||||||
raise WebFingerExpection("hostmeta host did not match account host")
|
raise WebFingerExpection("hostmeta host did not match account host")
|
||||||
|
|
||||||
template = hm.find_link(WEBFINGER_TYPES, attr='template')
|
template = hm.find_link(WEBFINGER_TYPES, attr='template')
|
||||||
if not template.startswith('https://'):
|
if not template.startswith('https://'):
|
||||||
insecure = True
|
insecure = True
|
||||||
xrd_url = template.replace('{uri}',
|
xrd_url = template.replace('{uri}',
|
||||||
urllib.quote_plus('acct:%s@%s' % (username, self._host)))
|
urllib.quote_plus('acct:%s@%s' % (username, self._host)))
|
||||||
|
|
||||||
data = self.xrd(xrd_url)
|
data = self.xrd(xrd_url)
|
||||||
return WebFingerResponse(data, insecure)
|
return WebFingerResponse(data, insecure)
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue