Python how to use bitly links shortener


SUBMITTED BY: alemotta

DATE: March 15, 2017, 6:22 p.m.

FORMAT: Text only

SIZE: 3.6 kB

HITS: 153

  1. Python how to use bitly links shortener
  2. Before we start
  3. Bitly allows users to shorten, share, and track links (URLs).
  4. It's a way to save, share and discover links on the web.
  5. Bitly provides public API's which is intended to make it even easier for
  6. python programmers to use.
  7. First Step
  8. The first step is to head over to dev.bitly.com where you will find the API
  9. documentation, best practices, code libraries, public data sets.
  10. What is an API Key?
  11. Many services on the Internet (such as Twitter, Facebook..) requires that you
  12. have an "API Key".
  13. An application programming interface key (API key) is a code passed in by
  14. computer programs calling an API to identify the calling program, its developer,
  15. or its user to the Web site.
  16. API keys are used to track and control how the API is being used, for example
  17. to prevent malicious use or abuse of the API.
  18. The API key often acts as both a unique identifier and a secret token for
  19. authentication, and will generally have a set of access rights on the API
  20. associated with it.
  21. Get the Bitly API Key
  22. To be able for us to shorten links (see below) we've to sign up for an API key.
  23. The signup procedure is pretty much explaining itself, so I'll not cover
  24. that in this post.
  25. Create your Bitly API Key here
  26. Bitly Code Libraries
  27. A number of developers have written code libraries to interact with the bitly
  28. API in several different languages. Since we're programming in Python,
  29. we're of course interested in the Python libraries.
  30. There are currently three different libraries to choose, which you can find here
  31. In this post we will use the "bitly-api-python" library, which is also the
  32. official Python client.
  33. Bitly API Python
  34. The installation of Bitly API is very easy.
  35. # Installation using PIP
  36. pip install bitly_api
  37. Downloading/unpacking bitly-api
  38. Downloading bitly_api-0.2.tar.gz
  39. Running setup.py egg_info for package bitly-api
  40. Installing collected packages: bitly-api
  41. Running setup.py install for bitly-api
  42. Successfully installed bitly-api
  43. Cleaning up...
  44. Shorten a URL
  45. We want to write a script that will reduce the URL length to make the sharing
  46. easier. Open up your favourite text editor and put in the code below.
  47. Save the file as shortener.py
  48. #!/usr/bin/env python
  49. # Import the modules
  50. import bitlyapi
  51. import sys
  52. # Define your API information
  53. API_USER = "your_api_username"
  54. API_KEY = "your_api_key"
  55. b = bitlyapi.BitLy(API_USER, API_KEY)
  56. # Define how to use the program
  57. usage = """Usage: python shortener.py [url]
  58. e.g python shortener.py http://www.google.com"""
  59. if len(sys.argv) != 2:
  60. print usage
  61. sys.exit(0)
  62. longurl = sys.argv[1]
  63. response = b.shorten(longUrl=longurl)
  64. print response['url']
  65. Shortener.py Explained
  66. We started the program with #!/usr/bin/env python
  67. #!/usr/bin/env python
  68. Importing the modules that we're going to use in our program
  69. import bitlyapi
  70. import sys
  71. Defining our API information
  72. API_USER = "your_api_username"
  73. API_KEY = "your_api_key"
  74. b = bitlyapi.BitLy(API_USER, API_KEY)
  75. Defining how to use the the program
  76. usage = """Usage: python shortener.py [url]
  77. e.g python shortener.py http://www.google.com"""
  78. if len(sys.argv) != 2:
  79. print usage
  80. sys.exit(0)
  81. Creates a variable longurl and sets the value to the argument passed in
  82. longurl = sys.argv[1]
  83. Gives the Bitly API the longurl
  84. response = b.shorten(longUrl=longurl)
  85. Prints out the the URL value
  86. print response['url']

comments powered by Disqus