Panda In A Space Suit

Posts about space, science, romance, new york, and programming

Getting Basic Auth to Work With Jira-python

| Comments

Dark Blue Tiger tirumala septentrionis by kadavoor

At 10gen we use JIRA as our main bug tracker. We use a few different custom python scripts to generate charts that track everything from customer engagement to the number of server tickets linked to support tickets.

Unfortunately, before JIRA’s REST interface was available, the majority of this stuff was done with the suds package and a few hand written calls to urllib (all of this was written well before the time of the brilliant requests package). Now we attack these problems with a more elegant weapon for a more civilized age. So, in walks jira-python a tiny wrapper over JIRA’s REST API written by Ben Speakmon. It’s about 100x better than anything we currently use… that is until we try to use it.

As it turns out http basic authentication doesn’t work at all with the current distribution and apparently my pull request doesn’t meet Atlassian standards. This means I have to write a wrapper over their code until it’s fixed. I had never had to override another class before but it was fairly straight forward and I thought I’d preserve the pattern on this blog so that I could come back to it at some point.

Here’s the code:

apod.nasa.gov for Your Twitter Background

| Comments

So I like space and I thought my twitter background should reflect that.

Wouldn’t it be cool if this:

Could be this:

requirements:

1
2
3
PIL==1.1.7
requests==0.13.3
requests-oauth==0.4.1

So you can probably just run (hopefully in a virtualenv):

1
pip install PIL requests requests-oauth

Next you’ll want to use this script:

apod.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import re
import urllib
import Image
import cStringIO
from oauth_hook import OAuthHook
import requests

# This is the APOD index page
apodbaseurl = 'http://apod.nasa.gov/apod/{}'
# This is how we look for the image on the page
regex = r'a href="(image.*)"'
# You can adjust this but twitter only allows 800k uploads
imgsize = 900, 900
# This our twitter API endpoint for changing the background
twitter_endpoint = 'http://api.twitter.com/1/account/update_profile_background_image.json'

# Create a twitter app: https://dev.twitter.com/apps/new
# After creation and clicking the generate access token button, click through
# to the oauth tab and use the info from there in the variables below.
OAuthHook.consumer_key = 'blarg'
OAuthHook.consumer_secret = 'blarg'
access_token = 'blarg'
access_token_secret = 'blarg'
# Setup the hook to call before we make a request
oauth_hook = OAuthHook(access_token, access_token_secret, header_auth=True)


def get_apod_image():
    # grab the mainpage
    apodpage = urllib.urlopen(apodbaseurl.format('astropix.html')).read()
    # find image url
    apodurl = re.search(regex, apodpage).group(1)
    # open the image file
    imgfile = urllib.urlopen(apodbaseurl.format(apodurl))
    # parse it into memory (cStringIO is faster than StringIO)
    imgstr = cStringIO.StringIO(imgfile.read())
    img = Image.open(imgstr)
    img.convert("RGB")
    # resize preserving aspect ratio
    img.thumbnail(imgsize, Image.ANTIALIAS)
    # save it in the smallest size possible
    img.save("apod.png", "PNG", optimize=True)


def update_twitter():
    client = requests.session(hooks={'pre_request': oauth_hook})
    image = open('apod.png', 'rb')
    response = client.post(twitter_endpoint, '', params={'tile': True},
                           files={'image': ('apod.png', image)})
    # lets print and return some info for troubleshooting
    print response.text
    return response

if __name__ == '__main__':
    get_apod_image()
    update_twitter()

Now make a cronjob with something like:

1
0 23 * * * ~/.virtualenvs/apod/bin/python ~/apod/apod.py

AND BOOM! You’re done.