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

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #!/usr/bin/python
  2. #This program is free software: you can redistribute it and/or modify
  3. #it under the terms of the GNU General Public License as published by
  4. #the Free Software Foundation, either version 3 of the License, or
  5. #(at your option) any later version.
  6. #This program is distributed in the hope that it will be useful,
  7. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. #GNU General Public License for more details.
  10. #You should have received a copy of the GNU General Public License
  11. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. #Copyright (c) 2011 Blagovest Petrov b l a g o v e s t ({ AT ]] p e t r o v s D0T info
  13. import json
  14. import urllib2
  15. import sys
  16. import ConfigParser
  17. import xmlrpclib
  18. from optparse import OptionParser
  19. def get_last_pic(user):
  20. jsonurl = "http://api.picplz.com/api/v2/user.json?username=" + str(user) + "&include_pics=1"
  21. response = urllib2.urlopen(jsonurl)
  22. jsobj = json.loads(response.read())
  23. #lastpic = jsobj['value']['users'][0]['last_pic_id']
  24. lastpic = jsobj['value']['users'][0]['pics'][0]['id']
  25. #print lastpic
  26. return lastpic
  27. def write_last_pic(pic):
  28. config = ConfigParser.RawConfigParser()
  29. config.add_section('pics')
  30. config.set('pics', 'lastpic', str(pic))
  31. #config.set('pics', 'int', lastpic)
  32. with open('foo.cfg', 'wb') as configfile:
  33. config.write(configfile)
  34. def get_old_pic():
  35. config = ConfigParser.RawConfigParser()
  36. config.read('foo.cfg')
  37. return config.get('pics', 'lastpic')
  38. #return config.get('pics', 'int')
  39. def get_pic_info(picid):
  40. jsonurl = "http://api.picplz.com/api/v2/pic.json?id=" + str(picid)
  41. #jsonurl = "http://api.picplz.com/api/v2/pic.json?id=4684959"
  42. response = urllib2.urlopen(jsonurl)
  43. jsobj = json.loads(response.read())
  44. url640 = jsobj['value']['pics'][0]['pic_files']['640r']['img_url']
  45. url320 = jsobj['value']['pics'][0]['pic_files']['320rh']['img_url']
  46. caption = jsobj['value']['pics'][0]['caption']
  47. #placename = jsobj['value']['pics'][0]['place']['id'] ##TODO
  48. rtr = { 'url640' : url640, 'url320' : url320, 'caption' : caption }
  49. return rtr
  50. #picinfo = get_pic_info(45)
  51. #print picinfo['url640']
  52. #print picinfo['url320']
  53. #print picinfo['caption']
  54. def wpost(picurl, title, site, user, passwd):
  55. content = '[caption id="" align="aligncenter" width="640" caption="' + title + '"]<img title="' + title + '" src="' + picurl + '" alt="' + title + '" width="640"height="480" />[/caption]'
  56. server = xmlrpclib.ServerProxy(str(site) + 'xmlrpc.php')
  57. blog_content = { 'title' : title, 'description' : content }
  58. post_id = int(server.metaWeblog.newPost(0, str(user), str(passwd), blog_content,0))
  59. server.mt.publishPost(post_id, str(user), str(passwd))
  60. def check_pic(pic):
  61. if str(pic) == get_old_pic():
  62. return False
  63. else:
  64. return True
  65. def main():
  66. parser = OptionParser("usage: %prog -wpuser WP_username -wppass WP_password -plzusr picplz_username")
  67. parser.add_option("--wpuser", "--wordpress-username", dest="wpuser", help="Wordpress username")
  68. parser.add_option("--wppass", "--wordpress-password", dest="wppass", help="Wordpress password")
  69. parser.add_option("--plzusr", "--picplz-username", dest="plzusr", help="PizPlz username")
  70. parser.add_option("--wpsite", "--wordpress-site", dest="wpsite", help="Wordpress site URL ( http://myblog.mydomain/ )")
  71. (options, args) = parser.parse_args()
  72. if options.wpuser is None or options.wppass is None or options.wpsite is None or options.plzusr is None :
  73. parser.error("Incorrect number of options")
  74. sys.exit(1)
  75. wpuser = options.wpuser
  76. wppass = options.wppass
  77. wpsite = options.wpsite
  78. plzusr = options.plzusr
  79. lpic = get_last_pic(str(plzusr))
  80. if(check_pic(lpic) == 1):
  81. write_last_pic(plzusr)
  82. picinfo = get_pic_info(lpic)
  83. wpost(picinfo['url640'], picinfo['caption'], wpsite, wpuser, wppass)
  84. else:
  85. print "No change"
  86. if __name__ == "__main__":
  87. main()