-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebPublisher.groovy
185 lines (145 loc) · 5.88 KB
/
WebPublisher.groovy
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
183
184
185
import groovy.text.GStringTemplateEngine
class WebPublisher {
String publish_base = '/var/www/html/'
Db db = new Db()
boolean forced = false
String templatePath = './templates'
class Publish {
String template = ''
int page = 0
boolean hasMore = false
String uuid = ''
String channel = ''
def data = [:]
def next = null
def prev = null
}
WebPublisher() {
if (new File('zore_vids').exists()) {
print "Remove invalidation video "
new File('zore_vids').readLines().each{ line ->
print '.'
db.deleteVideoByVID(line);
}
println 'done!'
}
}
int publishContent() {
int count = 0;
print "start publish content page "
def video_list = forced ? db.listVideosAll() as List
: db.listVideos() as List
for (int i=0; i < video_list.size(); i++) {
def uuid = video_list[i]
if (forced && i == video_list.size() - 1)
break;
if (forced || !new File(publish_base + urlVideo(uuid)).exists()) { // skip exist content page
print '.'
def bytes = new File(templatePath + '/content.html').readBytes()
def template = new String(bytes, 'UTF-8').replace('$', '\\$')
def video = db.findVideo(uuid)
Publish task = new Publish('template': template, 'channel': video.channel)
if (i > 0) {
task.next = db.findVideo(video_list[i-1]);
if (task.next.channel != video.channel) {
task.next = null;
}
}
if (i < video_list.size() - 1) {
task.prev = db.findVideo(video_list[i+1]);
if (task.prev.channel != video.channel) {
task.prev = null;
}
}
task.data.putAll(video)
Utils.mkdirs4file(publish_base + urlVideo(uuid))
new FileOutputStream(publish_base + urlVideo(uuid)).withWriter('UTF-8'){ w ->
w << publishTask(task);
}
count++
}
}
println " done!"
return count
}
int publishIndex() {
int count = 0;
print "start publish list page "
db.findChannels().each { channel ->
def bytes = new File(templatePath + '/list.html').readBytes()
def template = new String(bytes, 'UTF-8').replace('$', '\\$')
Publish task = new Publish('template': template, 'channel': channel)
// publish pages
while(true) {
print '.'
Utils.mkdirs4file(publish_base + urlChannel(channel,task.page))
new FileOutputStream(publish_base + urlChannel(channel,task.page)).withWriter('UTF-8'){ w ->
w << publishTask(task);
}
count++
if (!task.hasMore)
break;
task.page++;
task.hasMore = false;
}
}
println " done!"
return count;
}
def publishHomepage() {
print "start publish home page "
def bytes = new File(templatePath + '/index.html').readBytes()
def template = new String(bytes, 'UTF-8').replace('$', '\\$')
Publish task = new Publish('template': template)
Utils.mkdirs4file(publish_base + '/index.html')
new FileOutputStream(publish_base + '/index.html').withWriter('UTF-8'){ w ->
w << publishTask(task);
}
print '.'
println " done!"
}
int pagesChannel(String channel, int max = 10) {
int count = db.countVideosByChannel(channel)
return Math.floor((count + max / 2)/max)
}
def urlChannel(String channel, def page = null) {
String id = Utils.encodeAsMD5Hex(channel)
return '/c/' + id + '/' + 'index' + (page > 0 ? "_$page" : '') + '.html'
}
def urlVideo(String uuid) {
return '/v/' + uuid + '.html'
}
def urlVideoPoster(String uuid) {
return '/v/p/' + uuid + '.jpg'
}
def publishTask(def task) {
def binding = ['service': this, 'task': task]
def engine = new GStringTemplateEngine()
def template = engine.createTemplate(task.template).make(binding)
return template.toString()
}
static void main(String[] args) {
def cli = new CliBuilder()
cli.h( longOpt: 'help', required: false, '显示当前帮助信息' )
cli.p( longOpt: 'publish-path', argName: 'publish-path', required: false, args: 1, '发布路径' )
cli.m( longOpt: 'templates', argName: 'templates', required: false, args: 1, '模板路径' )
cli.f( longOpt: 'force', required: false, args: 0, '强制发布' )
cli.d( longOpt: 'publish-date', argName: 'publish-date', required: false, args: 1, '发布日期[yyyyMMdd]' )
def opt = cli.parse(args)
if (!opt) return;
if (opt.h) {
cli.usage();
return
}
def publisher = new WebPublisher();
if (opt.p) publisher.publish_base = opt.p;
if (opt.m) publisher.templatePath = opt.m;
if (opt.f) publisher.forced = true;
if (publisher.publishContent() > 0 || !publisher.forced) {
publisher.publishIndex()
publisher.publishHomepage()
} else {
println "no more content to publish!"
}
}
}