Browse Source

command line options

master
Blagovest Petrov 14 years ago
parent
commit
3388cd88b3
  1. 72
      picplz2wp.py

72
picplz2wp.py

@ -1,19 +1,29 @@
#!/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
#import getopt
from optparse import OptionParser
wpuser = "YOUR WORDPRESS USERNAME"
wppass = "YOUR WORDPRESS PASSWORD"
wpsite = "http://myblog.inc/blog/"
plzusr = "YOUR PICPLZ USERNAME"
def get_last_pic(user):
jsonurl = "http://api.picplz.com/api/v2/user.json?username=" + user + "&include_pics=1"
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']
@ -21,15 +31,11 @@ def get_last_pic(user):
#print lastpic
return lastpic
lpic = get_last_pic(plzusr)
def write_last_pic():
lastpic = get_last_pic(plzusr)
def write_last_pic(pic):
config = ConfigParser.RawConfigParser()
config.add_section('pics')
config.set('pics', 'lastpic', lastpic)
config.set('pics', 'lastpic', str(pic))
#config.set('pics', 'int', lastpic)
with open('foo.cfg', 'wb') as configfile:
config.write(configfile)
@ -48,7 +54,7 @@ def get_pic_info(picid):
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']
#placename = jsobj['value']['pics'][0]['place']['id'] ##TODO
rtr = { 'url640' : url640, 'url320' : url320, 'caption' : caption }
return rtr
@ -59,32 +65,46 @@ def get_pic_info(picid):
#print picinfo['url320']
#print picinfo['caption']
def wpost(picurl, title):
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(wpsite + 'xmlrpc.php')
server = xmlrpclib.ServerProxy(str(site) + 'xmlrpc.php')
blog_content = { 'title' : title, 'description' : content }
post_id = int(server.metaWeblog.newPost(0, wpuser, wppass, blog_content,0))
server.mt.publishPost(post_id, wpuser, wppass)
#picinfo = get_pic_info('4684959')
#wpost(picinfo['url640'], picinfo['caption'])
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():
if str(lpic) == get_old_pic():
def check_pic(pic):
if str(pic) == get_old_pic():
return False
else:
return True
def main():
if(check_pic() == 1):
write_last_pic()
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'])
wpost(picinfo['url640'], picinfo['caption'], wpsite, wpuser, wppass)
else:
print "No change"
write_last_pic()
if __name__ == "__main__":
main()

Loading…
Cancel
Save