2016-04-11 11:48:35 -05:00
# -*- coding: utf-8 -*-
2018-11-05 09:37:40 -06:00
""" Attachment upload methods for different kind of posts in VK. This should become the framework for posting attachment files to the social network. """
2016-04-11 11:48:35 -05:00
import os
2016-05-10 20:23:48 -05:00
import logging
2018-11-05 09:37:40 -06:00
import widgetUtils
2016-04-11 11:48:35 -05:00
from wxUI . dialogs import attach as gui
2017-03-13 02:16:34 -06:00
from wxUI . dialogs import selector
2016-05-10 20:23:48 -05:00
log = logging . getLogger ( " controller.attach " )
2016-04-11 11:48:35 -05:00
2018-11-05 09:37:40 -06:00
class attachFromLocal ( object ) :
""" Controller used in some sections of the application, mainly for uploading photos from the local computer to VK.
This controller will not upload the contents by itself , but will generate the data structures for being send over the VK API .
At the current time , only photo uploading is supported . """
2017-03-13 02:16:34 -06:00
def __init__ ( self , voice_messages = False ) :
2018-11-05 09:37:40 -06:00
""" Constructor.
@voice_messages bool : If True , will add a button for sending voice messages . Functionality for this button has not been added yet .
"""
# Self.attachments will hold a reference to all attachments added to the dialog.
2016-04-11 11:48:35 -05:00
self . attachments = list ( )
2018-11-05 09:37:40 -06:00
# Type is just a reference, as there will be local and online based attachments.
2017-03-13 02:16:34 -06:00
self . type = " local "
self . dialog = gui . attachDialog ( voice_messages )
2016-04-11 11:48:35 -05:00
widgetUtils . connect_event ( self . dialog . photo , widgetUtils . BUTTON_PRESSED , self . upload_image )
2016-04-25 05:26:55 -05:00
widgetUtils . connect_event ( self . dialog . remove , widgetUtils . BUTTON_PRESSED , self . remove_attachment )
2016-04-11 11:48:35 -05:00
self . dialog . get_response ( )
2016-05-10 20:23:48 -05:00
log . debug ( " Attachments controller started. " )
2016-04-11 11:48:35 -05:00
def upload_image ( self , * args , * * kwargs ) :
2018-11-05 09:37:40 -06:00
""" allows uploading an image from the computer. In theory
"""
# toDo: Basically description is not working here. Research on how to add description to uploaded images needs attention.
# For now, we fake a description, as it is not being send anymore.
2016-04-11 11:48:35 -05:00
image , description = self . dialog . get_image ( )
if image != None :
2018-11-05 09:37:40 -06:00
# Define data structure for this attachment, as will be required by VK API later.
2016-05-10 20:23:48 -05:00
imageInfo = { " type " : " photo " , " file " : image , " description " : os . path . basename ( image ) }
log . debug ( " Image data to upload: %r " % ( imageInfo , ) )
self . attachments . append ( imageInfo )
2016-06-29 10:56:41 -05:00
# Translators: This is the text displayed in the attachments dialog, when the user adds a photo.
2016-04-11 11:48:35 -05:00
info = [ _ ( u " Photo " ) , os . path . basename ( image ) ]
2016-04-25 05:26:55 -05:00
self . dialog . attachments . insert_item ( False , * info )
self . dialog . remove . Enable ( True )
def remove_attachment ( self , * args , * * kwargs ) :
2018-11-05 09:37:40 -06:00
""" Remove the currently focused item from the attachments list. """
2016-04-25 05:26:55 -05:00
current_item = self . dialog . attachments . get_selected ( )
2016-05-10 20:23:48 -05:00
log . debug ( " Removing item %d " % ( current_item , ) )
2016-04-25 05:26:55 -05:00
if current_item == - 1 : current_item = 0
self . attachments . pop ( current_item )
self . dialog . attachments . remove_item ( current_item )
self . check_remove_status ( )
2016-05-10 20:23:48 -05:00
log . debug ( " Removed " )
2016-04-25 05:26:55 -05:00
def check_remove_status ( self ) :
2018-11-05 09:37:40 -06:00
""" Checks whether the remove button should remain enabled. """
2016-04-25 05:26:55 -05:00
if len ( self . attachments ) == 0 and self . dialog . attachments . get_count ( ) == 0 :
self . dialog . remove . Enable ( False )
2017-03-13 02:16:34 -06:00
2018-11-05 09:37:40 -06:00
class attachFromOnline ( object ) :
""" this was the previously working audio attachment uploader. As VK has deprecated their Audio API for third party clients, this class will not work.
However , I have decided to keep this here so in future it may be modified to attach different kind of online based attachments , such as posted photos , videos , etc .
"""
2017-03-13 02:16:34 -06:00
def __init__ ( self , session ) :
2018-11-05 09:37:40 -06:00
""" Default constructor.
@session vk . session : An VK session , capable of calling methods from the VK API .
"""
2017-03-13 02:16:34 -06:00
self . session = session
2018-11-05 09:37:40 -06:00
# Self.attachments will hold a reference to all attachments added to the dialog.
2017-03-13 02:16:34 -06:00
self . attachments = list ( )
2018-11-05 09:37:40 -06:00
# Define type as online.
2017-03-13 02:16:34 -06:00
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 ) :
2018-11-05 09:37:40 -06:00
""" Allow adding an audio directly from the user ' s audio library. """
2017-03-13 02:16:34 -06:00
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 :
list_of_audios [ i ] [ " type " ] = " audio "
self . attachments . append ( list_of_audios [ i ] )
def parse_attachments ( self ) :
2018-11-05 09:37:40 -06:00
""" Retrieve all attachments and convert them to data structures required by VK API. """
2017-03-13 02:16:34 -06:00
result = " "
for i in self . attachments :
2018-11-05 09:37:40 -06:00
# Define data structures required by VK API.
2017-03-13 02:16:34 -06:00
preresult = " {0} {1} _ {2} " . format ( i [ " type " ] , i [ " owner_id " ] , i [ " id " ] )
result = preresult + " , "
return result