Added support for description in photo uploading
This commit is contained in:
parent
51f7daba6c
commit
bbee451f2c
@ -34,7 +34,7 @@ class attachFromLocal(object):
|
||||
image, description = self.dialog.get_image()
|
||||
if image != None:
|
||||
# Define data structure for this attachment, as will be required by VK API later.
|
||||
imageInfo = {"type": "photo", "file": image, "description": os.path.basename(image)}
|
||||
imageInfo = {"type": "photo", "file": image, "description": description}
|
||||
log.debug("Image data to upload: %r" % (imageInfo,))
|
||||
self.attachments.append(imageInfo)
|
||||
# Translators: This is the text displayed in the attachments dialog, when the user adds a photo.
|
||||
@ -79,8 +79,8 @@ class attachFromOnline(object):
|
||||
|
||||
def add_audio(self, *args, **kwargs):
|
||||
""" Allow adding an audio directly from the user's audio library."""
|
||||
list_of_audios = self.session.vk.client.audio.get(count=1000)
|
||||
list_of_audios = list_of_audios["items"]
|
||||
list_of_audios = self.session.vk.client_audio.get()
|
||||
# list_of_audios = list_of_audios["items"]
|
||||
audios = []
|
||||
for i in list_of_audios:
|
||||
audios.append(u"{0}, {1}".format(i["title"], i["artist"]))
|
||||
|
@ -15,10 +15,10 @@ import posts
|
||||
import attach
|
||||
from pubsub import pub
|
||||
from vk_api.exceptions import VkApiError
|
||||
from vk_api import upload
|
||||
from wxUI.tabs import home
|
||||
from sessionmanager import session, renderers, utils
|
||||
from mysc.thread_utils import call_threaded
|
||||
from mysc import upload
|
||||
from wxUI import commonMessages, menus
|
||||
from sessionmanager.utils import add_attachment
|
||||
|
||||
@ -129,7 +129,7 @@ class baseBuffer(object):
|
||||
Currently this function only supports photos."""
|
||||
# To do: Check the caption and description fields for this kind of attachments.
|
||||
local_attachments = ""
|
||||
uploader = upload.VkUpload(self.session.vk.client)
|
||||
uploader = upload.VkUpload(self.session.vk.session_object)
|
||||
for i in attachments:
|
||||
if i["type"] == "photo":
|
||||
photos = i["file"]
|
||||
|
@ -1,144 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import requests
|
||||
|
||||
class VkUpload(object):
|
||||
def __init__(self, vk):
|
||||
"""
|
||||
|
||||
:param vk: объект VkApi
|
||||
"""
|
||||
|
||||
self.vk = vk
|
||||
# https://vk.com/dev/upload_files
|
||||
self.session = requests.session()
|
||||
|
||||
def photo(self, photos, album_id,
|
||||
latitude=None, longitude=None, caption=None, description=None,
|
||||
group_id=None):
|
||||
""" Загрузка изображений в альбом пользователя
|
||||
|
||||
:param photos: список путей к изображениям, либо путь к изображению
|
||||
:param album_id: идентификатор альбома
|
||||
:param latitude: географическая широта, заданная в градусах
|
||||
(от -90 до 90)
|
||||
:param longitude: географическая долгота, заданная в градусах
|
||||
(от -180 до 180)
|
||||
:param caption: текст описания изображения
|
||||
:param description: текст описания альбома
|
||||
:param group_id: идентификатор сообщества (если загрузка идет в группу)
|
||||
"""
|
||||
|
||||
values = {'album_id': album_id}
|
||||
|
||||
if group_id:
|
||||
values['group_id'] = group_id
|
||||
|
||||
# Получаем ссылку для загрузки
|
||||
url = self.vk.photos.getUploadServer(**values)['upload_url']
|
||||
|
||||
# Загружаем
|
||||
photos_files = open_photos(photos)
|
||||
response = self.session.post(url, files=photos_files).json()
|
||||
close_photos(photos_files)
|
||||
|
||||
# Олег Илларионов:
|
||||
# это не могу к сожалению просто пофиксить
|
||||
if 'album_id' not in response:
|
||||
response['album_id'] = response['aid']
|
||||
|
||||
response.update({
|
||||
'latitude': latitude,
|
||||
'longitude': longitude,
|
||||
'caption': caption,
|
||||
'description': description
|
||||
})
|
||||
|
||||
values.update(response)
|
||||
|
||||
# Сохраняем фото в альбоме
|
||||
response = self.vk.photos.save(values)
|
||||
|
||||
return response
|
||||
|
||||
def photo_messages(self, photos):
|
||||
""" Загрузка изображений в сообщения
|
||||
|
||||
:param photos: список путей к изображениям, либо путь к изображению
|
||||
"""
|
||||
|
||||
url = self.vk.method('photos.getMessagesUploadServer')
|
||||
url = url['upload_url']
|
||||
|
||||
photos_files = open_photos(photos)
|
||||
response = self.vk.http.post(url, files=photos_files)
|
||||
close_photos(photos_files)
|
||||
|
||||
response = self.vk.method('photos.saveMessagesPhoto', response.json())
|
||||
|
||||
return response
|
||||
|
||||
def photo_wall(self, photos, user_id=None, group_id=None, caption="No description"):
|
||||
""" Загрузка изображений на стену пользователя или в группу
|
||||
|
||||
:param photos: список путей к изображениям, либо путь к изображению
|
||||
:param user_id: идентификатор пользователя
|
||||
:param group_id: идентификатор сообщества (если загрузка идет в группу)
|
||||
"""
|
||||
|
||||
values = {}
|
||||
|
||||
if user_id:
|
||||
values['user_id'] = user_id
|
||||
elif group_id:
|
||||
values['group_id'] = group_id
|
||||
response = self.vk.photos.getWallUploadServer(**values)
|
||||
url = response['upload_url']
|
||||
photos_files = open_photos(photos)
|
||||
response = self.session.post(url, files=photos_files)
|
||||
close_photos(photos_files)
|
||||
values.update(response.json())
|
||||
response = self.vk.photos.saveWallPhoto(**values)
|
||||
return response
|
||||
|
||||
def document(self, file_path, title=None, tags=None, group_id=None):
|
||||
""" Загрузка документа
|
||||
|
||||
:param file_path: путь к документу
|
||||
:param title: название документа
|
||||
:param tags: метки для поиска
|
||||
:param group_id: идентификатор сообщества (если загрузка идет в группу)
|
||||
"""
|
||||
|
||||
values = {'group_id': group_id}
|
||||
url = self.vk.docs.getUploadServer(values)['upload_url']
|
||||
|
||||
with open(file_path, 'rb') as file:
|
||||
response = self.session.post(url, files={'file': file}).json()
|
||||
|
||||
response.update({
|
||||
'title': title,
|
||||
'tags': tags
|
||||
})
|
||||
|
||||
response = self.vk.method('docs.save', response)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def open_photos(photos_paths):
|
||||
if not isinstance(photos_paths, list):
|
||||
photos_paths = [photos_paths]
|
||||
|
||||
photos = []
|
||||
|
||||
for x, filename in enumerate(photos_paths):
|
||||
filetype = filename.split('.')[-1]
|
||||
photos.append(
|
||||
('file%s' % x, ('pic.' + filetype, open(filename, 'rb')))
|
||||
)
|
||||
return photos
|
||||
|
||||
|
||||
def close_photos(photos):
|
||||
for photo in photos:
|
||||
photo[1][1].close()
|
@ -40,11 +40,11 @@ class attachDialog(widgetUtils.BaseDialog):
|
||||
openFileDialog = wx.FileDialog(self, _(u"Select the picture to be uploaded"), "", "", _("Image files (*.png, *.jpg, *.gif)|*.png; *.jpg; *.gif"), wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
|
||||
if openFileDialog.ShowModal() == wx.ID_CANCEL:
|
||||
return None
|
||||
dsc = ""
|
||||
dsc = self.ask_description()
|
||||
return (openFileDialog.GetPath(), dsc)
|
||||
|
||||
def ask_description(self):
|
||||
dlg = wx.TextEntryDialog(self, _(u"please provide a description"), _(u"Description"), defaultValue="")
|
||||
dlg = wx.TextEntryDialog(self, _(u"please provide a description"), _(u"Description"))
|
||||
dlg.ShowModal()
|
||||
result = dlg.GetValue()
|
||||
dlg.Destroy()
|
||||
|
Loading…
Reference in New Issue
Block a user