-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsnapflickr
executable file
·151 lines (126 loc) · 3.74 KB
/
snapflickr
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
#!/usr/bin/python
import Pashua
from flickrapi import FlickrAPI
import Image
import sys, os
import subprocess
import re
# Local parameters
extension = "png"
localdir = os.environ['HOME'] + "/Pictures/Screenshots"
tempfile = "temp.png"
bgcolor = (61, 101, 156)
bigshadow = (25, 5, 25, 35)
smallshadow = (0, 0, 0, 0)
border = 16
# Flickr parameters
fuser = 'your Flickr username'
key = 'get your key from Flickr'
secret = 'get your secret from Flickr'
screenshotsID = 'the ID of the Flickr set'
# Dialog box configuration
conf = '''
# Window properties
*.title = Snapshot Flickr
# File name text field properties
fn.type = textfield
fn.default = snap
fn.width = 264
fn.x = 94
fn.y = 130
fnl.type = text
fnl.default = File name:
fnl.x = 20
fnl.y = 132
# Description text field properties
desc.type = textbox
desc.width = 264
desc.height = 72
desc.x = 94
desc.y = 40
descl.type = text
descl.default = Description:
descl.x = 10
descl.y = 95
# Size radiobutton properties
radio.type = radiobutton
radio.label = Size of <img>:
radio.option = Original
radio.option = Large
radio.option = Medium 640
radio.option = Medium 500
radio.default = Medium 640
radio.x = 380
radio.y = 50
# Local files checkbox properties
lf.type = checkbox
lf.label = Local file only
lf.x = 20
lf.y = 5
# Border checkbox properties
bd.type = checkbox
bd.label = Background border
bd.x = 140
bd.y = 5
bd.default = 1
# Default button
db.type = defaultbutton
db.label = Save
# Cancel button
cb.type = cancelbutton
'''
# Go to the localdir.
os.chdir(localdir)
# Capture a portion of the screen and save it to a temporary file.
subprocess.call(["screencapture", "-ioW", "-t", extension, tempfile])
# Exit if the user canceled the screencapture.
if not os.access(tempfile, os.F_OK):
sys.exit()
# Open the dialog box and get the input.
dialog = Pashua.run(conf)
if dialog['cb'] == '1':
os.remove(tempfile)
sys.exit()
# Rename the temporary file.
fn = '%s.%s' % (dialog['fn'], extension)
subprocess.call(["mv", tempfile, fn])
# Add a desktop background border if asked for.
snap = Image.open(fn)
if dialog['bd'] == '1':
# Make a solid-colored background bigger than the screenshot.
snapsize = tuple([ x + 2*border for x in snap.size ])
bg = Image.new('RGB', snapsize, bgcolor)
bg.paste(snap, (border, border))
bg.save(fn)
# Put the image on the clipboard.
impbcopy = os.environ['HOME'] + '/Dropbox/bin/impbcopy'
subprocess.call([impbcopy, fn])
# Unless this is just for the clipboard...
if dialog['lf'] != '1':
# Upload the file.
flickr = FlickrAPI(api_key=key, secret=secret)
response = flickr.upload(filename=fn, title=dialog['fn'],\
description=dialog['desc'], is_public=1,\
format='etree')
# Get the photo info from Flickr.
photoID = response.find('photoid').text
photoURL = 'http://www.flickr.com/photos/%s/%s/' % (fuser, photoID)
# Add to the screenshots set.
flickr.photosets_addPhoto(photo_id = photoID, photoset_id = screenshotsID)
# Open the photo page in the default browser.
subprocess.call(['open', photoURL])
# Put an img element on the clipboard for the selected size of the image.
size = dialog['radio']
if size == 'Medium 500': size = 'Medium'
flickr = FlickrAPI(api_key=key, secret=secret)
etree = flickr.photos_getSizes(photo_id = photoID, format = 'etree')
for i in etree[0]:
if i.attrib['label'] == size:
tag = '<img class="ss" src="%s" alt="%s" title="%s" />' %\
(i.attrib['source'], dialog['fn'], dialog['fn'])
subprocess.Popen('pbcopy', stdin=subprocess.PIPE).communicate(tag)
sys.exit()
# If the selected size wasn't available.
tag = '<img src="%s" alt="%s" title="%s" />' %\
('size_not_found', dialog['fn'], dialog['fn'])
subprocess.Popen('pbcopy', stdin=subprocess.PIPE).communicate(tag)