move some libs to python3

modified:   lib/liblinkpeek.py
	modified:   lib/rst_linkpeek.py
This commit is contained in:
russellballestrini 2020-09-01 21:36:36 -04:00
parent 7cd288aa0c
commit 35606a10b5
2 changed files with 36 additions and 32 deletions

View file

@ -1,20 +1,25 @@
#!/usr/bin/python #!/usr/bin/python
from hashlib import md5 from hashlib import md5
from urllib import quote import urllib.parse
# signup for an APIKEY here: https://linkpeek.com/signup # signup for an APIKEY here: https://linkpeek.com/signup
APIKEY = '' APIKEY = ""
SECRET = '' 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""" """LinkPeek.com API v1 helper function"""
token = md5(secret + uri + size).hexdigest() token = md5(
return 'https://linkpeek.com/api/v1?uri={}&apikey={}&token={}&size={}&viewport={}'.format( secret.encode("utf-8") +
quote(uri), apikey, token, size, viewport) 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""" """TESTS"""
print api_v1('remarkbox.com') print(api_v1("remarkbox.com"))
print api_v1('https://www.remarkbox.com', size='200x400') print(api_v1("https://www.remarkbox.com", size="200x400"))
print api_v1('https://www.remarkbox.com', size='400x200') print(api_v1("https://www.remarkbox.com", size="400x200"))

View file

@ -9,7 +9,7 @@ For example:
.. code-block:: restructuredtext .. code-block:: restructuredtext
.. linkpeek:: .. linkpeek::
uri = http://www.remarkbox.com uri = https://www.remarkbox.com
action = link-image action = link-image
size = 600x400 size = 600x400
@ -19,7 +19,7 @@ In Pelican static site generator, you can register this Docutils plugin in your
# register linkpeek docutils directive. # register linkpeek docutils directive.
import sys import sys
sys.path.append('lib') sys.path.append("lib")
import rst_linkpeek import rst_linkpeek
linkpeek = rst_linkpeek.LinkPeek linkpeek = rst_linkpeek.LinkPeek
linkpeek.apikey = LINKPEEK_APIKEY linkpeek.apikey = LINKPEEK_APIKEY
@ -52,11 +52,11 @@ class LinkPeek(Directive):
def api_call(self): def api_call(self):
return api_v1( return api_v1(
self.params['uri'], self.params["uri"],
self.apikey, self.apikey,
self.secret, self.secret,
self.params['size'], self.params["size"],
self.params['viewport'], self.params["viewport"],
) )
def html_image(self): def html_image(self):
@ -64,27 +64,27 @@ class LinkPeek(Directive):
html = '<img src="{}" title="{}" align="{}" style="{}" class="{}" alt="{}" />' html = '<img src="{}" title="{}" align="{}" style="{}" class="{}" alt="{}" />'
return html.format( return html.format(
self.api_call(), self.api_call(),
self.params['title'], self.params["title"],
self.params['align'], self.params["align"],
self.params['style'], self.params["style"],
self.params['class'], self.params["class"],
self.params['alt'], self.params["alt"],
) )
def html_link_image(self): def html_link_image(self):
"""return html link image markup.""" """return html link image markup."""
html = '<a href="{}" target="_blank">{}</a>' html = '<a href="{}" target="_blank">{}</a>'
return html.format(self.params['uri'], self.html_image()) return html.format(self.params["uri"], self.html_image())
def _get_params(self): def _get_params(self):
""" """
turn [u'uri = linkpeek.com', u'size = 200x200'] turn [u"uri = linkpeek.com", u"size = 200x200"]
into {u'uri': u'linkpeek.com', u'size': u'200x200'} into {u"uri": u"linkpeek.com", u"size": u"200x200"}
""" """
params = defaultdict(str) params = defaultdict(str)
for c in self.content: for c in self.content:
# split on = and then remove leading/trailing whitespace. # split on = and then remove leading/trailing whitespace.
key, value = c.split('=') key, value = c.split("=")
key = key.strip() key = key.strip()
value = value.strip() value = value.strip()
params[key] = value params[key] = value
@ -94,25 +94,24 @@ class LinkPeek(Directive):
@property @property
def action_registry(self): def action_registry(self):
return { return {
'image' : self.html_image, "image" : self.html_image,
'link-image' : self.html_link_image, "link-image" : self.html_link_image,
'' : self.api_call, "" : self.api_call,
} }
def run(self): def run(self):
"""this method is fired when rendering.""" """this method is fired when rendering."""
self._get_params() self._get_params()
action = self.action_registry.get(self.params['action']) action = self.action_registry.get(self.params["action"])
output = action() output = action()
return [nodes.raw('', output, format='html')] return [nodes.raw("", output, format="html")]
@classmethod @classmethod
def register(cls): def register(cls):
"""register this directive/class with docutils.""" """register this directive/class with docutils."""
# this allows us to modify apikey and secret attributes before registering. # this allows us to modify apikey and secret attributes before registering.
directives.register_directive('linkpeek', cls) directives.register_directive("linkpeek", cls)