~danielholm/twitpicgtk/0.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/python

# Created by Daniel Holm <admin@danielholm.se>, http://www.danielholm.se
# 100613, Gotenburg, Sweden
# Licensed under GPLv3. See the aboutdialog when runned.

# TwitpicGTK - easy way to post images to Twitpic, either using the standalone GUI, or using Nautilus Right-Click.

from ConfigParser import ConfigParser
import os
import base64
import pygtk
pygtk.require('2.0')
import gtk
import pynotify

version = "0.1"

license = "This program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or\n(at your option) any later version.\n\nThis program is distributed in the hope that it will be useful,\nbut WITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\nGNU General Public License for more details.\n\nYou should have received a copy of the GNU General Public License\nalong with this program.  If not, see <http://www.gnu.org/licenses/>."

home = os.getenv('HOME')
configfile = home + "/.config/twitpicgtk.conf"

print "Welcome to TwitPicGTK " + version

if not os.path.exists(configfile):
	print "Configfile did not exist. Creating..."
	config = open(configfile,'w')
	# RAW input for password and username
	name = raw_input("Twitter Username: ")	
	encryptname = base64.b64encode(name)
	print encryptname
	passw = raw_input("Twitter Password: ")	
	encryptpassw = base64.b64encode(passw)
	print passw

	# Writes to configfile
	sn = str(encryptname)
	sp = str(encryptpassw)
	config.write("[user]\n")
	config.write("USERNAME: " + sn + "\n")
	config.write("PASSWORD: " + sp)

	print "Configfile with encrypted name and pass created!"

else:
	print "Configfile exists. Decrypting info."

	
# Open configfile and get encrypted password and username. Save these in variables.
config = ConfigParser()
config.read([configfile])

USERNAME = config.get('user','USERNAME')
PASSWORD = config.get('user','PASSWORD')

# Check for new pygtk: this is new class in PyGtk 2.4
if gtk.pygtk_version < (2,3,90):
   print "PyGtk 2.3.90 or later required for this example"
   raise SystemExit

# Create the window with it's widgets (two buttons, one file-chooser and one entry-field.
class TwitPicGTK:

	# This is a callback function to answer the two buttons in the window: Ok and Close.
	def tweet(self, widget, entry):

		# Imports needed module: http://code.google.com/p/python-twitpic/
		import twitpic

		# Get tweet message
		tweet_text = self.entry.get_text()
		print "Tweeted: %s"  % tweet_text
		print "And the image: %s" % self.tweet_img

		# Decrypt username
		decryptname = base64.b64decode(USERNAME)
		#print "Decrypted username: " + decryptname

		# Decrypt password
		decryptpassw = base64.b64decode(PASSWORD)
		#print "Decrypted password: " + decryptpassw

		twit = twitpic.TwitPicAPI(decryptname, decryptpassw)
		twitpic_url = twit.upload(self.tweet_img, message=tweet_text, post_to_twitter=True)
		print twitpic_url

		# Create notification
		n = pynotify.Notification("TwitPicGTK", "Tweeted: \n" + tweet_text + "\n" + twitpic_url, self.tweet_img)
		n.show()

		# Close after sending.
		gtk.main_quit()

	def delete_event(self, widget, event, data=None):
		print "Delete event occurred."
		return False

	def destroy(self, widget, data=None):
		gtk.main_quit()


	# Filechooser for the image you want to submit to TwitPic.
	def filedialog(self, widget, data):
		dialog = gtk.FileChooserDialog("Open..",
                               None,
                               gtk.FILE_CHOOSER_ACTION_OPEN,
                               (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                gtk.STOCK_OPEN, gtk.RESPONSE_OK))
		dialog.set_default_response(gtk.RESPONSE_OK)

		filter = gtk.FileFilter()
		filter.set_name("Images")
		filter.add_mime_type("image/png")
		filter.add_mime_type("image/jpeg")
		filter.add_mime_type("image/gif")
		filter.add_pattern("*.png")
		filter.add_pattern("*.jpg")
		filter.add_pattern("*.gif")
		filter.add_pattern("*.tif")
		filter.add_pattern("*.xpm")
		dialog.add_filter(filter)

		response = dialog.run()
		if response == gtk.RESPONSE_OK:
			self.tweet_img = dialog.get_filename()
			print self.tweet_img, 'selected'
			#self.entry.connect("activate", self.tweet, self.entry)		
		elif response == gtk.RESPONSE_CANCEL:
			print 'Closed, no files selected'
		dialog.destroy()

	# About
	def about_dialog(self, widget, data):
		# Create AboutDialog object
		dialog = gtk.AboutDialog()

		# Add the application name to the dialog
		dialog.set_name('TwitPicGTK')

		# Add a application icon to the dialog
#		dialog.set_logo(gtk.gdk.pixbuf_new_from_file("none.png"))

		# Set the application version
		dialog.set_version('0.1')

		# Pass a list of authors.
		dialog.set_authors(['Daniel Holm - <d.holmen@gmail.com>',])

		# Add a short comment about the application.
		dialog.set_comments('Twitter image, easy from your desktop.')

		# Add license information.
		dialog.set_license(license)

		# Add a homepage to the dialog.
        	dialog.set_website('http://www.launchpad.net/twitpicgtk')

		# Show the dialog
		dialog.run()

		# The destroy method must be called otherwise the 'Close' button will
		# not work.
		dialog.destroy()

	# Create the main window.
	def __init__(self):
		# Create a new window.
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
		self.window.set_position(gtk.WIN_POS_CENTER)
	        self.window.set_size_request(200, 120)
		self.window.set_title("TwitPicGTK")
		#self.set_icon_from_file("web.png")

		# It's a good idea to do this for all windows.
		self.window.connect("destroy", lambda wid: gtk.main_quit())
		self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())

		# When clicked on the close button or titlerow call delete_event.
		self.window.connect("delete_event", self.delete_event)
		
		# Call destroy when clicked.
		self.window.connect("destroy", self.destroy)

		# Set the window border width.
		self.window.set_border_width(10)

		# Twitter message entry field.
		self.entry = gtk.Entry()
		self.entry.set_max_length(140)
		self.entry.connect("activate", self.tweet, self.entry)
		self.entry.set_text("Twitter Message")

		# Create the first button, Ok.
		self.send = gtk.Button(stock=gtk.STOCK_OK)
		self.send.connect("clicked", self.tweet, None)

		# Create the second button, Close.
		self.close = gtk.Button(stock=gtk.STOCK_CANCEL)
		self.close.connect("clicked", self.destroy, None)

		# Create the third button, Open.
		self.open = gtk.Button(stock=gtk.STOCK_OPEN)
		self.open.connect("clicked", self.filedialog, None)

		# Create the fourth button, About
		self.about = gtk.Button(stock=gtk.STOCK_ABOUT)
		self.about.connect("clicked", self.about_dialog, None)

		# Create fixed positions of the buttons.
		self.fixed = gtk.Fixed()

		self.fixed.put(self.open, 10, 10)
		self.fixed.put(self.entry, 10, 40)
		self.fixed.put(self.send, 80, 70)
		self.fixed.put(self.close, 120, 70)
		self.fixed.put(self.about, 10, 70)

		# Pack the buttons into the window.
		#self.window.add(self.send)
		#self.window.add(self.close)
		self.window.add(self.fixed)

		# Display the buttons and the window.
		self.open.show()
		self.send.show()
		self.close.show()
		self.about.show()
		self.window.show()
		self.fixed.show()
		self.entry.show()
		#self.show_all()

	def main(self):
		gtk.main()

if __name__ == "__main__":
	twitpic = TwitPicGTK()
	twitpic.main()