Skip to content

Instantly share code, notes, and snippets.

@zhu327
Created September 16, 2014 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhu327/2a473332a796146b4dbd to your computer and use it in GitHub Desktop.
Save zhu327/2a473332a796146b4dbd to your computer and use it in GitHub Desktop.
通过文件发布文章 https://github.com/zhu327/blog
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2, cookielib, hashlib, urllib, json
EMAIL = ''
PWD = ''
URL = 'http://bozpy.sinaapp.com'
def post(filename):
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
signin = {'email': EMAIL, 'password': hashlib.md5(PWD).hexdigest()}
response = opener.open(URL+'/api/authenticate', urllib.urlencode(signin))
rdict = json.loads(response.read())
if rdict.get('error'):
raise IOError('Signin failed. Msg: %s' % rdict.get('message'))
with open(filename, 'r') as f:
title = f.readline()
tags = f.readline()
content = f.read()
data = dict(title=title, tags=tags, content=content)
response = opener.open(URL+'/api/blogs', urllib.urlencode(data))
rdict = json.loads(response.read())
if not rdict.get('error'):
print 'Post new entry success. ID: %s' % rdict.get('id')
else:
print 'Post failed. Msg: %s' % rdict.get('message')
if __name__ == '__main__':
import sys
if len(sys.argv) == 2:
post(sys.argv[1])
else:
print 'Example: python postEntry.py article.md'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment