mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2025-01-18 16:40:41 -06:00
Image uploader has been rewritten. Images can have description when uploading
This commit is contained in:
parent
1ee629d731
commit
d768afc329
38
src/controller/attach.py
Normal file
38
src/controller/attach.py
Normal file
@ -0,0 +1,38 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import widgetUtils
|
||||
import logging
|
||||
from wxUI.dialogs import attach as gui
|
||||
log = logging.getLogger("controller.attach")
|
||||
|
||||
class attach(object):
|
||||
def __init__(self):
|
||||
self.attachments = list()
|
||||
self.dialog = gui.attachDialog()
|
||||
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()
|
||||
log.debug("Attachments controller started.")
|
||||
|
||||
def upload_image(self, *args, **kwargs):
|
||||
image, description = self.dialog.get_image()
|
||||
if image != None:
|
||||
imageInfo = {"type": "photo", "file": image, "description": description}
|
||||
log.debug("Image data to upload: %r" % (imageInfo,))
|
||||
self.attachments.append(imageInfo)
|
||||
info = [_(u"Photo"), description]
|
||||
self.dialog.attachments.insert_item(False, *info)
|
||||
self.dialog.remove.Enable(True)
|
||||
|
||||
def remove_attachment(self, *args, **kwargs):
|
||||
current_item = self.dialog.attachments.get_selected()
|
||||
log.debug("Removing item %d" % (current_item,))
|
||||
if current_item == -1: current_item = 0
|
||||
self.attachments.pop(current_item)
|
||||
self.dialog.attachments.remove_item(current_item)
|
||||
self.check_remove_status()
|
||||
log.debug("Removed")
|
||||
|
||||
def check_remove_status(self):
|
||||
if len(self.attachments) == 0 and self.dialog.attachments.get_count() == 0:
|
||||
self.dialog.remove.Enable(False)
|
@ -137,22 +137,24 @@ class bufferController(object):
|
||||
self.session.settings["mysc"]["twishort_enabled"] = tweet.message.long_tweet.GetValue()
|
||||
text = tweet.message.get_text()
|
||||
if len(text) > 140 and tweet.message.get("long_tweet") == True:
|
||||
if tweet.image == None:
|
||||
if not hasattr(tweet, "attachments"):
|
||||
text = twishort.create_tweet(self.session.settings["twitter"]["user_key"], self.session.settings["twitter"]["user_secret"], text)
|
||||
else:
|
||||
text = twishort.create_tweet(self.session.settings["twitter"]["user_key"], self.session.settings["twitter"]["user_secret"], text, 1)
|
||||
if tweet.image == None:
|
||||
if not hasattr(tweet, "attachments"):
|
||||
call_threaded(self.session.api_call, call_name="update_status", status=text)
|
||||
else:
|
||||
call_threaded(self.post_with_media, text=text, image=tweet.image, description="test")
|
||||
call_threaded(self.post_with_media, text=text, attachments=tweet.attachments)
|
||||
if hasattr(tweet.message, "destroy"): tweet.message.destroy()
|
||||
|
||||
def post_with_media(self, text="", image=None, description=None):
|
||||
if image != None:
|
||||
img = self.session.twitter.twitter.upload_media(media=image)
|
||||
if description != None:
|
||||
self.session.twitter.twitter.set_description(media_id=img["media_id"], alt_text=dict(text=description))
|
||||
self.session.api_call(call_name="update_status", status=text, media_ids=img["media_id"])
|
||||
def post_with_media(self, text, attachments):
|
||||
media_ids = []
|
||||
for i in attachments:
|
||||
photo = open(i["file"], "rb")
|
||||
img = self.session.twitter.twitter.upload_media(media=photo)
|
||||
self.session.twitter.twitter.set_description(media_id=img["media_id"], alt_text=dict(text=i["description"]))
|
||||
media_ids.append(img["media_id"])
|
||||
self.session.twitter.twitter.update_status(status=text, media_ids=media_ids)
|
||||
|
||||
def save_positions(self):
|
||||
try:
|
||||
|
@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import platform
|
||||
import attach
|
||||
system = platform.system()
|
||||
import widgetUtils
|
||||
import output
|
||||
@ -32,6 +33,7 @@ class basicTweet(object):
|
||||
widgetUtils.connect_event(self.message.shortenButton, widgetUtils.BUTTON_PRESSED, self.shorten)
|
||||
widgetUtils.connect_event(self.message.unshortenButton, widgetUtils.BUTTON_PRESSED, self.unshorten)
|
||||
widgetUtils.connect_event(self.message.translateButton, widgetUtils.BUTTON_PRESSED, self.translate)
|
||||
self.attachments = []
|
||||
|
||||
def translate(self, event=None):
|
||||
dlg = translator.gui.translateDialog()
|
||||
@ -125,16 +127,9 @@ class tweet(basicTweet):
|
||||
except AttributeError: pass
|
||||
|
||||
def upload_image(self, *args, **kwargs):
|
||||
if self.message.get("upload_image") == _(u"Discard image"):
|
||||
del self.image
|
||||
self.image = None
|
||||
output.speak(_(u"Discarded"))
|
||||
self.message.set("upload_image", _(u"Upload a picture"))
|
||||
else:
|
||||
self.image = self.message.get_image()
|
||||
if self.image != None:
|
||||
self.message.set("upload_image", _(u"Discard image"))
|
||||
self.message.text_focus()
|
||||
a = attach.attach()
|
||||
if len(a.attachments) != 0:
|
||||
self.attachments = a.attachments
|
||||
|
||||
def autocomplete_users(self, *args, **kwargs):
|
||||
c = autocompletionUsers.completion.autocompletionUsers(self.message, self.session.session_id)
|
||||
@ -184,7 +179,7 @@ class viewTweet(basicTweet):
|
||||
else:
|
||||
text = text + "rt @%s: %s\n" % (tweetList[i]["retweeted_status"]["user"]["screen_name"], tweetList[i][value])
|
||||
else:
|
||||
text = text + "@%s: %s\n" % (tweetList[i]["user"]["screen_name"], tweetList[i][value])
|
||||
text = text + " @%s: %s\n" % (tweetList[i]["user"]["screen_name"], tweetList[i][value])
|
||||
# tweets with extended_entities could include image descriptions.
|
||||
if tweetList[i].has_key("extended_entities") and tweetList[i]["extended_entities"].has_key("media"):
|
||||
for z in tweetList[i]["extended_entities"]["media"]:
|
||||
@ -234,5 +229,5 @@ class viewTweet(basicTweet):
|
||||
urls = utils.find_urls_in_text(text)
|
||||
for i in urls:
|
||||
if "https://twitter.com/" in i:
|
||||
text = text.replace(i, "")
|
||||
text = text.replace(i, "\n")
|
||||
return text
|
||||
|
47
src/wxUI/dialogs/attach.py
Normal file
47
src/wxUI/dialogs/attach.py
Normal file
@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" Attach dialog. Taken from socializer: https://github.com/manuelcortez/socializer"""
|
||||
import wx
|
||||
import widgetUtils
|
||||
from multiplatform_widgets import widgets
|
||||
|
||||
class attachDialog(widgetUtils.BaseDialog):
|
||||
def __init__(self):
|
||||
super(attachDialog, self).__init__(None, title=_(u"Add an attachment"))
|
||||
panel = wx.Panel(self)
|
||||
sizer = wx.BoxSizer(wx.VERTICAL)
|
||||
lbl1 = wx.StaticText(panel, wx.NewId(), _(u"Attachments"))
|
||||
self.attachments = widgets.list(panel, _(u"Type"), _(u"Title"), style=wx.LC_REPORT)
|
||||
box = wx.BoxSizer(wx.HORIZONTAL)
|
||||
box.Add(lbl1, 0, wx.ALL, 5)
|
||||
box.Add(self.attachments.list, 0, wx.ALL, 5)
|
||||
sizer.Add(box, 0, wx.ALL, 5)
|
||||
static = wx.StaticBox(panel, label=_(u"Add attachments"))
|
||||
self.photo = wx.Button(panel, wx.NewId(), _(u"&Photo"))
|
||||
self.remove = wx.Button(panel, wx.NewId(), _(u"Remove attachment"))
|
||||
self.remove.Enable(False)
|
||||
btnsizer = wx.StaticBoxSizer(static, wx.HORIZONTAL)
|
||||
btnsizer.Add(self.photo, 0, wx.ALL, 5)
|
||||
sizer.Add(btnsizer, 0, wx.ALL, 5)
|
||||
ok = wx.Button(panel, wx.ID_OK)
|
||||
ok.SetDefault()
|
||||
cancelBtn = wx.Button(panel, wx.ID_CANCEL)
|
||||
btnSizer = wx.BoxSizer()
|
||||
btnSizer.Add(ok, 0, wx.ALL, 5)
|
||||
btnSizer.Add(cancelBtn, 0, wx.ALL, 5)
|
||||
sizer.Add(btnSizer, 0, wx.ALL, 5)
|
||||
panel.SetSizer(sizer)
|
||||
self.SetClientSize(sizer.CalcMin())
|
||||
|
||||
def get_image(self):
|
||||
openFileDialog = wx.FileDialog(self, _(u"Select the picture to be uploaded"), "", "", _("Image files (*.png, *.jpg, *.gif)|*.png; *.jpg; *.gif"), wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
|
||||
if openFileDialog.ShowModal() == wx.ID_CANCEL:
|
||||
return (None, None)
|
||||
dsc = self.ask_description()
|
||||
return (openFileDialog.GetPath(), dsc)
|
||||
|
||||
def ask_description(self):
|
||||
dlg = wx.TextEntryDialog(self, _(u"please provide a description"), _(u"Description"), defaultValue="")
|
||||
dlg.ShowModal()
|
||||
result = dlg.GetValue()
|
||||
dlg.Destroy()
|
||||
return result
|
Loading…
x
Reference in New Issue
Block a user