Created
August 14, 2013 18:31
-
-
Save dryan/6234033 to your computer and use it in GitHub Desktop.
CLI to put your latest CloudApp image into Campfire chat
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 | |
import sys, os, ConfigParser, argparse | |
from getpass import getpass | |
CLOUD_CONFIG_PATH = os.path.expanduser('~/.cloudapp') | |
CAMPFIRE_CONFIG_PATH = os.path.expanduser('~/.basecamp') | |
def log(message, error_code = None): | |
if error_code: | |
sys.stderr.write('%s\n' % message) | |
sys.stderr.flush() | |
sys.exit(error_code) | |
else: | |
sys.stdout.write('%s\n' % message) | |
sys.stdout.flush() | |
cloud_config = ConfigParser.ConfigParser() | |
cloud_config.read(CLOUD_CONFIG_PATH) | |
if not cloud_config.has_section('Credentials'): | |
cloud_config.add_section('Credentials') | |
cloud_config.set('Credentials', 'username', None) | |
cloud_config.set('Credentials', 'password', None) | |
username = cloud_config.get('Credentials', 'username', None) | |
password = cloud_config.get('Credentials', 'password', None) | |
if not username: | |
username = raw_input('Your CloudApp Username: ') | |
cloud_config.set('Credentials', 'username', username) | |
cloud_config.write(open(CLOUD_CONFIG_PATH, 'w')) | |
if not password: | |
password = getpass('Your CloudApp Password: ') | |
cloud_config.set('Credentials', 'password', password) | |
cloud_config.write(open(CLOUD_CONFIG_PATH, 'w')) | |
try: | |
import requests | |
from requests.auth import HTTPDigestAuth | |
except ImportError: | |
log('Please install the requests module. `pip install requests`', os.EX_SOFTWARE) | |
response = requests.get('http://my.cl.ly/items?type=image&page=1&per_page=1', auth = HTTPDigestAuth(username, password), headers = {'Accept': 'application/json'}) | |
response = response.json() | |
image = response.pop(0) | |
image_url = image.get('content_url') | |
try: | |
from camplight import Request, Campfire | |
except ImportError: | |
log('Please install the camplight module. `pip install camplight`', os.EX_SOFTWARE) | |
campfire_config = ConfigParser.ConfigParser() | |
campfire_config.read(CAMPFIRE_CONFIG_PATH) | |
if not campfire_config.has_section('Basecamp'): | |
campfire_config.add_section('Basecamp') | |
campfire_config.set('Basecamp', 'subdomain', None) | |
campfire_config.set('Basecamp', 'api_token', None) | |
campfire_config.set('Basecamp', 'room') | |
subdomain = campfire_config.get('Basecamp', 'subdomain', None) | |
api_token = campfire_config.get('Basecamp', 'api_token', None) | |
room = campfire_config.get('Basecamp', 'room', None) | |
if not subdomain: | |
subdomain = raw_input('Campfire subdomain: ') | |
campfire_config.set('Basecamp', 'subdomain', subdomain) | |
campfire_config.write(open(CAMPFIRE_CONFIG_PATH, 'w')) | |
if not api_token: | |
api_token = raw_input('Campfire API Token (find it at https://%s.campfirenow.com/member/edit): ' % subdomain) | |
campfire_config.set('Basecamp', 'api_token', api_token) | |
campfire_config.write(open(CAMPFIRE_CONFIG_PATH, 'w')) | |
if not room: | |
room = raw_input('Campfire room to post in (by name, not ID): ') | |
campfire_config.set('Basecamp', 'room', room) | |
campfire_config.write(open(CAMPFIRE_CONFIG_PATH, 'w')) | |
request = Request('https://%s.campfirenow.com' % subdomain, api_token) | |
campfire = Campfire(request) | |
campfire_room = campfire.room(room) | |
campfire_room.speak(image_url) |
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
requests==1.2.3 | |
camplight==0.9.6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment