2018-09-06 10:47:10 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-11-05 09:11:20 -06:00
|
|
|
""" A profile viewer and editor for VK user objects."""
|
2019-01-01 19:42:53 -06:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
from future import standard_library
|
|
|
|
standard_library.install_aliases()
|
|
|
|
import io
|
2018-11-05 09:11:20 -06:00
|
|
|
import webbrowser
|
|
|
|
import logging
|
2018-09-06 10:47:10 -05:00
|
|
|
import arrow
|
|
|
|
import requests
|
|
|
|
import languageHandler
|
|
|
|
import widgetUtils
|
|
|
|
import output
|
|
|
|
import wx
|
|
|
|
from wxUI.dialogs import urlList, profiles
|
2018-12-06 17:34:48 -06:00
|
|
|
from sessionmanager import utils
|
2018-09-06 10:47:10 -05:00
|
|
|
|
2018-11-05 09:11:20 -06:00
|
|
|
log = logging.getLogger("controller.profiles")
|
2018-09-06 10:47:10 -05:00
|
|
|
|
|
|
|
class userProfile(object):
|
2018-11-05 09:11:20 -06:00
|
|
|
""" Main controller to view an user profile. This controller will retrieve needed data from the VK website and display it appropiately."""
|
2018-09-06 10:47:10 -05:00
|
|
|
|
|
|
|
def __init__(self, session, user_id):
|
2018-11-05 09:11:20 -06:00
|
|
|
""" Default constructor:
|
|
|
|
@session vk.session: The main session object, capable of calling VK methods.
|
|
|
|
@user_id integer: User ID to retrieve information of.
|
|
|
|
At the current time, only users (and not communities) are supported.
|
|
|
|
"""
|
|
|
|
# self.person will hold a reference to the user object when retrieved from VK.
|
2018-09-06 10:47:10 -05:00
|
|
|
self.person = None
|
|
|
|
self.session = session
|
|
|
|
self.user_id = user_id
|
2019-01-01 19:42:53 -06:00
|
|
|
self.dialog = profiles.userProfile(title=_("Profile"))
|
2018-09-06 10:47:10 -05:00
|
|
|
self.dialog.create_controls("main_info")
|
|
|
|
self.dialog.realice()
|
|
|
|
self.get_basic_information()
|
|
|
|
if self.person != None:
|
|
|
|
self.dialog.get_response()
|
|
|
|
|
|
|
|
def get_basic_information(self):
|
|
|
|
""" Gets and inserts basic user information.
|
|
|
|
See https://vk.com/dev/users.get"""
|
2018-11-05 09:11:20 -06:00
|
|
|
# List of fields (information) to retrieve. For a list of fields available for user objects,
|
|
|
|
# see https://vk.com/dev/fields
|
2018-09-06 10:47:10 -05:00
|
|
|
fields = "first_name, last_name, bdate, city, country, home_town, photo_200_orig, online, site, status, last_seen, occupation, relation, relatives, personal, connections, activities, interests, music, movies, tv, books, games, about, quotes, can_write_private_message"
|
2018-11-05 09:11:20 -06:00
|
|
|
# ToDo: this method supports multiple user IDS, I'm not sure if this may be of any help for profile viewer.
|
2018-09-06 10:47:10 -05:00
|
|
|
person = self.session.vk.client.users.get(user_ids=self.user_id, fields=fields)
|
2018-11-05 09:11:20 -06:00
|
|
|
# If VK does not return anything it is very likely we have found a community.
|
2018-09-06 10:47:10 -05:00
|
|
|
if len(person) == 0:
|
2019-01-01 19:42:53 -06:00
|
|
|
return output.speak(_("Information for groups is not supported, yet."))
|
2018-09-06 10:47:10 -05:00
|
|
|
person = person[0]
|
2018-11-05 09:11:20 -06:00
|
|
|
# toDo: remove this print when I will be done with creation of profile viewer logic.
|
|
|
|
print(person)
|
|
|
|
# From this part we will format data from VK so users will see it in the GUI control.
|
|
|
|
# Format full name.
|
2019-01-01 19:42:53 -06:00
|
|
|
n = "{0} {1}".format(person["first_name"], person["last_name"])
|
2018-11-05 09:11:20 -06:00
|
|
|
# Format birthdate.
|
2019-01-01 19:42:53 -06:00
|
|
|
if "bdate" in person and person["bdate"] != "":
|
2018-09-06 10:47:10 -05:00
|
|
|
self.dialog.main_info.enable("bdate")
|
2018-11-05 09:11:20 -06:00
|
|
|
# VK can display dd.mm or dd.mm.yyyy birthdates. So let's compare the string lenght to handle both cases accordingly.
|
|
|
|
if len(person["bdate"]) <= 5: # dd.mm
|
2018-10-14 08:13:12 -05:00
|
|
|
d = arrow.get(person["bdate"], "D.M")
|
2019-01-01 19:42:53 -06:00
|
|
|
self.dialog.main_info.set("bdate", d.format(_("MMMM D"), locale=languageHandler.curLang[:2]))
|
2018-11-05 09:11:20 -06:00
|
|
|
else: # mm.dd.yyyy
|
2018-09-06 10:47:10 -05:00
|
|
|
d = arrow.get(person["bdate"], "D.M.YYYY")
|
2019-01-01 19:42:53 -06:00
|
|
|
self.dialog.main_info.set("bdate", d.format(_("MMMM D, YYYY"), locale=languageHandler.curLang[:2]))
|
2018-11-05 09:11:20 -06:00
|
|
|
# Format current city and home town
|
2018-09-06 10:47:10 -05:00
|
|
|
city = ""
|
2019-01-01 19:42:53 -06:00
|
|
|
if "home_town" in person and person["home_town"] != "":
|
2018-09-06 10:47:10 -05:00
|
|
|
home_town = person["home_town"]
|
|
|
|
self.dialog.main_info.enable("home_town")
|
|
|
|
self.dialog.main_info.set("home_town", home_town)
|
2019-01-01 19:42:53 -06:00
|
|
|
if "city" in person and len(person["city"]) > 0:
|
2018-09-06 10:47:10 -05:00
|
|
|
city = person["city"]["title"]
|
2019-01-01 19:42:53 -06:00
|
|
|
if "country" in person and person["country"] != "":
|
2018-09-06 10:47:10 -05:00
|
|
|
if city != "":
|
2019-01-01 19:42:53 -06:00
|
|
|
city = city+", {0}".format(person["country"]["title"])
|
2018-09-06 10:47:10 -05:00
|
|
|
else:
|
|
|
|
city = person["country"]["title"]
|
|
|
|
self.dialog.main_info.enable("city")
|
|
|
|
self.dialog.main_info.set("city", city)
|
|
|
|
self.dialog.main_info.set("name", n)
|
2019-01-01 19:42:53 -06:00
|
|
|
self.dialog.SetTitle(_("{name}'s profile").format(name=n,))
|
2018-11-05 09:11:20 -06:00
|
|
|
# Format website (or websites, if there are multiple of them).
|
2019-01-01 19:42:53 -06:00
|
|
|
if "site" in person and person["site"] != "":
|
2018-09-06 10:47:10 -05:00
|
|
|
self.dialog.main_info.enable("website")
|
|
|
|
self.dialog.main_info.set("website", person["site"])
|
|
|
|
self.dialog.main_info.enable("go_site")
|
|
|
|
widgetUtils.connect_event(self.dialog.main_info.go_site, widgetUtils.BUTTON_PRESSED, self.visit_website)
|
2018-11-05 09:11:20 -06:00
|
|
|
# Format status message.
|
2019-01-01 19:42:53 -06:00
|
|
|
if "status" in person and person["status"] != "":
|
2018-09-06 10:47:10 -05:00
|
|
|
self.dialog.main_info.enable("status")
|
|
|
|
self.dialog.main_info.set("status", person["status"])
|
2018-11-05 09:11:20 -06:00
|
|
|
# Format occupation.
|
|
|
|
# toDo: Research in this field is needed. Sometimes it returns university information even if users have active work places.
|
2019-01-01 19:42:53 -06:00
|
|
|
if "occupation" in person and person["occupation"] != None:
|
|
|
|
if person["occupation"]["type"] == "work": c1 = _("Work ")
|
|
|
|
elif person["occupation"]["type"] == "school": c1 = _("Student ")
|
|
|
|
elif person["occupation"]["type"] == "university": c1 = _("Student ")
|
|
|
|
if "name" in person["occupation"] and person["occupation"]["name"] != "":
|
|
|
|
c2 = _("In {0}").format(person["occupation"]["name"],)
|
2018-09-06 10:47:10 -05:00
|
|
|
else:
|
|
|
|
c2 = ""
|
|
|
|
self.dialog.main_info.enable("occupation")
|
|
|
|
self.dialog.main_info.set("occupation", c1+c2)
|
2018-11-05 09:11:20 -06:00
|
|
|
# format relationship status.
|
|
|
|
# ToDo: When dating someone, the button associated to the information should point to the profile of the user.
|
2019-01-01 19:42:53 -06:00
|
|
|
if "relation" in person and person["relation"] != 0:
|
2018-09-06 10:47:10 -05:00
|
|
|
if person["relation"] == 1:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("Single")
|
2018-09-06 10:47:10 -05:00
|
|
|
elif person["relation"] == 2:
|
2019-01-01 19:42:53 -06:00
|
|
|
if "relation_partner" in person:
|
|
|
|
r = _("Dating with {0} {1}").format(person["relation_partner"]["first_name"], person["relation_partner"]["last_name"])
|
2018-09-06 10:47:10 -05:00
|
|
|
else:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("Dating")
|
2018-09-06 10:47:10 -05:00
|
|
|
elif person["relation"] == 3:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("Engaged with {0} {1}").format(person["relation_partner"]["first_name"], person["relation_partner"]["last_name"])
|
2018-09-06 10:47:10 -05:00
|
|
|
elif person["relation"] == 4:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("Married with {0} {1}").format(person["relation_partner"]["first_name"], person["relation_partner"]["last_name"])
|
2018-09-06 10:47:10 -05:00
|
|
|
elif person["relation"] == 5:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("It's complicated")
|
2018-09-06 10:47:10 -05:00
|
|
|
elif person["relation"] == 6:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("Actively searching")
|
2018-09-06 10:47:10 -05:00
|
|
|
elif person["relation"] == 7:
|
2019-01-01 19:42:53 -06:00
|
|
|
r = _("In love")
|
2018-09-06 10:47:10 -05:00
|
|
|
self.dialog.main_info.enable("relation")
|
2019-01-01 19:42:53 -06:00
|
|
|
self.dialog.main_info.relation.SetLabel(_("Relationship: ")+r)
|
2018-11-05 09:11:20 -06:00
|
|
|
# format last seen.
|
2019-01-01 19:42:53 -06:00
|
|
|
if "last_seen" in person and person["last_seen"] != False:
|
2018-09-06 10:47:10 -05:00
|
|
|
original_date = arrow.get(person["last_seen"]["time"])
|
|
|
|
# Translators: This is the date of last seen
|
2019-01-01 19:42:53 -06:00
|
|
|
last_seen = _("{0}").format(original_date.humanize(locale=languageHandler.curLang[:2]),)
|
2018-09-06 10:47:10 -05:00
|
|
|
self.dialog.main_info.enable("last_seen")
|
|
|
|
self.dialog.main_info.set("last_seen", last_seen)
|
|
|
|
self.person = person
|
2018-11-05 09:11:20 -06:00
|
|
|
# Adds photo to the dialog.
|
|
|
|
# ToDo: Need to ask if this has a visible effect in the dialog.
|
2019-01-01 19:42:53 -06:00
|
|
|
if "photo_200_orig" in person:
|
2018-09-06 10:47:10 -05:00
|
|
|
img = requests.get(person["photo_200_orig"])
|
2019-01-01 19:42:53 -06:00
|
|
|
image = wx.Image(stream=io.StringIO(requests.get(person["photo_200_orig"]).content))
|
2018-09-06 10:47:10 -05:00
|
|
|
try:
|
|
|
|
self.dialog.image.SetBitmap(wx.Bitmap(image))
|
|
|
|
except ValueError:
|
|
|
|
return
|
|
|
|
self.dialog.panel.Layout()
|
|
|
|
|
|
|
|
def visit_website(self, *args, **kwargs):
|
2018-09-06 12:34:08 -05:00
|
|
|
""" Allows to visit an user's website. """
|
|
|
|
text = self.person["site"]
|
2018-11-05 09:11:20 -06:00
|
|
|
# Let's search for URLS with a regexp, as there are users with multiple websites in their profiles.
|
2018-09-06 12:34:08 -05:00
|
|
|
urls = utils.find_urls_in_text(text)
|
|
|
|
if len(urls) == 0:
|
2019-01-01 19:42:53 -06:00
|
|
|
output.speak(_("No URL addresses were detected."))
|
2018-09-06 12:34:08 -05:00
|
|
|
return
|
|
|
|
elif len(urls) == 1:
|
|
|
|
selected_url = urls[0]
|
|
|
|
else:
|
|
|
|
dialog = urlList.urlList()
|
|
|
|
dialog.populate_list(urls)
|
|
|
|
if dialog.get_response() != widgetUtils.OK:
|
|
|
|
return
|
|
|
|
selected_url = urls[dialog.get_item()]
|
2019-01-01 19:42:53 -06:00
|
|
|
output.speak(_("Opening URL..."))
|
2018-09-06 12:34:08 -05:00
|
|
|
webbrowser.open_new_tab(selected_url)
|