Программирование
- 1 ответ
- 0 вопросов
1
Вклад в тег
import requests
import vk_api
BASE_DIR = os.path.dirname(os.path.abspath('__file__'))
class VKPage(object):
def __init__(self, photo_album_id, post_owner_id):
self.vk_session = vk_api.VkApi(LOGIN, PASSWORD, app_id='*****',
config_filename='config.json')
try:
self.vk_session.authorization()
except vk_api.AuthorizationError as error_msg:
print(error_msg)
return
self.vk = self.vk_session.get_api()
self.album_id = photo_album_id
self.content_owner_id = self.groupIds()
self.post_owner_id = post_owner_id
def upload_photo(self, path_to_photo):
upload = vk_api.VkUpload(self.vk_session)
photo = upload.photo( # Подставьте свои данные
path_to_photo,
album_id=self.album_id
)
vk_photo = 'photo{}_{}'.format(
photo[0]['owner_id'], photo[0]['id']
)
return vk_photo
def save_photo(self, url):
r = requests.get(url)
path_to_picture = BASE_DIR + '/picture.jpg'
with open(path_to_picture, 'wb') as f:
f.write(r.content)
return path_to_picture
def main(self):
path_to_picture = self.save_photo(photo_res)
self.vk.wall.post(owner_id=self.post_owner_id, message=text,
attachments=self.upload_photo(path_to_picture))