You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

110 lines
3.9 KiB

#!/usr/bin/python
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#Copyright (c) 2011 Blagovest Petrov b l a g o v e s t ({ AT ]] p e t r o v s D0T info
import json
import urllib2
import sys
import ConfigParser
import xmlrpclib
from optparse import OptionParser
def get_last_pic(user):
jsonurl = "http://api.picplz.com/api/v2/user.json?username=" + str(user) + "&include_pics=1"
response = urllib2.urlopen(jsonurl)
jsobj = json.loads(response.read())
#lastpic = jsobj['value']['users'][0]['last_pic_id']
lastpic = jsobj['value']['users'][0]['pics'][0]['id']
#print lastpic
return lastpic
def write_last_pic(pic):
config = ConfigParser.RawConfigParser()
config.add_section('pics')
config.set('pics', 'lastpic', str(pic))
#config.set('pics', 'int', lastpic)
with open('foo.cfg', 'wb') as configfile:
config.write(configfile)
def get_old_pic():
config = ConfigParser.RawConfigParser()
config.read('foo.cfg')
return config.get('pics', 'lastpic')
#return config.get('pics', 'int')
def get_pic_info(picid):
jsonurl = "http://api.picplz.com/api/v2/pic.json?id=" + str(picid)
#jsonurl = "http://api.picplz.com/api/v2/pic.json?id=4684959"
response = urllib2.urlopen(jsonurl)
jsobj = json.loads(response.read())
url640 = jsobj['value']['pics'][0]['pic_files']['640r']['img_url']
url320 = jsobj['value']['pics'][0]['pic_files']['320rh']['img_url']
caption = jsobj['value']['pics'][0]['caption']
#placename = jsobj['value']['pics'][0]['place']['id'] ##TODO
rtr = { 'url640' : url640, 'url320' : url320, 'caption' : caption }
return rtr
#picinfo = get_pic_info(45)
#print picinfo['url640']
#print picinfo['url320']
#print picinfo['caption']
def wpost(picurl, title, site, user, passwd):
content = '[caption id="" align="aligncenter" width="640" caption="' + title + '"]<img title="' + title + '" src="' + picurl + '" alt="' + title + '" width="640"height="480" />[/caption]'
server = xmlrpclib.ServerProxy(str(site) + 'xmlrpc.php')
blog_content = { 'title' : title, 'description' : content }
post_id = int(server.metaWeblog.newPost(0, str(user), str(passwd), blog_content,0))
server.mt.publishPost(post_id, str(user), str(passwd))
def check_pic(pic):
if str(pic) == get_old_pic():
return False
else:
return True
def main():
parser = OptionParser("usage: %prog -wpuser WP_username -wppass WP_password -plzusr picplz_username")
parser.add_option("--wpuser", "--wordpress-username", dest="wpuser", help="Wordpress username")
parser.add_option("--wppass", "--wordpress-password", dest="wppass", help="Wordpress password")
parser.add_option("--plzusr", "--picplz-username", dest="plzusr", help="PizPlz username")
parser.add_option("--wpsite", "--wordpress-site", dest="wpsite", help="Wordpress site URL ( http://myblog.mydomain/ )")
(options, args) = parser.parse_args()
if options.wpuser is None or options.wppass is None or options.wpsite is None or options.plzusr is None :
parser.error("Incorrect number of options")
sys.exit(1)
wpuser = options.wpuser
wppass = options.wppass
wpsite = options.wpsite
plzusr = options.plzusr
lpic = get_last_pic(str(plzusr))
if(check_pic(lpic) == 1):
write_last_pic(plzusr)
picinfo = get_pic_info(lpic)
wpost(picinfo['url640'], picinfo['caption'], wpsite, wpuser, wppass)
else:
print "No change"
if __name__ == "__main__":
main()