/
lastpage.py
executable file
·167 lines (143 loc) · 5.15 KB
/
lastpage.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
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
#!/usr/bin/env python
"""
A command line utility for working with LastPage (see http://lastpage.me).
Run with --help for brief help.
Released into the public domain on August 2nd, 2011 by Fluidinfo Inc.
"""
import re
import sys
import time
import optparse
from datetime import datetime
from subprocess import Popen
from fom.session import Fluid
defaultUser = None
defaultPassword = None
def deleteTag(fi, tag):
"""
Delete all instances of the tag C{tag} from Fluidinfo.
@param fi: a C{Fluid} instance.
@param tag: the tag to use.
"""
fdb.values.delete('has %s' % tag, [tag])
def getTag(fi, tag):
"""
Get all instances of the tag C{tag} from Fluidinfo.
@param fi: a C{Fluid} instance.
@param tag: the tag to use.
"""
urls = []
response = fdb.values.get('has %s' % tag, [u'fluiddb/about'])
for obj in response.value['results']['id'].values():
urls.append(obj['fluiddb/about']['value'])
return urls
def setTag(fi, tag, url):
"""
Put a tag C{tag} on the Fluidinfo object about C{url}.
@param fi: a C{Fluid} instance.
@param tag: the tag to use.
@param url: The url indicating which object to put the tag onto.
"""
now = int(time.mktime(datetime.utcnow().timetuple()))
fdb.about[url][tag].put(now)
def browse(url):
"""
Open the user's browser on C{url}.
@param url: The url to open.
"""
if sys.platform.startswith('linux'):
opener = '/usr/bin/xdg-open'
elif sys.platform == 'darwin':
opener = '/usr/bin/open'
try:
Popen([opener, url])
except NameError:
print >>sys.stderr, (
"Sorry, I don't know how to open pages on %s systems." %
sys.platform)
if __name__ == '__main__':
opts = optparse.OptionParser()
opts.add_option('--user',
action='store',
type='string',
default=defaultUser,
help='The Fluidinfo username (default %default).')
opts.add_option('--password',
action='store',
type='string',
default=defaultPassword,
help="The Fluidinfo user's password (default %default).")
opts.add_option('-c', '--custom',
action='store',
type='string',
default='',
help="A custom suffix for the lastpage URL.")
opts.add_option('-u', '--url',
action='store',
type='string',
default='',
dest='url',
help="The URL to store (default none).")
opts.add_option('-d', '--delete',
action='store_true',
default=False,
help='If True, delete the tag (default %default).')
opts.add_option('-s', '--show',
action='store_true',
default=False,
help='If True, print the current URL (default %default).')
opts.add_option('-o', '--open',
action='store_true',
default=False,
help='If True, open the current URL (default %default).')
args, opt = opts.parse_args()
# Check that any custom URL suffix is legal as a Fluidinfo tag name.
if args.custom:
if not re.match('^[\:\.\-\w/]+$', args.custom, re.UNICODE):
print >>sys.stderr, ('Custom suffixes can only contain letters, '
'digits, dot, hyphen, colon, and slash.')
sys.exit(1)
args.custom = '-' + args.custom.replace('/', '-')
# Get an instance of fom.session.Fluid and provide it with credentials,
# if we have been given a password to use.
fdb = Fluid()
if args.password:
if not args.user:
print >>sys.stderr, 'Please use --user USERNAME.'
sys.exit(1)
if not args.password:
print >>sys.stderr, 'Please use --password PASSWORD.'
sys.exit(1)
fdb.login(args.user, args.password)
# The Fluidinfo tag that will be acted on.
tag = '%s/lastpage%s' % (args.user, args.custom)
# Prepend 'http://' if it looks like we can be helpful.
if args.url and not args.url.startswith('http'):
args.url = 'http://' + args.url
if args.url:
# Set the tag on the Fluidinfo object about url.
deleteTag(fdb, tag)
setTag(fdb, tag, args.url)
elif args.delete:
# Delete all instances of the tag.
deleteTag(fdb, tag)
elif args.show:
# Print all instances of the tag.
for url in getTag(fdb, tag):
print url
elif args.open:
# Browse to the URL, if there's just one.
urls = getTag(fdb, tag)
nurls = len(urls)
if nurls == 0:
print 'Tag %s currently does not point to any URL.'
elif nurls == 1:
url = urls[0]
if url.startswith('http'):
browse(url)
else:
print 'Value %s does not look like a URL.' % url
else:
print 'Multiple values of %s are set:' % tag
for url in urls:
print url