Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
fixing some examples and shorteners. removing soogd
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisonleao committed Feb 29, 2020
1 parent 622fcac commit 235d0f1
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 97 deletions.
8 changes: 0 additions & 8 deletions docs/apis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ Short.cm
:noindex:


Soo.gd
-----------

.. autoclass:: pyshorteners.shorteners.soogd.Shortener
:members:
:noindex:


Tiny.cc
-----------

Expand Down
8 changes: 0 additions & 8 deletions docs/apis/pyshorteners.shorteners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,6 @@ pyshorteners.shorteners.shortcm module
:undoc-members:
:show-inheritance:

pyshorteners.shorteners.soogd module
------------------------------------

.. automodule:: pyshorteners.shorteners.soogd
:members:
:undoc-members:
:show-inheritance:

pyshorteners.shorteners.tinycc module
-------------------------------------

Expand Down
25 changes: 19 additions & 6 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@


def hello():
s = Shortener()
print(f"""
Hello World! Testing Tinyurl shortener with www.google.com URL
Shorten url: {s.tinyurl.short('http://www.google.com')}
""")
s = Shortener(timeout=5)
print(
f"""
Hello World! Testing google.com for some shorteners
Results
=======
- TinyURL - {s.tinyurl.short('http://www.google.com')}
- Chilp.it - {s.chilpit.short('http://www.google.com')}
- Clck.ru - {s.clckru.short('http://www.google.com')}
- Da.gd - {s.dagd.short('http://www.google.com')}
- Git.io - {s.gitio.short('https://www.github.com/ellisonleao/sharer.js')}
- Is.gd - {s.isgd.short('http://www.google.com')}
- NullPointer - {s.nullpointer.short('http://www.google.com')}
- Osdb.link - {s.osdb.short('http://www.google.com')}
- Qps.ru - {s.qpsru.short('www.google.com')}
"""
)

if __name__ == '__main__':

if __name__ == "__main__":
hello()
2 changes: 1 addition & 1 deletion pyshorteners/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def _post(self, url, data=None, json=None, params=None, headers=None):
)
return response

def short(self, url ):
def short(self, url):
"""Shorten URL using a shortening service.
Args:
Expand Down
28 changes: 19 additions & 9 deletions pyshorteners/shorteners/gitio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@ class Shortener(BaseShortener):
Example:
>>> import pyshorteners
>>> s = pyshorteners.Shortener()
>>> s = pyshorteners.Shortener(code='12345')
>>> s.gitio.short('https://github.com/TEST')
'https://git.io/abc123'
>>> s.gitio.expand('https://git.io/abc123')
'https://git.io/12345'
>>> s.gitio.expand('https://git.io/12345')
'https://github.com/TEST'
"""

api_url = "https://git.io"

def short(self, url):
"""Short implementation for TinyURL.com
"""Short implementation for Git.io
Only works for github urls
Args:
url: the URL you want to shorten
url (str): the URL you want to shorten
code (str): (Optional) Custom permalink code: Eg.: test
Returns:
A string containing the shortened URL
str: The shortened URL
Raises:
ShorteningErrorException: If the API returns an error as response
"""
code = None
try:
code = self.code
except AttributeError:
pass

shorten_url = self.api_url
data = {"url": url}
data = {"url": url, "code": code}
response = self._post(shorten_url, data=data)
if not response.ok:
raise ShorteningErrorException(response.content)

if not response.headers["Location"]:
raise ShorteningErrorException()
if not response.headers.get("Location"):
raise ShorteningErrorException(response.content)

return response.headers["Location"]
34 changes: 29 additions & 5 deletions pyshorteners/shorteners/osdb.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import re
from html.parser import HTMLParser

from ..base import BaseShortener
from ..exceptions import ShorteningErrorException


class OHTMLParser(HTMLParser):
def __init__(self):
self.found = False
self.val = None
return HTMLParser.__init__(self)

def handle_starttag(self, tag, attrs):
attrs = dict(attrs)

if "id" in attrs and attrs["id"] == "surl":
self.found = True

def handle_data(self, data):
if not self.found:
return

if data.startswith("http://osdb"):
self.val = data


class Shortener(BaseShortener):
"""
Os.db shortener implementation
Expand All @@ -18,8 +38,7 @@ class Shortener(BaseShortener):
'https://www.google.com'
"""

api_url = "http://osdb.link/"
p = re.compile(r"(http:\/\/osdb.link\/[a-zA-Z0-9]+)")
api_url = "https://osdb.link/"

def short(self, url):
"""Short implementation for Os.db
Expand All @@ -37,5 +56,10 @@ def short(self, url):
response = self._post(self.api_url, data={"url": url})
if not response.ok:
raise ShorteningErrorException(response.content)
match = self.p.search(response.text)
return match.group()
p = OHTMLParser()
p.feed(response.text)

if not p.val:
raise ShorteningErrorException("Could not find osdb.link")

return p.val
60 changes: 0 additions & 60 deletions pyshorteners/shorteners/soogd.py

This file was deleted.

0 comments on commit 235d0f1

Please sign in to comment.