Update API version

This commit is contained in:
2017-03-13 02:16:34 -06:00
parent 2c6704ae9e
commit ead1f186f0
17 changed files with 199 additions and 52 deletions

View File

@@ -1,14 +1,17 @@
# -*- coding: utf-8 -*-
""" Attachment upload methods for different kind of posts in VK."""
import os
import widgetUtils
import logging
from wxUI.dialogs import attach as gui
from wxUI.dialogs import selector
log = logging.getLogger("controller.attach")
class attach(object):
def __init__(self):
class attachmentUploader(object):
def __init__(self, voice_messages=False):
self.attachments = list()
self.dialog = gui.attachDialog()
self.type = "local"
self.dialog = gui.attachDialog(voice_messages)
widgetUtils.connect_event(self.dialog.photo, widgetUtils.BUTTON_PRESSED, self.upload_image)
widgetUtils.connect_event(self.dialog.remove, widgetUtils.BUTTON_PRESSED, self.remove_attachment)
self.dialog.get_response()
@@ -37,3 +40,36 @@ class attach(object):
def check_remove_status(self):
if len(self.attachments) == 0 and self.dialog.attachments.get_count() == 0:
self.dialog.remove.Enable(False)
class attach(object):
def __init__(self, session):
self.session = session
self.attachments = list()
self.type = "online"
self.dialog = gui.attachDialog()
widgetUtils.connect_event(self.dialog.audio, widgetUtils.BUTTON_PRESSED, self.add_audio)
# widgetUtils.connect_event(self.dialog.remove, widgetUtils.BUTTON_PRESSED, self.remove_attachment)
self.dialog.get_response()
log.debug("Attachments controller started.")
def add_audio(self, *args, **kwargs):
list_of_audios = self.session.vk.client.audio.get(count=1000)
list_of_audios = list_of_audios["items"]
audios = []
for i in list_of_audios:
audios.append(u"{0}, {1}".format(i["title"], i["artist"]))
select = selector.selectAttachment(_(u"Select the audio files you want to send"), audios)
if select.get_response() == widgetUtils.OK and select.attachments.GetCount() > 0:
attachments = select.get_all_attachments()
for i in attachments:
print list_of_audios[i].keys()
list_of_audios[i]["type"] = "audio"
self.attachments.append(list_of_audios[i])
def parse_attachments(self):
result = ""
for i in self.attachments:
preresult = "{0}{1}_{2}".format(i["type"], i["owner_id"], i["id"])
result = preresult+","
return result

View File

@@ -12,6 +12,7 @@ import logging
import selector
import webbrowser
import posts
import attach
from wxUI.tabs import home
from pubsub import pub
from sessionmanager import session
@@ -19,6 +20,7 @@ from mysc.thread_utils import call_threaded
from wxUI import commonMessages, menus
from vk import upload
from vk.exceptions import VkAPIMethodError
from utils import add_attachment
log = logging.getLogger("controller.buffers")
@@ -591,6 +593,7 @@ class chatBuffer(baseBuffer):
def connect_events(self):
widgetUtils.connect_event(self.tab.send, widgetUtils.BUTTON_PRESSED, self.send_chat_to_user)
widgetUtils.connect_event(self.tab.attachment, widgetUtils.BUTTON_PRESSED, self.add_attachment)
self.tab.set_focus_function(self.onFocus)
def get_items(self, show_nextpage=False):
@@ -615,19 +618,29 @@ class chatBuffer(baseBuffer):
[self.insert(i, False) for i in self.session.db[self.name]["items"][:num]]
return retrieved
def add_attachment(self, *args, **kwargs):
a = attach.attach(self.session)
r = a.parse_attachments()
if r != "":
self.attachments_to_be_sent = r
def send_chat_to_user(self, *args, **kwargs):
text = self.tab.text.GetValue()
if text == "": return
# if text == "" and not hasattr(self, "attachments_to_be_sent"): return
call_threaded(self._send_message, text=text)
def _send_message(self, text):
try:
# try:
if hasattr(self, "attachments_to_be_sent"):
response = self.session.vk.client.messages.send(user_id=self.kwargs["user_id"], message=text, attachment=self.attachments_to_be_sent)
print self.attachments_to_be_sent
else:
response = self.session.vk.client.messages.send(user_id=self.kwargs["user_id"], message=text)
except VkAPIMethodError as ex:
if ex.code == 9:
output.speak(_(u"You have been sending a message that is already sent. Try to update the buffer if you can't see the new message in the history."))
finally:
self.tab.text.SetValue("")
# except VkAPIMethodError as ex:
# if ex.code == 9:
# output.speak(_(u"You have been sending a message that is already sent. Try to update the buffer if you can't see the new message in the history."))
# finally:
# self.tab.text.SetValue("")
def __init__(self, *args, **kwargs):
super(chatBuffer, self).__init__(*args, **kwargs)
@@ -635,7 +648,7 @@ class chatBuffer(baseBuffer):
def parse_attachments(self, post):
attachments = []
from posts import add_attachment
if post.has_key("attachments"):
for i in post["attachments"]:
# We don't need the photos_list attachment, so skip it.

View File

@@ -220,7 +220,7 @@ class Controller(object):
call_threaded(player.player.play, audio_object)
def play_audios(self, audios):
player.player.play_all(audios)
player.player.play_all(audios, shuffle=self.window.player_shuffle.IsChecked())
def view_post(self, post_object, controller_):
p = getattr(posts, controller_)(self.session, post_object)
@@ -385,6 +385,12 @@ class Controller(object):
return
# If the chat already exists, let's create a dictionary wich will contains data of the received message.
message = {"id": obj.message_id, "user_id": obj.user_id, "date": obj.timestamp, "body": obj.text, "attachments": obj.attachments}
# if attachments is true, let's request for the full message with attachments formatted in a better way.
# Todo: code improvements. We shouldn't need to request the same message again just for these attachments.
if len(message["attachments"]) != 0:
message_ids = message["id"]
results = self.session.vk.client.messages.getById(message_ids=message_ids)
message = results["items"][0]
# If outbox it's true, it means that message["from_id"] should be the current user. If not, the obj.user_id should be taken.
if obj.message_flags.has_key("outbox") == True:
message["from_id"] = self.session.user_id

View File

@@ -71,7 +71,7 @@ class post(object):
checker.clean()
def show_attach_dialog(self, *args, **kwargs):
a = attach.attach()
a = attach.attachmentUploader()
if len(a.attachments) != 0:
self.attachments = a.attachments

View File

@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import random
import output
import sound_lib
import logging
@@ -88,9 +89,11 @@ class audioPlayer(object):
if self.stream != None:
self.stream.volume = self.vol/100.0
def play_all(self, list_of_urls):
def play_all(self, list_of_urls, shuffle=False):
self.stop()
self.queue = list_of_urls
if shuffle:
random.shuffle(self.queue)
self.play(self.queue[0])
self.queue.remove(self.queue[0])
self.worker = RepeatingTimer(5, self.player_function)

View File

@@ -20,6 +20,7 @@ from wxUI.dialogs import postDialogs, urlList
from extra import SpellChecker, translator
from mysc.thread_utils import call_threaded
from wxUI import menus
from utils import add_attachment
log = logging.getLogger("controller.post")
@@ -31,32 +32,6 @@ def get_user(id, profiles):
# Translators: This string is user when socializer can't find the right user information.
return _(u"Unknown username")
def add_attachment(attachment):
msg = u""
tpe = ""
if attachment["type"] == "link":
msg = u"{0}: {1}".format(attachment["link"]["title"], attachment["link"]["url"])
tpe = _(u"Link")
elif attachment["type"] == "photo":
tpe = _(u"Photo")
msg = attachment["photo"]["text"]
if msg == "":
msg = _(u"no description available")
elif attachment["type"] == "video":
msg = u"{0}".format(attachment["video"]["title"],)
tpe = _(u"Video")
elif attachment["type"] == "audio":
msg = u"{0}".format(" ".join(session.compose_audio(attachment["audio"])))
tpe = _(u"Audio")
elif attachment["type"] == "doc":
if attachment["doc"].has_key("preview") and attachment["doc"]["preview"].has_key("audio_msg"):
tpe = _(u"Voice message")
msg = utils.seconds_to_string(attachment["doc"]["preview"]["audio_msg"]["duration"])
else:
msg = u"{0}".format(attachment["doc"]["title"])
tpe = _(u"{0} file").format(attachment["doc"]["ext"])
return [tpe, msg]
def get_message(status):
message = ""
if status.has_key("text"):
@@ -400,7 +375,8 @@ class postController(object):
log.debug("Unhandled attachment: %r" % (attachment,))
def __del__(self):
self.worker.finished.set()
if hasattr(self, "worker"):
self.worker.finished.set()
class comment(object):
def __init__(self, session, comment_object):