167 lines
4.1 KiB
Python
167 lines
4.1 KiB
Python
import requests
|
|
from urllib.parse import urlencode
|
|
|
|
client_id = '4362480'
|
|
client_secret = 'CI8hARpbqa5amp4OfTS6'
|
|
access_token = '39c1712d4005b24bd9faddf34b7f4647a26afb4c7b1a935ba7680b24378a9acb3e0faed5b3af8a1e7879c'
|
|
api_version = '5.52'
|
|
VK_API = 'https://api.vk.com/method/'
|
|
|
|
|
|
def vk_init(app_id, app_secret):
|
|
global client_id
|
|
global client_secret
|
|
client_id = app_id
|
|
client_secret = app_secret
|
|
|
|
|
|
def http_get(url):
|
|
req = requests.get(url)
|
|
return req.json()
|
|
|
|
|
|
def upload_file(url, file):
|
|
req = requests.post(url, files={'photo': file})
|
|
return req.json()
|
|
|
|
|
|
def vk_auth(client_id, redirect_uri, scope, response_type):
|
|
q = urlencode({
|
|
'client_id': client_id,
|
|
'scope': ','.join(scope),
|
|
'v': api_version,
|
|
'redirect_uri': redirect_uri,
|
|
'display': 'page',
|
|
'response_type': response_type
|
|
})
|
|
return 'https://oauth.vk.com/authorize?' + q
|
|
|
|
|
|
def vk_get_access_token(client_id, client_secret, redirect_uri, code):
|
|
q = urlencode({
|
|
'client_id': client_id,
|
|
'client_secret': client_secret,
|
|
'code': code,
|
|
'redirect_uri': redirect_uri,
|
|
})
|
|
uri = 'https://oauth.vk.com/access_token?' + q
|
|
return http_get(uri)
|
|
|
|
|
|
def vk_photos_get_wall_upload_server():
|
|
q = urlencode({
|
|
'access_token': access_token,
|
|
})
|
|
uri = VK_API + 'photos.getWallUploadServer?' + q
|
|
return http_get(uri)['response']
|
|
|
|
|
|
def vk_photos_save_wall_photo(server, hash, photo):
|
|
q = urlencode({
|
|
'access_token': access_token,
|
|
'v': api_version,
|
|
'server': server,
|
|
'hash': hash,
|
|
'photo': photo,
|
|
})
|
|
uri = VK_API + 'photos.saveWallPhoto?' + q
|
|
return http_get(uri)
|
|
|
|
|
|
def vk_wall_get_by_id(ids):
|
|
if not isinstance(ids, list):
|
|
ids = [ids]
|
|
|
|
q = urlencode({
|
|
'posts': ','.join(ids),
|
|
'v': api_version,
|
|
})
|
|
uri = VK_API + 'wall.getById?{q}'.format(q=q)
|
|
return http_get(uri)
|
|
|
|
|
|
def vk_wall_post(owner_id, message, attachments):
|
|
q = urlencode({
|
|
'access_token': access_token,
|
|
'v': api_version,
|
|
'owner_id': owner_id,
|
|
'message': message,
|
|
'attachments': ','.join(attachments)
|
|
})
|
|
uri = VK_API + 'wall.post?{q}'.format(q=q)
|
|
return http_get(uri)
|
|
|
|
|
|
def vk_photos_get_albums(owner_id, album_ids):
|
|
q = urlencode({
|
|
'v': api_version,
|
|
'owner_id': owner_id,
|
|
'album_ids': ','.join(album_ids),
|
|
'photo_sizes': '1',
|
|
'need_covers': '1',
|
|
})
|
|
uri = VK_API + 'photos.getAlbums?{q}'.format(q=q)
|
|
return http_get(uri)
|
|
|
|
|
|
def vk_photos_get(owner_id, album_id):
|
|
q = urlencode({
|
|
'v': api_version,
|
|
'owner_id': owner_id,
|
|
'album_id': album_id,
|
|
'photo_sizes': '1',
|
|
})
|
|
uri = VK_API + 'photos.get?{q}'.format(q=q)
|
|
return http_get(uri)
|
|
|
|
if __name__ == '__main__':
|
|
import os
|
|
|
|
print(os.getcwd())
|
|
|
|
# redirect_uri = 'http://tmshv.ru/callback'
|
|
redirect_uri = 'http://vk.com/blank.html'
|
|
|
|
# print(vk_auth(client_id, redirect_uri, ['photos', 'wall', 'offline', 'messages'], 'token'))
|
|
|
|
# exit()
|
|
|
|
upload_url = vk_photos_get_wall_upload_server()['upload_url']
|
|
print(upload_url)
|
|
|
|
# UPLOADING
|
|
uploaded = upload_file(upload_url, open('1.jpg', 'rb'))
|
|
print(uploaded)
|
|
exit()
|
|
|
|
# uploaded_photo = uploaded['photo']
|
|
saved_photo = vk_photos_save_wall_photo(**uploaded)
|
|
print(saved_photo)
|
|
|
|
# {'response': [
|
|
# {
|
|
# 'text': '',
|
|
# 'id': 420377937,
|
|
# 'album_id': -14,
|
|
# 'photo_75': 'http://cs631817.vk.me/v631817931/37c93/eiG89yDwZl4.jpg',
|
|
# 'photo_130': 'http://cs631817.vk.me/v631817931/37c94/FDDyNTsJYYQ.jpg',
|
|
# 'date': 1467142947,
|
|
# 'width': 200,
|
|
# 'owner_id': 1338931,
|
|
# 'photo_604': 'http://cs631817.vk.me/v631817931/37c95/_TMbT8QHwAc.jpg',
|
|
# 'height': 200}
|
|
# ]
|
|
# }
|
|
|
|
photo_id = saved_photo['response'][0]['id']
|
|
photo_owner_id = saved_photo['response'][0]['owner_id']
|
|
|
|
attachment = '{type}{owner_id}_{media_id}'.format(
|
|
type='photo',
|
|
owner_id=photo_owner_id,
|
|
media_id=photo_id
|
|
)
|
|
|
|
post = vk_wall_post(photo_owner_id, '#api #test', [attachment])
|
|
print(post)
|