How to Shorten URLs from the Command Line with Python « Null Byte :: WonderHowTo

Shortening URLs has become a necessity in this age ofTwitterand limited-character status updates. It not only allows us to cram more words into our oh-so-important Twitter statuses, but it also helps us with a few other things.Some Benefits of URL ShortenersWe can create temporary URLs. The URLs created by a URL shortener are deleted after a certain period of inactivity. This is good for a link that we just want to show to a group of friends quickly.Some URL shortening services allowanalysisof link traffic (GoogleandBitlycome to mind). This can be good for website owners, or people who make money off of webhits. It could help them analyze where their traffic is coming from for a particular link, rather than analyze a website as a whole.Posting longer status updates. People like me who use Twitter to promote their blog, or users who post links to news require every precious letter possible. Links tend to take up quite a few characters, so having URL shortener can be a godsend. It allows us to get our point across without spiraling downward into incoherent e-gibberish.I often want to shorten my URLs from the command line. Opening another browser tab can take up too many resources for my liking. It also can load rather slow, and the JavaScript on the page that loads the shortened URL will often loadimproperlyand not display the new URL. A URL shortener from the command line could also potentially allow us to convert big lists of links, as well as single ones, in a quick and simple manner. ThisNull Bytewill help us do just that! Let's hack together some code in thePython programming languageto shorten our URLs from the command line. This will go great in conjunction with our newly madeTwitter command-line application.RequirementsPythoninstalled on your system.Picking Our APIFirst off, we need to pick which API that we are going to code our Python application to communicate with. AnAPI(ApplicationProgrammingInterface) is basically the protocol for communicating with a web application's interface. This means that there is a specific standard and method for us to connect to the interface with that we must comply with.After a bit of research, the easiest API would be theGoogle URL shortener. I picked it because it does not require registration, an account, or any public API keys. This will keep everything simple so you can easily understand it.Code the ApplicationHere is what I came up with for a finished, cleanly coded and simplistic result. I named iturl.py.Grab the source codehere.To use the program, we simply type the following command into a terminal while in the same directory as the savedurl.pysource code.python url.py "http://paste.url/between/quotes.kthx"Code BreakdownIn the main function,gurl =is simply "curling" the data from the API URL. This is essentially all that we need.Assign all of the appropriate variables needed.The data returned from curling the Goo.gl API is printed out to the screen.If you think coding looks cool, maybe you should try to join theCommunity Byteactivites that we host weekly here at Null Byte.Become one with Null Byte!Twitter,Google+andIRC chat!Want to start making money as a white hat hacker?Jump-start your hacking career with our2020 Premium Ethical Hacking Certification Training Bundlefrom the newNull Byte Shopand get over 60 hours of training from cybersecurity professionals.Buy Now (90% off) >Other worthwhile deals to check out:97% off The Ultimate 2021 White Hat Hacker Certification Bundle99% off The 2021 All-in-One Data Scientist Mega Bundle98% off The 2021 Premium Learn To Code Certification Bundle62% off MindMaster Mind Mapping Software: Perpetual LicenseImage viasolobizcoachRelatedHow To:Use WebTech to Discover What Technologies a Website UsesHow To:Shorten links with bit.lyHow To:An Introduction to IPythonHow To:Bypass Antivirus Software by Obfuscating Your Payloads with GraffitiHow To:Execute Hidden Python Commands in a One-Line StagerHow to Train Your Python:Part 15, Script Structure and ExecutionHow To:Get Started with MicroPython for ESP8266 MicrocontrollersHow To:Hack UnrealIRCd Using Python Socket ProgrammingHacking macOS:How to Use One Python Command to Bypass Antivirus Software in 5 SecondsHack Like a Pro:Python Scripting for the Aspiring Hacker, Part 1How To:Take Control of Sonos IoT Devices with PythonHow To:Get Fastboot Flashable Factory Images for Any OnePlus PhoneHow To:Use Your Command Line to Cast Almost Any Music or Video File TypeHow To:Turbo Ping Sweeping with PythonThe Hacks of Mr. Robot:How to Use the Shodan API with Python to Automate Scans for Vulnerable DevicesHow To:Use the YouTube's built-in URL shortening toolHow To:Find Hidden Web Directories with DirsearchIRC Guide:Making a BotHow To:Use beEF (Browser Exploitation Framework)How To:Transfer Your Spotify Playlists to Google Play MusicHow To:Make a Change-of-IP Notifier in PythonNews:A Basic Website Crawler, in Python, in 12 Lines of Code.How To:Code Your Own Twitter Client in Python Using OAuthHow To:Send SMS Messages with PythonGoodnight Byte:Hack Our IRC Bot to Issue CommandsGoodnight Byte:Coding an IRC Bot in Python (For Beginners)News:Learning Python 3.x as I go (Last Updated 6/72012)How To:Enable Code Syntax Highlighting for Python in the Nano Text EditorCommunity Byte:Coding a Web-Based Password Cracker in PythonHow To:Make a Gmail Notifier in PythonHow To:Generate Word-Lists with Python for Dictionary AttacksGoodnight Byte:Coding a Web-Based Password Cracker in PythonHow To:Push and Pull Remote Files Securely Over SSH with PipesNews:First Steps of Compiling a Program in LinuxGoodnight Byte:HackThisSite Walkthrough, Part 3 - Legal Hacker Training