-
Notifications
You must be signed in to change notification settings - Fork 7
/
ikigrabimage
executable file
·62 lines (51 loc) · 1.6 KB
/
ikigrabimage
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
#!/usr/bin/python
#
# Grab an image to the iki-wiki, and save the source url into a
# markdown file.
#
# Usage: ikigrabimage [options] url
#
# Options:
# -h, --help show this help message and exit
# -o BASENAME, --output=BASENAME
# basename
#
from os.path import join, split, splitext
import os
from common.misc import runcmd
import sys
from urlparse import urlparse
from optparse import OptionParser
IMAGEDIR = join(os.environ["HOME"], "iki/images")
usage = "usage: %prog [options] url"
parser = OptionParser(usage)
parser.add_option("-o", "--output", dest="basename",
help="basename")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
url = args[0]
if not options.basename:
options.basename = splitext(split(urlparse(url).path)[1])[0]
ext = splitext(url)[1]
import re
if re.compile("\&").search(options.basename):
sys.stderr.write("Output name should not contain '&' character. Aborting...\n")
sys.exit(-1)
outputimage_file = "%s%s" % (options.basename, ext)
outputdesc_desc = "%s.mdwn" % (options.basename)
outputimage = join(IMAGEDIR, outputimage_file)
outputdesc = join(IMAGEDIR, outputdesc_desc)
cmd = "wget %s -O %s" % (url, outputimage)
sys.stderr.write("%s\n" % cmd)
runcmd(cmd)
desctext = """
[[!img %s]]
Image from
<%s>
""" % (outputimage_file, url)
sys.stderr.write("Writing to file %s\n" % outputdesc)
open(outputdesc, "wt").write(desctext)
cmd = "cd %s && /usr/bin/git add %s %s" % (IMAGEDIR, outputimage_file, outputdesc_desc)
sys.stderr.write("%s\n" % cmd)
os.system(cmd)