-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintpal.py
executable file
·182 lines (140 loc) · 4.92 KB
/
printpal.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
'''
PrintPal
---------
-> Automated Site PDF Creation
PrintPal is an html to pdf CLI utility written in Python. Its purpose is to
easily take a domain with one or more urls and bundle each html page into one
pdf document utilizing wkhtmltopdf <http://code.google.com/p/wkhtmltopdf/>.
**********************************************************************
Usage
------
//create a simple page pdf of a site
$ python printpal.py -D example.com/index.html
//create a multi-page pdf of a site
$ python printpal.py -D example.com -I urls.txt
//create a pdf with a specified output
$python printpal.py -D example.com -O ~/Desktop/Sample.pdf
**********************************************************************
Available Commands
------------------
-D <--domain>: Domain of website to print.
-I <--uri-file>: file of uri segments to use with domain ( newline delimited ).
-O <--output>: Path where the file should be saved to.
**********************************************************************
@Author: Chris Morris http://github.com/morriswchris
@Date: July 24 2013
@version: 1.0.0
Released under the MIT license
'''
#imports
import os
import sys
import getopt
import csv
#
# Our Main Program
#
def main(argv):
#maping table for commands
map_commands = { 'D': 'domain'
, 'I': 'uri-file'
, 'O': 'output'
}
#try getting arguments
try:
opts, args = getopt.getopt( argv
, "D:I:O: h"
, [ "domain="
, "uri-file="
, "output="
]
)
except getopt.GetoptError as err:
usage(err)
#take out inputs and create our call based on the params
options = {}
for opt, value in opts:
if opt in ["-h", "help"]:
usage("")
key = opt.strip("-")
#replace short tag with long text equivalent
if map_commands.get(key, False):
key = map_commands.get(key)
#ensure we only set the index once
if not options.get(key, False):
options[key] = value
#if we have no options go to usage
if not options:
usage( "please set options" )
pdf(options)
#
# Create our PDF
#
def pdf(options):
print options
#ensure we atleast have a domain
if not options.get('domain',False):
usage( "please set a domain")
#see if we need to do anything extra
if 'uri-file' in options:
#get the uris from the file
urls = urls_from_file( options.get( 'uri-file' ) )
cmd = "wkhtmltopdf "
for url in urls:
cmd += " "+options.get('domain')+"/"+url
cmd += " "+options.get('output',options.get('domain','')+'.pdf')
print cmd
os.system(cmd)
sys.exit(0)
#
# Read in uri file for urls.
#
def urls_from_file( file ):
reader = csv.reader( open( file ), delimiter='\n' )
urls = []
for row in reader:
urls.extend( row );
#return our urls from file
return urls
#
# Man Page
#
def usage( error_str ):
usage = ""
if error_str != "":
usage += "\nThere was an issue with your command. Please review you statment against our list on commands!:\n"
usage += "\t*"
usage += str( error_str )
print usage
print '''
PrintPal
---------
-> Automated Site PDF Creation
PrintPal is an html to pdf CLI utility written in Python. Its purpose is to
easily take a domain with one or more urls and bundle each html page into one
pdf document utilizing wkhtmltopdf <http://code.google.com/p/wkhtmltopdf/>.
**********************************************************************
Usage
------
//create a simple page pdf of a site
$ python printpal.py -D example.com/index.html
//create a multi-page pdf of a site
$ python printpal.py -D example.com -I urls.txt
//create a pdf with a specified output
$python printpal.py -D example.com -O ~/Desktop/Sample.pdf
**********************************************************************
Available Commands
------------------
-D <--domain>: Domain of website to print.
-I <--uri-file>: file of uri segments to use with domain ( newline delimited ).
-O <--output>: Path where the file should be saved to.
**********************************************************************
@Author: Chris Morris http://github.com/morriswchris
@Date: July 24 2013
@version: 1.0.0
Released under the MIT license
'''
sys.exit(0)
#call our main app
main(sys.argv[1:])