Frazer McLean

I couldn't find a nice Python client for accessing a YOURLS server, so I created one.

>>> from yourls import YOURLSClient

>>> yourls = YOURLSClient('http://example.com/yourls-api.php', signature='6f344c2a8p')
>>> shorturl = yourls.shorten('http://google.com')
>>> shorturl
ShortenedURL(
    shorturl='http://example.com/abcde',
    url='http://google.com',
    title='Google',
    date=datetime.datetime(2015, 10, 31, 14, 31, 4),
    ip='203.0.113.0',
    clicks=0,
    keyword='abcde')

>>> yourls.expand('abcde')
'http://google.com'
>>> yourls.expand('http://example.com/abcde')
'http://google.com'

>>> yourls.url_stats('abcde')
ShortenedURL(
    shorturl='http://example.com/abcde',
    url='http://google.com',
    title='Google',
    date=datetime.datetime(2015, 10, 31, 14, 31, 4),
    ip='203.0.113.0',
    clicks=0,
    keyword='abcde')

>>> links, stats = yourls.stats(filter='random', limit=2)
>>> links
[ShortenedURL(
    shorturl='http://example.com/abcde',
    url='http://google.com',
    title='Google',
    date=datetime.datetime(2015, 10, 31, 14, 31, 4),
    ip='203.0.113.0',
    clicks=2,
    keyword='abcde'),
ShortenedURL(
    shorturl='http://example.com/gd65t',
    url='http://www.youtube.com',
    title='YouTube',
    date=datetime.datetime(2015, 10, 31, 11, 34, 5),
    ip='203.0.113.0',
    clicks=567,
    keyword='gd65t')]
>>> stats
DBStats(total_clicks=1234, total_links=5678)

>>> yourls.db_stats()
DBStats(total_clicks=1234, total_links=5678)