Monday, December 31, 2018

Python URL Shortener

Python URL Shortener

happy new year 2019

It was late evening on new years eve – or Old Years Night as they call it in the West country – and to pass some time I was rummaging through Python articles on the Internet at random, looking for ideas for a new project.

I didn’t get inspired much, but I thought for a moment that a Bitly URL shortener with  GUI might make a neat little app.

Ass Pain Instansiator

There are Python modules that use Bitly’s API, but when you go to get an API key it says, API keys are deprecated, use OAuth.

python bitly-oauth api deprecated

Somehow they have contrived to try and make it complicated for my pea-sized brain, cobblers to it, I’ll Pyautogui it instead of learning what OAuth is (great attitude Steve, you knob jockey!).

No API And No Sign Up

Here is the quick bit of code that I knocked up that sends a user selected URL to Bitly, and then returns the result of a shortened URL.  No API, and no sign up nonsense required.

By the way, Bitly accepts Mailinator email addresses, so you could use my Dismal app to sign up anonymously in 4 clicks and 4 seconds (steady on there Steve), if you do feel the need to sign up to Bitly.

Say what you want about my crap coding, but I do use my own programs ha-ha.

Double click inside code box to select all.

import webbrowser
import pyautogui
import time
import pyperclip
from tkinter import Tk, simpledialog

ROOT = Tk()
# Stop naff GUI window from showing, for now.
ROOT.withdraw()

bitly_url = "https://bitly.com/"

# Ask user for the URL to shorten via GUI input.
users_url = simpledialog.askstring(title='Bitly', prompt='Enter URL:')

#copy users URL to the system clipboard
pyperclip.copy(users_url)

#open bitly.com in default web browser
webbrowser.open(bitly_url)
time.sleep(2)

#paste users url into bitly
pyautogui.hotkey('ctrl', 'v')  # ctrl-v to paste
time.sleep(2)

# copy bitlys response to clipboard
pyautogui.hotkey('ctrl', 'c')  # ctrl-c to copy

#get the newshortned url from clipboard
short_url = pyperclip.paste()

print (users_url)
print (short_url)

It works well enough, though you may need to adjust the pauses if you have a slow connection or your browser loads slower than mine.

I haven’t added the GUI yet, the above code was to see if it works, it does, but now I am having second thoughts on weather it is actually worth pursuing this project.

It’s All Pretty Pointless Really

I mean, it’s almost just as easy to got to bitly.com yourself and type or paste the URL in there, at the moment this code is pretty pointless and I have learnt nothing I didn’t already know writing it.

Having said all that, I have an idea of  a GUI with a listbox displaying all your URLs with their shortened version so that you can copy them at will to the clipboard for pasting to wherever.

I’m still not convinced it is worth my time or effort to write something I have not much interest in, or will ever be likely to use myself.  I don’t think anyone else will want the GUI either, so there it is, I’ve decided not to continue this project as its worthless and I will not learn anything new from it.

Well, that’s left this article a bit flat then hasn’t it!  I had hoped to do a special long post for the first day of 2019, but it has turned out to be bit of a damp squib.

One last thing, I did find a nice little snippet of code on my travels:

import os
newlist = [f for f in os.listdir('.') if f.endswith('.txt')]
print (newlist)

The above code will search the current directory for any .txt files and print the files names.

Pretty neat code I thought, I have never come across .endswith before, it would definitely of been of use in some of my previous projects.

Of course you can change the directory and the file type to search for easy enough to make this a useful snippet.

This Article .endswith

I started this blog during August 2018.  I deleted it in November, and was going to give up programming for good, as I know I’ll never be good enough to write something worthwhile.  But after a week’s rest I came back and asked WordPress to undelete it, and here we are.

Sometimes I push myself too hard, and that is the result.  I blow up like a drama queen and give up, and then come back stronger for it, yes I know, what a weirdo.

Blog Report Update

On January the 1st 2019 I have 30 subscribers. It is not many, but nearly all of those people are Python people, so look genuine subs.

I am at last starting to get a few hits from Google search here and there, so I’m hoping this will bring in new readers this coming year.

I decided from the start not to worry too much about SEO, apart from the standard post layout, do a few external and internal links, add an image or two and not to worry about keywords and such.

I used to do all that stuff on my old blogs over the years and it made no difference, all it did was cause a lot of extra work and leave me less time to write articles.

It would be nice to have lots of readers to chip in with comments and ideas but I am willing to wait, after all, this blog was originally me just writing notes to myself about my Python learning journey.

Have a great 2019 y’all.

Using Python V3.6.5(32 bit) on Windows 7, (64 bit).

Previous post:  Project Py-Meme

Advertisements


from Python Coder
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...