omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    PLS Help: Bitly URL Shortener

    Pythonista
    2
    2
    3649
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Maik239
      Maik239 last edited by

      Hi, I try me on my first snippet of Pythonista, but need some more help.

      I have following code already, but that still does not work. Help me someone?

      <code>import clipboard
      import urllib
      import urllib2

      kurzurl = clipboard.get()

      def shorten_url(long_url):
      username = 'USERNAMW'
      apikey = 'APIKEY'
      bitly_url = "http://api.bit.ly/v3/shorten?login={0}&apiKey={1}&longUrl={2}&format=txt"
      req_url = urlencode(bitly_url.format(username, apikey, long_url))
      short_url = urlopen(req_url).read()
      return short_url

      print shorten_url('kurzurl')</code>

      1 Reply Last reply Reply Quote 0
      • robbielj31
        robbielj31 last edited by

        This should work.
        first of all, long url should be url-quoted.
        then bitly api returns json data so needs json module to load.
        build a urllib2 opener to open formatted <code>bitly_url</code>, that's it.

        <pre>
        import clipboard
        import urllib2
        import json

        kurzurl = urllib2.quote(clipboard.get())

        def shorten_url(long_url):
        username = 'username'
        apikey = 'apikey'
        bitly_url = "http://api.bit.ly/v3/shorten?login=%s&apiKey=%s&longUrl=%s" %(username, apikey, long_url)
        r = urllib2.Request(bitly_url)
        opener = urllib2.build_opener()
        f = json.loads(opener.open(r).read())
        short_url = f['data']['url']
        return short_url

        print shorten_url(kurzurl)
        </pre>

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors