diff --git a/lib/liblinkpeek.py b/lib/liblinkpeek.py index b113cd1..ab63f8c 100644 --- a/lib/liblinkpeek.py +++ b/lib/liblinkpeek.py @@ -1,20 +1,25 @@ #!/usr/bin/python from hashlib import md5 -from urllib import quote +import urllib.parse # signup for an APIKEY here: https://linkpeek.com/signup -APIKEY = '' -SECRET = '' +APIKEY = "" +SECRET = "" -def api_v1(uri, apikey=APIKEY, secret=SECRET, size='336', viewport='1024'): +def api_v1(uri, apikey=APIKEY, secret=SECRET, size="336", viewport="1024"): """LinkPeek.com API v1 helper function""" - token = md5(secret + uri + size).hexdigest() - return 'https://linkpeek.com/api/v1?uri={}&apikey={}&token={}&size={}&viewport={}'.format( - quote(uri), apikey, token, size, viewport) + token = md5( + secret.encode("utf-8") + + uri.encode("utf-8") + + size.encode("utf-8") + ).hexdigest() + return "https://linkpeek.com/api/v1?uri={}&apikey={}&token={}&size={}&viewport={}".format( + urllib.parse.quote(uri), apikey, token, size, viewport + ) -if __name__ == '__main__': +if __name__ == "__main__": """TESTS""" - print api_v1('remarkbox.com') - print api_v1('https://www.remarkbox.com', size='200x400') - print api_v1('https://www.remarkbox.com', size='400x200') + print(api_v1("remarkbox.com")) + print(api_v1("https://www.remarkbox.com", size="200x400")) + print(api_v1("https://www.remarkbox.com", size="400x200")) diff --git a/lib/rst_linkpeek.py b/lib/rst_linkpeek.py index 4546d49..d006545 100644 --- a/lib/rst_linkpeek.py +++ b/lib/rst_linkpeek.py @@ -9,7 +9,7 @@ For example: .. code-block:: restructuredtext .. linkpeek:: - uri = http://www.remarkbox.com + uri = https://www.remarkbox.com action = link-image size = 600x400 @@ -19,7 +19,7 @@ In Pelican static site generator, you can register this Docutils plugin in your # register linkpeek docutils directive. import sys - sys.path.append('lib') + sys.path.append("lib") import rst_linkpeek linkpeek = rst_linkpeek.LinkPeek linkpeek.apikey = LINKPEEK_APIKEY @@ -52,11 +52,11 @@ class LinkPeek(Directive): def api_call(self): return api_v1( - self.params['uri'], + self.params["uri"], self.apikey, self.secret, - self.params['size'], - self.params['viewport'], + self.params["size"], + self.params["viewport"], ) def html_image(self): @@ -64,27 +64,27 @@ class LinkPeek(Directive): html = '{}' return html.format( self.api_call(), - self.params['title'], - self.params['align'], - self.params['style'], - self.params['class'], - self.params['alt'], + self.params["title"], + self.params["align"], + self.params["style"], + self.params["class"], + self.params["alt"], ) def html_link_image(self): """return html link image markup.""" html = '{}' - return html.format(self.params['uri'], self.html_image()) + return html.format(self.params["uri"], self.html_image()) def _get_params(self): """ - turn [u'uri = linkpeek.com', u'size = 200x200'] - into {u'uri': u'linkpeek.com', u'size': u'200x200'} + turn [u"uri = linkpeek.com", u"size = 200x200"] + into {u"uri": u"linkpeek.com", u"size": u"200x200"} """ params = defaultdict(str) for c in self.content: # split on = and then remove leading/trailing whitespace. - key, value = c.split('=') + key, value = c.split("=") key = key.strip() value = value.strip() params[key] = value @@ -94,25 +94,24 @@ class LinkPeek(Directive): @property def action_registry(self): return { - 'image' : self.html_image, - 'link-image' : self.html_link_image, - '' : self.api_call, + "image" : self.html_image, + "link-image" : self.html_link_image, + "" : self.api_call, } def run(self): """this method is fired when rendering.""" self._get_params() - action = self.action_registry.get(self.params['action']) + action = self.action_registry.get(self.params["action"]) output = action() - return [nodes.raw('', output, format='html')] + return [nodes.raw("", output, format="html")] @classmethod def register(cls): """register this directive/class with docutils.""" # this allows us to modify apikey and secret attributes before registering. - directives.register_directive('linkpeek', cls) - + directives.register_directive("linkpeek", cls)