* * * * * /bin/bash -l -c 'cd /projects/basecamp && /usr/bin/python task.py >> /projects/basecamp/cron.log 2>&1'
Created
February 1, 2012 04:05
-
-
Save gabrielfalcao/1715024 to your computer and use it in GitHub Desktop.
Creates to do lists on Basecamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.pyc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from httplib import HTTPSConnection | |
from base64 import encodestring | |
# notice the 4 soft-spaces indentation two lines separating class or | |
# function definitions, although instance methods uses 1 space | |
class Basecamp: | |
def __init__(self, username, password, basecamp_url, project_id): | |
self.auth = encodestring('%s:%s' % (username, password))[:-1] | |
self.basecamp_url = basecamp_url | |
self.connection = HTTPSConnection(basecamp_url) | |
self.project_id = project_id | |
def post(self, data, url): | |
self.connection.putrequest("POST", url) | |
self.connection.putheader("Authorization", "Basic %s" % self.auth) | |
# you could use single-quotes when wrapping around double-quotes: | |
self.connection.putheader("Content-type", 'text/xml; charset="UTF-8"') | |
self.connection.putheader("Content-length", "%d" % len(data)) | |
self.connection.endheaders() | |
self.connection.send(data) | |
response = self.connection.getresponse() | |
self.connection.close() | |
return response | |
def create_list(self, name, description): | |
blist = List( | |
name=name, | |
description=description, | |
basecamp_instance=self, | |
) | |
return blist.create() | |
class List: | |
template = """ | |
<todo-list> | |
<name>%s</name> | |
<description>%s</description> | |
</todo-list>""".strip() | |
# no spaces between parameters and its default args | |
def __init__(self, name, description=None, basecamp_instance=None): | |
self.name = name | |
self.description = description | |
self.basecamp = basecamp_instance | |
def create(self): | |
data = self.template % (self.name, self.description) | |
url = "/projects/%s/todo_lists.xml" % self.basecamp.project_id | |
response = self.basecamp.post(data, url) | |
self.id = response.getheader("Location").split('/')[-1] | |
return response | |
def create_item(self, name, content=""): | |
item = ListItem(parent=self, name=name, content=content) | |
return item.create() | |
class ListItem(object): | |
# "list" is a python keyword, avoid naming variables after that | |
def __init__(self, parent, content, basecamp_instance): | |
self.list = parent | |
self.content = content | |
def create(self): | |
data = "<todo-item><content>%s</content></todo-item>" % self.content | |
url = "/todo_lists/%s/todo_items.xml" % self.list.id | |
response = self.parent.basecamp.post(data, url) | |
return response | |
# making sure the connection runs only once | |
if __name__ == "__main__": | |
basecamp = Basecamp(username="username", | |
password="password", | |
basecamp_url="basecamp_url", | |
project="123456") | |
# again, let's avoid using the "list" keyword | |
list1 = basecamp.create_list( | |
name="Daily", | |
description="It's my description", | |
) | |
for name in ["Testing", "Works?"]: | |
list1.create_item(name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment