среда, 20 июня 2012 г.

Vk.com python photo uploader


#!/usr/bin/python
# -*- coding: utf-8 -*-

import vkontakte
#XXXXX is your vk application id
#http://oauth.vk.com/authorize?client_id=XXXXXXX&scope=photos&response_type=token
#then you get 'token' from address line
access_token='soo_long_token_string_that_you_get'
vk = vkontakte.API('vk_application_id_again', 'vk_application_secrete_code')
vk = vkontakte.API(token=access_token)
upload_url_=vk.photos.getUploadServer(aid=vk.photos.getAlbums()[0]['aid'])['upload_url']
import requests
img_from=234 #you own number - it is the part of file name for me
blocks=500-img_from#vk albums cant contain more then 500 photos
block_size=1#it can not be bigger than 5! see vk developer docs
img = img_from
import json
print 'well connected, start upload by blocks of '+str(block_size)+' files'

while img<img_from+blocks*block_size:
   files = {}
   for f in range(1,block_size+1):
      files['file'+str(f)]= open('/home/argon/py/lxml/artkritka/'+str(img+f-1)+'.jpg', 'rb')
   img+=block_size
   #print files.__str__()+"\n\n"
   r=requests.post(upload_url_,files=files)
   s=json.loads(r.text)
   vk.photos.save(aid=s['aid'],server=s['server'],photos_list=s['photos_list'],hash=s['hash'])
   print 'last uploaded file:'+str(img-1)+'.jpg'

Title

In the name of my lovely girlfriend:)