question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

URL shortening parameter is not used in api.py, therefore service is not available

See original GitHub issue

Hi bear, You’ve did a nice job on this repo! 😃 Please correct me if I’m wrong but I can’t use the custom URL shortening service as-is because there is no reference of it in your api.py apart from a parameter and a documentation comment: https://github.com/bear/python-twitter/blob/master/twitter/api.py#L140 https://github.com/bear/python-twitter/blob/master/twitter/api.py#L169 I have implemented after some work a bitly oauth client which can shorten urls, then created a url shortener class according to your shorten_url.py but when I tried to use it, there was no effect of it. When I examined the api.py, I’ve recognized the shortner variable is not used, which is a really great downside of the whole project. Can you or somebody else please provide a fix for this issue? 😃

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremylowcommented, Feb 17, 2016

If you have a working implementation of a shortener service, you can create a shortener instance, pass the URL to that, and replace the long URL with the short URL in the status string, and then post the modified string to Api.PostUpdate():

import re

from twitter import Api
from twitter.twitter_utils import URL_REGEXP
from shorten_url import ShortenURL

CONSUMER_KEY = 'WWWWWWW'
CONSUMER_SECRET = 'XXXXXXXXXXX'
ACCESS_TOKEN = 'YYYYYYYYYYY'
ACCESS_TOKEN_SECRET = 'ZZZZZZZZZ'


def PostStatusWithShortenedURL(status):
    shortener = ShortenURL()
    api = Api(CONSUMER_KEY,
              CONSUMER_SECRET,
              ACCESS_TOKEN,
              ACCESS_TOKEN_SECRET)

    urls = re.findall(URL_REGEXP, status)

    for url in urls:
        status = status.replace(url, shortener.Shorten(url), 1)

    api.PostUpdate(status)


if __name__ == '__main__':
    PostStatusWithShortenedURL("this is a test: http://www.example.com/tests")




0reactions
jeremylowcommented, Feb 18, 2016

Just created a PR for the documentation and examples, so I think the issue can be closed. Code from above is now in the shorten_url.py example and has inline comments so folks can see how it works a little easier.

Read more comments on GitHub >

github_iconTop Results From Across the Web

URL Shortener using Python | Towards Data Science
Learn how to use various APIs in Python to shorten URLs easily with only a few lines of code.
Read more >
URL Shortener using Python + Flask (explained beginner ...
In this video we are going to be building a simple URL shortener using Python and Flask. The process is fully explained and...
Read more >
URL Shortener Microservice - freeCodeCamp APIs ... - YouTube
This is a full walkthrough for the URL Shortener Microservice project on freeCodeCamp. After setting up a database connection we create a ...
Read more >
URL shortener | Practice Problems - HackerEarth
You have a URL-shortening service that registered users can use to create custom short links for any website. The service can also be...
Read more >
Python Tutorial – How to Create a URL Shortener using Flask
But we'll not create index.html directly. We can use the Template Inheritance concept in Jinja2. So, let's create a templates directory within ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found