-
Notifications
You must be signed in to change notification settings - Fork 7
/
currentflickr.py
executable file
·107 lines (81 loc) · 3.04 KB
/
currentflickr.py
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
#!/usr/bin/python
from flickrapi import FlickrAPI, shorturl
from applescript import asrun
import re
def currentFlickrID():
'''Return the ID for the Flickr image currently showing in the browser.
The function works through Apple Events and supports only the Safari
browser. It will generate an IndexError if the frontmost tab of the
browser isn't showing a Flickr image.'''
# The regex for extracting user and photo info.
infoRE = r'flickr\.com/photos/(.*)/(\d+)/?'
# Get the URL of the current page in either Safari.
thisURL = asrun('tell application "Safari" to get the URL of the front document')
# Extract the user and photo info from the URL.
info = re.findall(infoRE, thisURL)
return info[0][1]
def currentFlickrTitle():
'''Return the title of the Flickr image currently showing in the browser.
The function works through Apple Events and supports only the Safari
browser.'''
# Flickr parameters
fuser = 'Flickr username'
key = 'Get key from Flickr'
secret = 'Get secret from Flickr'
# Get the image ID.
try:
imageID = currentFlickrID()
except IndexError:
return "Not a Flickr image"
# Establish the connection with Flickr.
flickr = FlickrAPI(api_key=key, secret=secret)
# Get the title.
etree = flickr.photos_getInfo(photo_id = imageID, format = 'etree')
for i in etree[0]:
if i.tag == 'title':
return i.text
break
# If the size wasn't found.
return "Title not found"
def currentFlickrURL(kind):
'''Return a URL for the Flickr image currently showing in the browser.
The string parameter "kind" can be either "Short" or one of the
standard Flickr image sizes: "Original", "Large", "Medium 800",
"Medium 640", "Medium", "Small 320", "Small", "Thumbnail", "Large
Square", or "Square". If it's Short, the function will return a
flic.kr URL for the image page. If it's one of the others, the
function will return the URL of the image of that size, if
available.
The function works through Apple Events and supports only the Safari
browser.'''
# Flickr parameters
fuser = 'Flickr username'
key = 'Get key from Flickr'
secret = 'Get secret from Flickr'
# Make sure we're asking for a legitimate kind.
kind = ' '.join([x.capitalize() for x in kind.split()])
kinds = ["Short", "Original", "Large", "Medium 800", "Medium 640",
"Medium", "Small 320", "Small", "Thumbnail",
"Large Square", "Square"]
if kind not in kinds:
return "Not a legitimate kind of URL"
# Get the image ID.
try:
imageID = currentFlickrID()
except IndexError:
return "Not a Flickr image"
# Establish the connection with Flickr.
flickr = FlickrAPI(api_key=key, secret=secret)
# Get the URL.
if kind == "Short":
return shorturl.url(photo_id = imageID)
else:
etree = flickr.photos_getSizes(photo_id = imageID, format = 'etree')
for i in etree[0]:
if i.attrib['label'] == kind:
return i.attrib['source']
break
# If the size wasn't found.
return "Size not found"
if __name__ == '__main__':
print currentFlickrURL('Short')