-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathimport
executable file
·229 lines (194 loc) · 5.75 KB
/
import
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env ruby
require 'rexml/document'
require 'date'
require 'fileutils'
require 'net/http'
require 'uri'
require 'cgi'
class Encloser
@enclosures_size
def initialize(import_file)
@import_file = import_file
end
def retrieve_enclosures
@enclosures_size = Array.new(60) { }
@enclosures_size[1] = 71902534
@enclosures_size[2] = 15911287
@enclosures_size[3] = 28003038
@enclosures_size[4] = 19005003
@enclosures_size[5] = 34507554
@enclosures_size[6] = 19724935
@enclosures_size[7] = 31708241
@enclosures_size[8] = 30610041
@enclosures_size[9] = 33890098
doc = REXML::Document.new( File.read( @import_file ) )
root = doc.root
root.get_elements( 'channel/item/enclosure' ).each do |enclosure|
retrieve_enclosure( enclosure )
end
end
def enclosure_size(index)
@enclosures_size[index]
end
private
def retrieve_enclosure(enclosure)
length = enclosure.attribute('length').value
url = enclosure.attribute('url').value
if ( url =~ /[.]*Episode-([0-9]*).mp3/)
puts "#{length} for #{$1}"
@enclosures_size[Integer($1)] = length
end
end
end
class Importer
@encloser
def initialize(import_file, output_dir, rss_feed)
@import_file = import_file
@output_dir = output_dir
@encloser = Encloser.new( rss_feed )
@encloser.retrieve_enclosures
end
def import_file
doc = REXML::Document.new( File.read( @import_file ) )
root = doc.root
root.get_elements( 'channel/item' ).each do |item|
import_item( item )
end
end
private
def import_item(item_xml)
type = item_xml.get_text( 'wp:post_type' )
return if type == 'page'
status = item_xml.get_text( 'wp:status' )
return if status != 'publish'
tags = Array.new
item_xml.get_elements( 'category' ).each do |tag|
value = tag.text
if value != "Uncategorized"
tags << value
end
end
title = item_xml.get_text( 'title' ).to_s
author = 'Emmanuel Bernard'
link = item_xml.get_text( 'link' ).to_s
if ( title =~ /[.]*pisode[s]? ([0-9]+)[.]*/ )
episode = $1
enclosure_size = @encloser.enclosure_size(Integer(episode))
end
if ( link =~ %r(/([^/]+)/$) )
slug = $1
end
published = DateTime.parse( item_xml.get_text( 'pubDate' ).to_s )
#content = REXML::Text.unnormalize( item_xml.get_text( 'content:encoded' ).to_s )
content = item_xml.get_text( 'content:encoded' ).to_s
in_pre = false
p_content = ''
content.each_line do |line|
if ( line =~ /<pre>/ )
in_pre = true
end
if ( line =~ /<\/pre>/ )
in_pre = false
end
if ( in_pre || ( line =~ /^\s*</ ) )
p_content << line
elsif ( line.strip == '' )
# nothing
else
p_content << "<p>#{line}</p>\n"
end
end
content = p_content
content = import_images( content )
content = import_assets( content )
output_path = File.join( @output_dir, published.strftime( "%Y-%m-%d-#{slug}.html.erb" ) )
#if ( ! File.exist?( output_path ) )
puts "writing post: #{output_path}"
FileUtils.mkdir_p( File.dirname( output_path ) )
File.open( output_path, 'w' ) do |f|
#f.puts "date: #{published.year}-#{published.month}-#{published.day}"
f.puts '---'
f.puts "title: '#{title}'"
f.puts "author: 'Emmanuel Bernard'"
f.puts "layout: blog-post"
if ( !episode.nil? && !episode.empty? )
f.puts "episode: #{episode}"
end
if ( !enclosure_size.nil? )
f.puts "mp3_length: #{enclosure_size}"
end
f.puts '---'
f.puts content
end
#end
end
def import_images(content)
#doc = REXML::Document.new( '<entry>' + CGI.escapeHTML( content ) + '</entry>' )
doc = REXML::Document.new( '<entry>' + content + '</entry>' )
REXML::XPath.each( doc, '//img' ) do |img|
src = img.attributes['src']
if ( src =~ /^\// )
src = "http://lescastcodeurs.com#{src}"
else
next
end
basename = File.basename( src )
output_path = "posts/assets/#{basename}"
url = URI.parse( src )
res = Net::HTTP.start(url.host, url.port) {|http|
http.get(url.path)
}
FileUtils.mkdir_p( File.dirname( output_path ) )
puts "writing image: #{output_path}"
File.open( output_path, 'wb' ) do |f|
f.write res.body
end
img.attributes['src'] = "/#{output_path}"
end
content = ''
doc.root.children.each do |c|
content += c.to_s
end
content
end
def import_assets(content)
doc = REXML::Document.new( '<entry>' + content + '</entry>' )
REXML::XPath.each( doc, '//a' ) do |a|
href = a.attributes['href']
local = false
if ( href =~ /^\// )
href = "http://lescastcodeurs.com#{href}"
local = true
end
if ( local && href =~ /\.(pdf)$/ )
basename = File.basename( href )
output_path = "posts/assets/#{basename}"
url = URI.parse( href )
if ( ! File.exist?( output_path ) )
puts "fetching: #{url}"
FileUtils.mkdir_p( File.dirname( output_path ) )
File.open( output_path, 'wb' ) do |f|
Net::HTTP.start(url.host, url.port) do |http|
http.get(url.path) do |str|
f.write str
$stderr.putc '.'
$stderr.flush
end
end
end
puts "writing asset: #{output_path}"
end
a.attributes['href'] = "/#{output_path}"
end
end
content = ''
doc.root.children.each do |c|
content += c.to_s
end
content
end
end
if ( $0 == __FILE__ )
importer = Importer.new( ARGV[0], ARGV[1], ARGV[2] )
importer.import_file
end