Category Archives: Python

Imgur API part 1: getting an Anonymous Key and uploading an image

This first part will focus on creating an anonymous API (Application programming interface) key for your script to use, and then upload an image to imgur.com.  For this, and any following tutorals, I will be using Windows 7×64, with Python 2.7.

You will need to create an anonymous API key at http://imgur.com/register/api_anon, it’s really simple, just feed the name of your app, so the dudes over at Imgur know what your intentions are, and then your personal information and finally, the reCaptcha, to verify you’re not a bot. The next page will give you your developer API key; this is important. This key will allow you to interact with the Imgur API. Since this is the limited Anonymous API, you will only have access to basic functions, like uploading images from your computer or from another website, and getting gallery and image information. Luckily, that’s enough for our purposes here.

The documentation for the Imgur API that we’ll be using today is found here: http://api.imgur.com/resources_anon, and the requests module documentation is here http://docs.python-requests.org/en/latest/user/quickstart/. Now, open up your favourite text editor for coding. I like using Wing IDE Professional,  but I’m sure notepad++ and any other one would work just as nicely.  This is where the fun part begins. Hit the jump!

Tinypaste of the full working code!

Continue reading Imgur API part 1: getting an Anonymous Key and uploading an image

Python API Basics

I have set of imports that I like to have at the top of every API client I write, some are built in, but some are not. I’ll just quickly go over them before you get started on the fun stuff. This post is just to list off the modules you might need. If you’re on 64 bit OSes, you might need to take a look here for compatible packages, I know I did. For then entirety of the tutorials, I’ll be using Windows 7×64, with Python 2.7, so maybe you’ll have a different experience with Python 3, and on a different OS.*

First off, requests module is essential to everything we’ll be doing within the following tutorials. It handles all the hard-to-understand stuff, like POST, PUT, and GET requests, as well as cookies, so we don’t have to worry about it much at all; and ideal Python module.

Secondly, pprint is a nice thing to have, since we’ll be dealing a lot with dictionaries, and the regular print statement doesn’t print it off in the most readable of formats. Instead of printing everything in a single line, it linewraps, as well as uses separate lines where doing so would help with legibility. For example: a dictionary would be printed like this normally:

dictionary = {'a': 1, 'b': 2, 'c': 3}

but with pprint it’d look something more like this:

dictionary = {'a' : 1,
              'b' : 2,
              'c' : 3' }

which is amazing when you have huge dictionaries that you need to visually parse.

Thirdly, the json module is another essential one, as the APIs will often feed us JSON data. JSONs are essentially dictionaries writtien in Javascript. We’ll take the request data from the requests module, and feed it into the json.loads function and it’ll return a native python dict that we can manipulate like it’s not even a big deal. It’s pretty great.

Here’s an example of all three modules working together:

#imports
from pprint import pprint
import requests
import json

r = requests.get(r'http://www.reddit.com/user/tankorsmash/about/.json')
#print r.text  #raw text response as a string

j = json.loads(r.text)  #turn the json response into a python dict
#print j  #now it's a python dict

pprint(j)  #here's the final respone, printed out nice an readable format

There you have it,  the basic python modules I’ll be using throughout the next few tutorials.

*thanks to EuphoriaForAll for the Python Version inclusion here

Wrote a Tile system!

Welcome back! I’m very happy to be working on the game again, after the brief hiatus. I made a grid system, as shown below. Here’s the latest thing little app I made, the colors and text is all just there for making the tiles visible, otherwise it’d be a blank screen :

Hit the jump for code examples, and what my old broken version of the code was.

Continue reading Wrote a Tile system!

Windows7x64 Wallpaper Changer Script

I’ve been meaning to make this ever since I discovered win32 stuff. It uses the .2 version of SendKey, found here, that uses ctypes, instead of the win32 stuff I think, as well as working with 64 bit Windows. Make a shortcut to it on your desktop and just run it as a .pyw file, in order to hide the terminal window that pops up. Simple stuff

Anyways, all it does is is simulate a right-click and then an ‘n’ keystroke. It’s just a quick little app,  just a dozen lines long:

import win32api, win32con
import SendKeys
def click(x=-1,y=-1):
    oldX, oldY = win32api.GetCursorPos()
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN,x,y,0,0)
    win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP,x,y,0,0)
    win32api.SetCursorPos((oldX, oldY))

click()
SendKeys.SendKeys('n')

Popup Box

Today, I’ve created a popup dialog that will be used to display information to the player. It’s nothing to look at, but I feel like it’s important to get right, since the player will be spend a lot of their non-active gameplay time reading them, and it the popups don’t look right, then the game won’t feel as well put together as it otherwise might. I figure I’ll add an option to slowly scale it smaller and smaller, and also fadeout, just so I can broaden its use throughout my code. Once I get the scaling set, I can also start to implement damage numbers, because I once swore to myself that my game would have numbers everywhere.

In fact, this is a good time to mention that my game will have a stupid amount of stat tracking. Like everything possible will be tracked, and hopefully exported to xml or something so you can browse it yourself. I’m talking like amount of times you pressed a given key, or hit or missed something, or the average amount of experience you’ve earned in the first 5 minutes of the game. Little things like that, things that no one really thinks about, but I’m sure there’ll be one fan that is all about stats, and he’ll love it. I don’t know if it’ll drain the memory though, Python’s pretty slow already.

Hit the jump for an explanation of how I wrote my word wrapping , autosizing popup box.

Continue reading Popup Box