2016-02-13 17:06:36 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-02-15 16:49:09 -06:00
|
|
|
import os
|
2016-02-13 17:06:36 -06:00
|
|
|
import arrow
|
|
|
|
import messages
|
|
|
|
import languageHandler
|
|
|
|
import widgetUtils
|
|
|
|
import output
|
|
|
|
import wx
|
|
|
|
import webbrowser
|
2016-02-15 15:09:33 -06:00
|
|
|
import utils
|
2016-02-17 17:37:57 -06:00
|
|
|
from sessionmanager import session # We'll use some functions from there
|
2016-02-13 17:06:36 -06:00
|
|
|
from pubsub import pub
|
|
|
|
from wxUI.dialogs import postDialogs, urlList
|
|
|
|
from extra import SpellChecker, translator
|
|
|
|
from mysc.thread_utils import call_threaded
|
|
|
|
from wxUI import menus
|
|
|
|
|
|
|
|
class postController(object):
|
|
|
|
def __init__(self, session, postObject):
|
|
|
|
super(postController, self).__init__()
|
|
|
|
self.session = session
|
|
|
|
self.post = postObject
|
|
|
|
self.dialog = postDialogs.post()
|
|
|
|
self.dialog.comments.list.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.show_comment)
|
|
|
|
# widgetUtils.connect_event(self.message.spellcheck, widgetUtils.BUTTON_PRESSED, self.spellcheck)
|
|
|
|
# widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate)
|
|
|
|
widgetUtils.connect_event(self.dialog.like, widgetUtils.BUTTON_PRESSED, self.post_like)
|
|
|
|
widgetUtils.connect_event(self.dialog.comment, widgetUtils.BUTTON_PRESSED, self.add_comment)
|
|
|
|
widgetUtils.connect_event(self.dialog.tools, widgetUtils.BUTTON_PRESSED, self.show_tools_menu)
|
|
|
|
self.dialog.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.show_menu, self.dialog.comments.list)
|
|
|
|
self.dialog.Bind(wx.EVT_LIST_KEY_DOWN, self.show_menu_by_key, self.dialog.comments.list)
|
2016-02-17 17:37:57 -06:00
|
|
|
# call_threaded(self.load_all_components)
|
|
|
|
self.get_post_information()
|
|
|
|
|
|
|
|
def get_post_information(self):
|
|
|
|
if self.post.has_key("type"):
|
|
|
|
if self.post["type"] == "post":
|
|
|
|
from_ = self.session.get_user_name(self.post["source_id"])
|
2016-02-22 05:53:37 -06:00
|
|
|
if self.post.has_key("copy_owner_id"):
|
|
|
|
title = _(u"repost from {0}").format(from_,)
|
|
|
|
else:
|
|
|
|
title = _(u"Post from {0}").format(from_,)
|
2016-02-17 17:37:57 -06:00
|
|
|
self.dialog.set_title(title)
|
|
|
|
message = u""
|
|
|
|
if self.post.has_key("text"):
|
2016-02-22 08:49:51 -06:00
|
|
|
message = utils.clean_text(self.post["text"])
|
2016-02-17 17:37:57 -06:00
|
|
|
if self.post.has_key("attachment"):
|
|
|
|
print self.post["attachment"].keys()
|
2016-02-22 08:49:51 -06:00
|
|
|
message = message+session.add_attachment(self.post["attachment"])
|
2016-02-17 17:37:57 -06:00
|
|
|
self.dialog.set_post(message)
|
2016-02-13 17:06:36 -06:00
|
|
|
|
|
|
|
def load_all_components(self):
|
|
|
|
self.get_likes()
|
|
|
|
self.get_shares()
|
|
|
|
self.get_comments()
|
|
|
|
|
|
|
|
def post_like(self, *args, **kwargs):
|
|
|
|
lk = self.session.like(self.post["id"])
|
|
|
|
self.get_likes()
|
|
|
|
|
|
|
|
def get_likes(self):
|
|
|
|
self.likes = self.session.fb.client.get_connections(id=self.post["id"], connection_name="likes", summary=True)
|
|
|
|
self.dialog.set_likes(self.likes["summary"]["total_count"])
|
|
|
|
|
|
|
|
def get_shares(self):
|
|
|
|
self.shares = self.session.fb.client.get_connections(id=self.post["id"], connection_name="sharedposts")
|
|
|
|
self.dialog.set_shares(str(len(self.shares["data"])))
|
|
|
|
|
|
|
|
def get_comments(self):
|
|
|
|
self.comments = self.session.fb.client.get_connections(id=self.post["id"], connection_name="comments", filter="stream")
|
|
|
|
comments = []
|
|
|
|
for i in self.comments["data"]:
|
|
|
|
from_ = i["from"]["name"]
|
|
|
|
if len(i["message"]) > 100:
|
|
|
|
comment = i["message"][:100]
|
|
|
|
else:
|
|
|
|
comment = i["message"]
|
|
|
|
original_date = arrow.get(i["created_time"], "YYYY-MM-DTHH:m:sZ", locale="en")
|
|
|
|
created_at = original_date.humanize(locale=languageHandler.getLanguage())
|
|
|
|
likes = str(i["like_count"])
|
|
|
|
comments.append([from_, comment, created_at, likes,])
|
|
|
|
self.dialog.insert_comments(comments)
|
|
|
|
|
|
|
|
def add_comment(self, *args, **kwargs):
|
|
|
|
comment = messages.comment(title=_(u"Add a comment"), caption="", text="")
|
|
|
|
if comment.message.get_response() == widgetUtils.OK:
|
|
|
|
msg = comment.message.get_text().encode("utf-8")
|
|
|
|
try:
|
|
|
|
self.session.fb.client.put_comment(self.post["id"], msg)
|
|
|
|
output.speak(_(u"You've posted a comment"))
|
|
|
|
if len(self.comments["data"]) < 25:
|
|
|
|
self.clear_comments_list()
|
|
|
|
self.get_comments()
|
|
|
|
except Exception as msg:
|
|
|
|
print msg
|
|
|
|
|
|
|
|
def clear_comments_list(self):
|
|
|
|
self.dialog.comments.clear()
|
|
|
|
|
|
|
|
def show_comment(self, *args, **kwargs):
|
|
|
|
c = comment(self.session, self.comments["data"][self.dialog.comments.get_selected()])
|
|
|
|
c.dialog.get_response()
|
|
|
|
|
|
|
|
def show_menu(self, *args, **kwargs):
|
|
|
|
if self.dialog.comments.get_count() == 0: return
|
|
|
|
menu = menus.commentMenu()
|
|
|
|
widgetUtils.connect_event(self.dialog, widgetUtils.MENU, self.show_comment, menuitem=menu.open)
|
|
|
|
widgetUtils.connect_event(self.dialog, widgetUtils.MENU, self.comment_like, menuitem=menu.like)
|
|
|
|
widgetUtils.connect_event(self.dialog, widgetUtils.MENU, self.comment_unlike, menuitem=menu.unlike)
|
|
|
|
self.dialog.PopupMenu(menu, self.dialog.comments.list.GetPosition())
|
|
|
|
|
|
|
|
def show_menu_by_key(self, ev):
|
|
|
|
if ev.GetKeyCode() == wx.WXK_WINDOWS_MENU:
|
|
|
|
self.show_menu()
|
|
|
|
|
|
|
|
def show_tools_menu(self, *args, **kwargs):
|
|
|
|
menu = menus.toolsMenu()
|
|
|
|
widgetUtils.connect_event(self.dialog, widgetUtils.MENU, self.open_url, menuitem=menu.url)
|
|
|
|
widgetUtils.connect_event(self.dialog, widgetUtils.MENU, self.translate, menuitem=menu.translate)
|
|
|
|
widgetUtils.connect_event(self.dialog, widgetUtils.MENU, self.spellcheck, menuitem=menu.CheckSpelling)
|
|
|
|
self.dialog.PopupMenu(menu, self.dialog.tools.GetPosition())
|
|
|
|
|
|
|
|
def comment_like(self, *args, **kwargs):
|
|
|
|
comment_id = self.comments["data"][self.dialog.comments.get_selected()]["id"]
|
|
|
|
self.session.like(comment_id)
|
|
|
|
output.speak(_(u"You do like this comment"))
|
|
|
|
|
|
|
|
def comment_unlike(self, *args, **kwargs):
|
|
|
|
comment_id = self.comments["data"][self.dialog.comments.get_selected()]["id"]
|
|
|
|
self.session.unlike(comment_id)
|
|
|
|
output.speak(_(u"You don't like this comment"))
|
|
|
|
|
|
|
|
def translate(self, *args, **kwargs):
|
|
|
|
dlg = translator.gui.translateDialog()
|
|
|
|
if dlg.get_response() == widgetUtils.OK:
|
|
|
|
text_to_translate = self.dialog.post_view.GetValue().encode("utf-8")
|
|
|
|
source = [x[0] for x in translator.translator.available_languages()][dlg.get("source_lang")]
|
|
|
|
dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")]
|
|
|
|
msg = translator.translator.translate(text_to_translate, source, dest)
|
|
|
|
self.dialog.post_view.ChangeValue(msg)
|
|
|
|
output.speak(_(u"Translated"))
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
def spellcheck(self, *args, **kwargs):
|
|
|
|
text = self.dialog.post_view.GetValue()
|
|
|
|
checker = SpellChecker.spellchecker.spellChecker(text, "")
|
|
|
|
if hasattr(checker, "fixed_text"):
|
|
|
|
self.dialog.post_view.ChangeValue(checker.fixed_text)
|
|
|
|
|
|
|
|
def open_url(self, *args, **kwargs):
|
|
|
|
text = self.dialog.post_view.GetValue()
|
|
|
|
urls = find_urls(text)
|
|
|
|
url = None
|
|
|
|
if len(urls) == 0: return
|
|
|
|
if len(urls) == 1:
|
|
|
|
url = urls[0]
|
|
|
|
elif len(urls) > 1:
|
|
|
|
url_list = urlList.urlList()
|
|
|
|
url_list.populate_list(urls)
|
|
|
|
if url_list.get_response() == widgetUtils.OK:
|
|
|
|
url = urls[url_list.get_item()]
|
|
|
|
if url != None:
|
|
|
|
output.speak(_(u"Opening URL..."), True)
|
|
|
|
webbrowser.open_new_tab(url)
|
|
|
|
|
|
|
|
class comment(object):
|
|
|
|
def __init__(self, session, comment_object):
|
|
|
|
super(comment, self).__init__()
|
|
|
|
self.session = session
|
|
|
|
self.comment = comment_object
|
|
|
|
self.dialog = postDialogs.comment()
|
|
|
|
from_ = self.comment["from"]["name"]
|
|
|
|
message = self.comment["message"]
|
|
|
|
original_date = arrow.get(self.comment["created_time"], "YYYY-MM-DTHH:m:sZ", locale="en")
|
|
|
|
created_at = original_date.humanize(locale=languageHandler.getLanguage())
|
|
|
|
self.dialog.set_post(message)
|
|
|
|
self.dialog.set_title(_(u"Comment from {0}").format(from_,))
|
|
|
|
widgetUtils.connect_event(self.dialog.like, widgetUtils.BUTTON_PRESSED, self.post_like)
|
|
|
|
call_threaded(self.get_likes)
|
|
|
|
|
|
|
|
def get_likes(self):
|
|
|
|
self.likes = self.session.fb.client.get_connections(id=self.comment["id"], connection_name="likes", summary=True)
|
|
|
|
self.dialog.set_likes(self.likes["summary"]["total_count"])
|
|
|
|
|
|
|
|
def post_like(self, *args, **kwargs):
|
|
|
|
lk = self.session.like(self.comment["id"])
|
|
|
|
self.get_likes()
|
|
|
|
|
2016-02-15 15:09:33 -06:00
|
|
|
class audio(postController):
|
|
|
|
def __init__(self, session, postObject):
|
2016-02-24 10:30:07 -06:00
|
|
|
self.added_audios = {}
|
2016-02-15 15:09:33 -06:00
|
|
|
self.session = session
|
|
|
|
self.post = postObject
|
|
|
|
self.dialog = postDialogs.audio()
|
2016-02-24 10:30:07 -06:00
|
|
|
widgetUtils.connect_event(self.dialog.list, widgetUtils.LISTBOX_CHANGED, self.handle_changes)
|
|
|
|
self.load_audios()
|
|
|
|
self.fill_information(0)
|
2016-02-15 16:49:09 -06:00
|
|
|
widgetUtils.connect_event(self.dialog.download, widgetUtils.BUTTON_PRESSED, self.download)
|
2016-02-15 17:49:39 -06:00
|
|
|
widgetUtils.connect_event(self.dialog.play, widgetUtils.BUTTON_PRESSED, self.play)
|
2016-02-19 04:17:49 -06:00
|
|
|
widgetUtils.connect_event(self.dialog.add, widgetUtils.BUTTON_PRESSED, self.add_to_library)
|
|
|
|
widgetUtils.connect_event(self.dialog.remove, widgetUtils.BUTTON_PRESSED, self.remove_from_library)
|
|
|
|
|
|
|
|
def add_to_library(self, *args, **kwargs):
|
2016-02-24 10:30:07 -06:00
|
|
|
post = self.post[self.dialog.get_audio()]
|
2016-02-19 04:17:49 -06:00
|
|
|
args = {}
|
2016-02-24 10:30:07 -06:00
|
|
|
args["audio_id"] = post["id"]
|
|
|
|
if post.has_key("album_id"):
|
|
|
|
args["album_id"] = post["album_id"]
|
|
|
|
args["owner_id"] = post["owner_id"]
|
2016-02-19 04:17:49 -06:00
|
|
|
audio = self.session.vk.client.audio.add(**args)
|
|
|
|
if audio != None and int(audio) > 21:
|
2016-02-24 10:30:07 -06:00
|
|
|
self.added_audios[post["id"]] = audio
|
2016-02-19 04:17:49 -06:00
|
|
|
self.dialog.change_state("add", False)
|
|
|
|
self.dialog.change_state("remove", True)
|
|
|
|
|
|
|
|
def remove_from_library(self, *args, **kwargs):
|
2016-02-24 10:30:07 -06:00
|
|
|
post = self.post[self.dialog.get_audio()]
|
2016-02-19 04:17:49 -06:00
|
|
|
args = {}
|
2016-02-24 10:30:07 -06:00
|
|
|
if self.added_audios.has_key(post["id"]):
|
|
|
|
args["audio_id"] = self.added_audios[post["id"]]
|
|
|
|
args["owner_id"] = self.session.user_id
|
2016-02-19 04:17:49 -06:00
|
|
|
else:
|
2016-02-24 10:30:07 -06:00
|
|
|
args["audio_id"] = post["id"]
|
|
|
|
args["owner_id"] = post["owner_id"]
|
2016-02-19 04:17:49 -06:00
|
|
|
result = self.session.vk.client.audio.delete(**args)
|
|
|
|
if int(result) == 1:
|
|
|
|
self.dialog.change_state("add", True)
|
|
|
|
self.dialog.change_state("remove", False)
|
2016-02-24 10:30:07 -06:00
|
|
|
if self.added_audios.has_key(post["id"]):
|
|
|
|
self.added_audios.pop(post["id"])
|
|
|
|
|
|
|
|
def fill_information(self, index):
|
|
|
|
post = self.post[index]
|
|
|
|
if post.has_key("artist"):
|
|
|
|
self.dialog.set("artist", post["artist"])
|
|
|
|
if post.has_key("title"):
|
|
|
|
self.dialog.set("title", post["title"])
|
|
|
|
if post.has_key("duration"):
|
|
|
|
self.dialog.set("duration", utils.seconds_to_string(post["duration"]))
|
|
|
|
self.dialog.set_title(u"{0} - {1}".format(post["title"], post["artist"]))
|
2016-02-15 15:09:33 -06:00
|
|
|
call_threaded(self.get_lyrics)
|
2016-02-24 10:30:07 -06:00
|
|
|
if post["owner_id"] == self.session.user_id or self.added_audios.has_key(post["id"]) == True:
|
|
|
|
self.dialog.change_state("remove", True)
|
|
|
|
self.dialog.change_state("add", False)
|
|
|
|
else:
|
|
|
|
self.dialog.change_state("add", True)
|
|
|
|
self.dialog.change_state("remove", False)
|
2016-02-15 15:09:33 -06:00
|
|
|
|
|
|
|
def get_lyrics(self):
|
2016-02-24 10:30:07 -06:00
|
|
|
post = self.post[self.dialog.get_audio()]
|
|
|
|
if post.has_key("lyrics_id"):
|
|
|
|
l = self.session.vk.client.audio.getLyrics(lyrics_id=int(post["lyrics_id"]))
|
2016-02-15 16:49:09 -06:00
|
|
|
self.dialog.set("lyric", l["text"])
|
|
|
|
|
|
|
|
def download(self, *args, **kwargs):
|
2016-02-24 10:30:07 -06:00
|
|
|
post = self.post[self.dialog.get_audio()]
|
|
|
|
f = u"{0} - {1}.mp3".format(post["title"], post["artist"])
|
2016-02-15 16:49:09 -06:00
|
|
|
path = self.dialog.get_destination_path(f)
|
|
|
|
if path != None:
|
2016-02-24 10:30:07 -06:00
|
|
|
pub.sendMessage("download-file", url=post["url"], filename=path)
|
2016-02-15 17:49:39 -06:00
|
|
|
|
|
|
|
def play(self, *args, **kwargs):
|
2016-02-24 10:30:07 -06:00
|
|
|
post = self.post[self.dialog.get_audio()]
|
|
|
|
pub.sendMessage("play-audio", audio_object=post["url"])
|
|
|
|
|
|
|
|
def load_audios(self):
|
|
|
|
for i in self.post:
|
|
|
|
s = u"{0} - {1}. {2}".format(i["title"], i["artist"], utils.seconds_to_string(i["duration"]))
|
|
|
|
self.dialog.insert_audio(s)
|
2016-02-25 05:08:56 -06:00
|
|
|
self.dialog.list.SetSelection(0)
|
2016-02-24 10:30:07 -06:00
|
|
|
|
|
|
|
def handle_changes(self, *args, **kwargs):
|
|
|
|
p = self.dialog.get_audio()
|
|
|
|
self.fill_information(p)
|
2016-02-17 17:37:57 -06:00
|
|
|
|
|
|
|
class friendship(object):
|
|
|
|
|
|
|
|
def __init__(self, session, post):
|
|
|
|
self.session = session
|
|
|
|
self.post = post
|
|
|
|
self.dialog = postDialogs.friendship()
|
|
|
|
list_of_friends = self.get_friend_names()
|
|
|
|
from_ = self.session.get_user_name(self.post["source_id"])
|
|
|
|
title = _(u"{0} added the following friends").format(from_,)
|
|
|
|
self.dialog.set_title(title)
|
|
|
|
self.set_friends_list(list_of_friends)
|
|
|
|
|
|
|
|
def get_friend_names(self):
|
|
|
|
self.friends = self.post["friends"][1:]
|
|
|
|
return [self.session.get_user_name(i["uid"]) for i in self.friends]
|
|
|
|
|
|
|
|
def set_friends_list(self, friendslist):
|
|
|
|
for i in friendslist:
|
|
|
|
self.dialog.friends.insert_item(False, *[i])
|