Separated rendering functions from the rest of the code
This commit is contained in:
176
src/sessionmanager/renderers.py
Normal file
176
src/sessionmanager/renderers.py
Normal file
@@ -0,0 +1,176 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" this module contains everything used to render different kind of posts (posts in the home buffer,
|
||||
Chat messages, audios, videos, photos, comments in posts, etc)"""
|
||||
import arrow
|
||||
import languageHandler
|
||||
import logging
|
||||
import utils
|
||||
|
||||
log = logging.getLogger(__file__)
|
||||
|
||||
### Some util funtions
|
||||
|
||||
def extract_attachment(attachment):
|
||||
""" Adds information about attachment files in posts. It only adds the text, I mean, no attachment file is added here.
|
||||
This will produce a result like:
|
||||
'website: http://url.com'.
|
||||
'photo: A forest'."""
|
||||
msg = u""
|
||||
if attachment["type"] == "link":
|
||||
msg = u"{0}: {1}".format(attachment["link"]["title"], attachment["link"]["url"])
|
||||
elif attachment["type"] == "photo":
|
||||
msg = attachment["photo"]["text"]
|
||||
if msg == "":
|
||||
return _(u"photo with no description available")
|
||||
elif attachment["type"] == "video":
|
||||
msg = _(u"video: {0}").format(attachment["video"]["title"],)
|
||||
return msg
|
||||
|
||||
def short_text(status):
|
||||
""" This shorts the text to 140 characters for displaying it in the list control of buffers."""
|
||||
message = ""
|
||||
# copy_story indicates that the post is a shared repost.
|
||||
if status.has_key("copy_history"):
|
||||
txt = status["copy_history"][0]["text"]
|
||||
else:
|
||||
txt = status["text"]
|
||||
if len(txt) < 140:
|
||||
message = utils.clean_text(txt)
|
||||
else:
|
||||
message = utils.clean_text(txt[:139])
|
||||
return message
|
||||
|
||||
def clean_audio(audio):
|
||||
""" Remove unavailable songs due to different reasons. This is used to clean the audio list when people adds audios and need to be displayed in the buffer."""
|
||||
for i in audio["items"][:]:
|
||||
if type(i) == bool:
|
||||
audio["items"].remove(i)
|
||||
audio["count"] = audio["count"] -1
|
||||
return audio
|
||||
|
||||
### Render functions
|
||||
|
||||
def render_person(status, session):
|
||||
""" Render users in people buffers such as everything related to friendships or buffers created with only people.
|
||||
Example result: ["John Doe", "An hour ago"]
|
||||
Reference: https://vk.com/dev/fields"""
|
||||
if status.has_key("last_seen"):
|
||||
original_date = arrow.get(status["last_seen"]["time"])
|
||||
# Translators: This is the date of last seen
|
||||
last_seen = _(u"{0}").format(original_date.humanize(locale=languageHandler.getLanguage()),)
|
||||
# Account suspended or deleted.
|
||||
elif status.has_key("last_seen") == False and status.has_key("deactivated"):
|
||||
last_seen = _(u"Account deactivated")
|
||||
return [u"{0} {1}".format(status["first_name"], status["last_name"]), last_seen]
|
||||
|
||||
def render_newsfeed_item(status, session):
|
||||
""" This me☻thod is used to render an item of the news feed.
|
||||
References:
|
||||
https://vk.com/dev/newsfeed.get
|
||||
https://vk.com/dev/post_source
|
||||
https://vk.com/dev/post
|
||||
"""
|
||||
user = session.get_user_name(status["source_id"], case_name="nom")
|
||||
# See if this is a post or repost.
|
||||
if status.has_key("copy_history"):
|
||||
user = _(u"{0} has shared the {1}'s post").format(user, session.get_user_name(status["copy_history"][0]["owner_id"]))
|
||||
message = ""
|
||||
original_date = arrow.get(status["date"])
|
||||
created_at = original_date.humanize(locale=languageHandler.getLanguage())
|
||||
# handle status updates.
|
||||
if status["type"] == "post":
|
||||
message += short_text(status)
|
||||
if status.has_key("attachment") and len(status["attachment"]) > 0:
|
||||
message += extract_attachment(status["attachment"])
|
||||
# If there is no message after adding text, it's because a pphoto with no description has been found.
|
||||
# so let's manually add the "no description" tag here.
|
||||
if message == "":
|
||||
message = "no description available"
|
||||
# Handle audio rendering.
|
||||
elif status["type"] == "audio":
|
||||
# removes deleted audios.
|
||||
status["audio"] = clean_audio(status["audio"])
|
||||
if status["audio"]["count"] == 1:
|
||||
message = _(u"{0} has added an audio: {1}").format(user, u", ".join(render_audio(status["audio"]["items"][0], session)),)
|
||||
else:
|
||||
prem = ""
|
||||
for i in xrange(0, status["audio"]["count"]):
|
||||
composed_audio = render_audio(status["audio"]["items"][i], session)
|
||||
prem += u"{0} - {1}, ".format(composed_audio[0], composed_audio[1])
|
||||
message = _(u"{0} has added {1} audios: {2}").format(user, status["audio"]["count"], prem)
|
||||
# handle new friends for people in the news buffer.
|
||||
elif status["type"] == "friend":
|
||||
msg_users = u""
|
||||
if status.has_key("friends"):
|
||||
for i in status["friends"]["items"]:
|
||||
msg_users = msg_users + u"{0}, ".format(session.get_user_name(i["user_id"], "nom"))
|
||||
else:
|
||||
print status.keys()
|
||||
message = _(u"{0} hadded friends: {1}").format(user, msg_users)
|
||||
elif status["type"] == "video":
|
||||
if status["video"]["count"] == 1:
|
||||
message = _(u"{0} has added a video: {1}").format(user, u", ".join(render_video(status["video"]["items"][0], session)),)
|
||||
else:
|
||||
prem = ""
|
||||
for i in xrange(0, status["video"]["count"]):
|
||||
composed_video = render_video(status["video"]["items"][i], session)
|
||||
prem += u"{0} - {1}, ".format(composed_video[0], composed_video[1])
|
||||
message = _(u"{0} has added {1} videos: {2}").format(user, status["video"]["count"], prem)
|
||||
else:
|
||||
if status["type"] != "post": print status
|
||||
return [user, message, created_at]
|
||||
|
||||
def render_message(message, session):
|
||||
""" Render a message posted in a private conversation.
|
||||
Reference: https://vk.com/dev/message"""
|
||||
user = session.get_user_name(message["from_id"], "nom")
|
||||
original_date = arrow.get(message["date"])
|
||||
now = arrow.now()
|
||||
original_date = original_date.to(now.tzinfo)
|
||||
# Format the date here differently depending in if this is the same day for both dates or not.
|
||||
if original_date.day == now.day:
|
||||
created_at = original_date.format(_(u"H:mm."), locale=languageHandler.getLanguage())
|
||||
else:
|
||||
created_at = original_date.format(_(u"H:mm. dddd, MMMM D, YYYY"), locale=languageHandler.getLanguage())
|
||||
# No idea why some messages send "text" instead "body"
|
||||
if message.has_key("body"):
|
||||
body = message["body"]
|
||||
else:
|
||||
body = message["text"]
|
||||
return [u"{2}, {0} {1}".format(body, created_at, user)]
|
||||
|
||||
def render_status(status, session):
|
||||
""" Render a wall post (shown in user's wall, not in newsfeed).
|
||||
Reference: https://vk.com/dev/post"""
|
||||
user = session.get_user_name(status["from_id"], "nom")
|
||||
if status.has_key("copy_history"):
|
||||
user = _(u"{0} has shared the {1}'s post").format(user, session.get_user_name(status["copy_history"][0]["owner_id"]))
|
||||
message = ""
|
||||
original_date = arrow.get(status["date"])
|
||||
created_at = original_date.humanize(locale=languageHandler.getLanguage())
|
||||
if status.has_key("copy_owner_id"):
|
||||
user = _(u"{0} has shared the {1}'s post").format(user, session.get_user_name(status["copy_owner_id"]))
|
||||
if status["post_type"] == "post" or status["post_type"] == "copy":
|
||||
message += short_text(status)
|
||||
if status.has_key("attachment") and len(status["attachment"]) > 0:
|
||||
message += extract_attachment(status["attachment"])
|
||||
if message == "":
|
||||
message = "no description available"
|
||||
return [user, message, created_at]
|
||||
|
||||
def render_audio(audio, session=None):
|
||||
""" Render audio files added to VK.
|
||||
Example result:
|
||||
["Song title", "Artist", "03:15"]
|
||||
reference: https://vk.com/dev/audio_object"""
|
||||
if audio == False: return [_(u"Audio removed from library"), "", ""]
|
||||
return [audio["title"], audio["artist"], utils.seconds_to_string(audio["duration"])]
|
||||
|
||||
def render_video(video, session=None):
|
||||
""" Render a video file from VK.
|
||||
Example result:
|
||||
["Video title", "Video description", "01:30:28"]
|
||||
Reference: https://vk.com/dev/video_object"""
|
||||
if video == False:
|
||||
return [_(u"Video not available"), "", ""]
|
||||
return [video["title"], video["description"], utils.seconds_to_string(video["duration"])]
|
@@ -1,11 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import time
|
||||
import arrow
|
||||
import logging
|
||||
import languageHandler
|
||||
import paths
|
||||
import vkSessionHandler
|
||||
import logging
|
||||
import utils
|
||||
import sound
|
||||
from config_utils import Configuration, ConfigurationResetException
|
||||
from pubsub import pub
|
||||
@@ -37,137 +34,6 @@ def find_item(list, item):
|
||||
return True
|
||||
return False
|
||||
|
||||
def add_attachment(attachment):
|
||||
""" Adds information about attachment files in posts. It only adds the text, I mean, no attachment file is added here.
|
||||
This will produce a result like 'Title of a web page: http://url.xxx', etc."""
|
||||
msg = u""
|
||||
if attachment["type"] == "link":
|
||||
msg = u"{0}: {1}".format(attachment["link"]["title"], attachment["link"]["url"])
|
||||
elif attachment["type"] == "photo":
|
||||
msg = attachment["photo"]["text"]
|
||||
if msg == "":
|
||||
return _(u"photo with no description available")
|
||||
elif attachment["type"] == "video":
|
||||
msg = _(u"video: {0}").format(attachment["video"]["title"],)
|
||||
return msg
|
||||
|
||||
def add_text(status):
|
||||
""" This shorts the text to 140 characters for displaying it in the list control."""
|
||||
message = ""
|
||||
if status.has_key("copy_history"):
|
||||
txt = status["copy_history"][0]["text"]
|
||||
else:
|
||||
txt = status["text"]
|
||||
if len(txt) < 140:
|
||||
message = utils.clean_text(txt)
|
||||
else:
|
||||
message = utils.clean_text(txt[:139])
|
||||
return message
|
||||
|
||||
def compose_person(status, session):
|
||||
if status.has_key("last_seen"):
|
||||
original_date = arrow.get(status["last_seen"]["time"])
|
||||
# Translators: This is the date of last seen
|
||||
last_seen = _(u"{0}").format(original_date.humanize(locale=languageHandler.getLanguage()),)
|
||||
elif status.has_key("last_seen") == False and status.has_key("deactivated"):
|
||||
last_seen = _(u"Account deactivated")
|
||||
return [u"{0} {1}".format(status["first_name"], status["last_name"]), last_seen]
|
||||
|
||||
def compose_new(status, session):
|
||||
""" This method is used to compose an item of the news feed."""
|
||||
user = session.get_user_name(status["source_id"], case_name="nom")
|
||||
if status.has_key("copy_history"):
|
||||
user = _(u"{0} has shared the {1}'s post").format(user, session.get_user_name(status["copy_history"][0]["owner_id"]))
|
||||
message = ""
|
||||
original_date = arrow.get(status["date"])
|
||||
created_at = original_date.humanize(locale=languageHandler.getLanguage())
|
||||
if status["type"] == "post":
|
||||
message += add_text(status)
|
||||
if status.has_key("attachment") and len(status["attachment"]) > 0:
|
||||
message += add_attachment(status["attachment"])
|
||||
if message == "":
|
||||
message = "no description available"
|
||||
elif status["type"] == "audio":
|
||||
# removes deleted audios.
|
||||
status["audio"] = clean_audio(status["audio"])
|
||||
if status["audio"]["count"] == 1:
|
||||
message = _(u"{0} has added an audio: {1}").format(user, u", ".join(compose_audio(status["audio"]["items"][0], session)),)
|
||||
else:
|
||||
prem = ""
|
||||
for i in xrange(0, status["audio"]["count"]):
|
||||
composed_audio = compose_audio(status["audio"]["items"][i], session)
|
||||
prem += u"{0} - {1}, ".format(composed_audio[0], composed_audio[1])
|
||||
message = _(u"{0} has added {1} audios: {2}").format(user, status["audio"]["count"], prem)
|
||||
elif status["type"] == "friend":
|
||||
msg_users = u""
|
||||
if status.has_key("friends"):
|
||||
for i in status["friends"]["items"]:
|
||||
msg_users = msg_users + u"{0}, ".format(session.get_user_name(i["user_id"], "nom"))
|
||||
else:
|
||||
print status.keys()
|
||||
message = _(u"{0} hadded friends: {1}").format(user, msg_users)
|
||||
elif status["type"] == "video":
|
||||
if status["video"]["count"] == 1:
|
||||
message = _(u"{0} has added a video: {1}").format(user, u", ".join(compose_video(status["video"]["items"][0], session)),)
|
||||
else:
|
||||
prem = ""
|
||||
for i in xrange(0, status["video"]["count"]):
|
||||
composed_video = compose_video(status["video"]["items"][i], session)
|
||||
prem += u"{0} - {1}, ".format(composed_video[0], composed_video[1])
|
||||
message = _(u"{0} has added {1} videos: {2}").format(user, status["video"]["count"], prem)
|
||||
else:
|
||||
if status["type"] != "post": print status
|
||||
return [user, message, created_at]
|
||||
|
||||
def clean_audio(audio):
|
||||
for i in audio["items"][:]:
|
||||
if type(i) == bool:
|
||||
audio["items"].remove(i)
|
||||
audio["count"] = audio["count"] -1
|
||||
return audio
|
||||
|
||||
def compose_message(message, session):
|
||||
user = session.get_user_name(message["from_id"], "nom")
|
||||
original_date = arrow.get(message["date"])
|
||||
now = arrow.now()
|
||||
original_date = original_date.to(now.tzinfo)
|
||||
# Format the date here differently depending in if this is the same day for both dates or not.
|
||||
if original_date.day == now.day:
|
||||
created_at = original_date.format(_(u"H:mm."), locale=languageHandler.getLanguage())
|
||||
else:
|
||||
created_at = original_date.format(_(u"H:mm. dddd, MMMM D, YYYY"), locale=languageHandler.getLanguage())
|
||||
# No idea why some messages send "text" instead "body"
|
||||
if message.has_key("body"):
|
||||
body = message["body"]
|
||||
else:
|
||||
body = message["text"]
|
||||
return [u"{2}, {0} {1}".format(body, created_at, user)]
|
||||
|
||||
def compose_status(status, session):
|
||||
user = session.get_user_name(status["from_id"], "nom")
|
||||
if status.has_key("copy_history"):
|
||||
user = _(u"{0} has shared the {1}'s post").format(user, session.get_user_name(status["copy_history"][0]["owner_id"]))
|
||||
message = ""
|
||||
original_date = arrow.get(status["date"])
|
||||
created_at = original_date.humanize(locale=languageHandler.getLanguage())
|
||||
if status.has_key("copy_owner_id"):
|
||||
user = _(u"{0} has shared the {1}'s post").format(user, session.get_user_name(status["copy_owner_id"]))
|
||||
if status["post_type"] == "post" or status["post_type"] == "copy":
|
||||
message += add_text(status)
|
||||
if status.has_key("attachment") and len(status["attachment"]) > 0:
|
||||
message += add_attachment(status["attachment"])
|
||||
if message == "":
|
||||
message = "no description available"
|
||||
return [user, message, created_at]
|
||||
|
||||
def compose_audio(audio, session=None):
|
||||
if audio == False: return [_(u"Audio removed from library"), "", ""]
|
||||
return [audio["title"], audio["artist"], utils.seconds_to_string(audio["duration"])]
|
||||
|
||||
def compose_video(video, session=None):
|
||||
if video == False: return [_(u"Audio removed from library"), "", ""]
|
||||
return [video["title"], video["description"], utils.seconds_to_string(video["duration"])]
|
||||
|
||||
class vkSession(object):
|
||||
|
||||
def order_buffer(self, name, data, show_nextpage):
|
||||
|
90
src/sessionmanager/utils.py
Normal file
90
src/sessionmanager/utils.py
Normal file
@@ -0,0 +1,90 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" Some utilities. I no have idea how I should put these, so..."""
|
||||
import os
|
||||
import requests
|
||||
import re
|
||||
import logging
|
||||
from sessionmanager import session
|
||||
log = logging.getLogger("utils")
|
||||
url_re = re.compile("(?:\w+://|www\.)[^ ,.?!#%=+][^ ]*")
|
||||
bad_chars = '\'\\.,[](){}:;"'
|
||||
|
||||
def seconds_to_string(seconds, precision=0):
|
||||
day = seconds // 86400
|
||||
hour = seconds // 3600
|
||||
min = (seconds // 60) % 60
|
||||
sec = seconds - (hour * 3600) - (min * 60)
|
||||
sec_spec = "." + str(precision) + "f"
|
||||
sec_string = sec.__format__(sec_spec)
|
||||
string = ""
|
||||
if day == 1:
|
||||
string += _(u"%d day, ") % day
|
||||
elif day >= 2:
|
||||
string += _(u"%d days, ") % day
|
||||
if (hour == 1):
|
||||
string += _(u"%d hour, ") % hour
|
||||
elif (hour >= 2):
|
||||
string += _("%d hours, ") % hour
|
||||
if (min == 1):
|
||||
string += _(u"%d minute, ") % min
|
||||
elif (min >= 2):
|
||||
string += _(u"%d minutes, ") % min
|
||||
if sec >= 0 and sec <= 2:
|
||||
string += _(u"%s second") % sec_string
|
||||
else:
|
||||
string += _(u"%s seconds") % sec_string
|
||||
return string
|
||||
|
||||
def find_urls_in_text(text):
|
||||
return [s.strip(bad_chars) for s in url_re.findall(text)]
|
||||
|
||||
def download_file(url, local_filename, window):
|
||||
r = requests.get(url, stream=True)
|
||||
window.change_status(_(u"Downloading {0}").format(local_filename,))
|
||||
total_length = r.headers.get("content-length")
|
||||
dl = 0
|
||||
total_length = int(total_length)
|
||||
with open(local_filename, 'wb') as f:
|
||||
for chunk in r.iter_content(chunk_size=64):
|
||||
if chunk: # filter out keep-alive new chunks
|
||||
dl += len(chunk)
|
||||
f.write(chunk)
|
||||
done = int(100 * dl / total_length)
|
||||
msg = _(u"Downloading {0} ({1}%)").format(os.path.basename(local_filename), done)
|
||||
window.change_status(msg)
|
||||
window.change_status(_(u"Ready"))
|
||||
return local_filename
|
||||
|
||||
def clean_text(text):
|
||||
""" Replaces all HTML entities and put the plain text equivalent if it's possible."""
|
||||
text = text.replace("<br>", "\n")
|
||||
text = text.replace("\\n", "\n")
|
||||
return text
|
||||
|
||||
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 = seconds_to_string(attachment["doc"]["preview"]["audio_msg"]["duration"])
|
||||
print attachment["doc"]["ext"]
|
||||
else:
|
||||
msg = u"{0}".format(attachment["doc"]["title"])
|
||||
tpe = _(u"{0} file").format(attachment["doc"]["ext"])
|
||||
return [tpe, msg]
|
||||
|
Reference in New Issue
Block a user