-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRakefile
309 lines (243 loc) · 9.25 KB
/
Rakefile
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
require "bridgetown"
Bridgetown.load_tasks
#
# Standard set of tasks, which you can customize if you wish:
#
desc "Build the Bridgetown site for deployment"
task :deploy => [:clean, "frontend:build"] do
Bridgetown::Commands::Build.start
end
desc "Build the site in a test environment"
task :test do
ENV["BRIDGETOWN_ENV"] = "test"
Bridgetown::Commands::Build.start
end
desc "Runs the clean command"
task :clean do
Bridgetown::Commands::Clean.start
end
namespace :frontend do
desc "Build the frontend with esbuild for deployment"
task :build do
sh "yarn run esbuild"
end
desc "Watch the frontend with esbuild during development"
task :dev do
sh "yarn run esbuild-dev"
rescue Interrupt
end
end
#
# Add your own Rake tasks here! You can use `environment` as a prerequisite
# in order to write automations or other commands requiring a loaded site.
#
# task :my_task => :environment do
# puts site.root_dir
# automation do
# say_status :rake, "I'm a Rake tast =) #{site.config.url}"
# end
# end
namespace :import do
task :convertkit => :environment do
# CONVERTKIT_API=yAo... bin/bt import:convertkit
site # init
broadcasts_count = site.collections.broadcasts.read.resources.count
previous_issues = 10 + broadcasts_count
api_secret = ENV.fetch("CONVERTKIT_API")
broadcasts = Faraday
.get("https://api.convertkit.com/v3/broadcasts?api_secret=#{api_secret}")
.body
.then { JSON.parse(_1, symbolize_names: true) }
broadcasts[:broadcasts].each do |broadcast|
id = broadcast[:id]
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:broadcasts, "_broadcasts/#{id}.html")
unless origin.exists?
puts "* CREATING #{id}"
item = Faraday
.get("https://api.convertkit.com/v3/broadcasts/#{id}?api_secret=#{api_secret}")
.body
.then { JSON.parse(_1, symbolize_names: true)[:broadcast] }
model = Bridgetown::Model::Base.new(number: previous_issues + 1, title: item[:subject], subtitle: "Issue description goes here.", date: item[:created_at])
model.content = item[:content].gsub(/\<(\/?)h2\>/, "<\\1h3>").gsub("<p></p>", "")
model.origin = origin
model.save
end
end
end
task :youtube, [:url] => :environment do |task, args|
# YOUTUBE_API=Alz... bin/bt import:youtube[https://www.youtube.com/watch?v=8TIiLAYnj3A]
require "yt"
Yt.configure do |config|
config.log_level = :debug
config.api_key = ENV["YOUTUBE_API"]
end
site # init
yt_url = Yt::URL.new args.url
video = yt_url.resource
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:posts, "_posts/videos/#{video.published_at.strftime("%Y")}/#{video.published_at.strftime("%Y-%m-%d")}-#{Bridgetown::Utils.slugify(video.title, mode: "ascii")}.md")
model = Bridgetown::Model::Base.new(
published: true,
category: :videos,
title: video.title,
description: video.description.split(/\n/)[0],
date: video.published_at,
youtube_id: video.id,
tags: "portland oregonexplored vlog" #default
)
model.content = video.description.gsub(/(\n)/, " \\1")
model.origin = origin
model.save
puts "Done! Saved in: src/#{origin.relative_path}"
end
task :glass, [:url] => :environment do |task, args|
# bin/bt import:glass[url]
require "faraday"
require "faraday/encoding"
require "nokogiri"
site # init
conn = Faraday.new do |connection|
connection.response :encoding # use Faraday::Encoding middleware
connection.adapter Faraday.default_adapter # net/http
end
body = conn.get(args.url).body
doc = Nokogiri::HTML5(body)
description = doc.at_css(".postInfo p").content + "\n#Portland #OregonExplored #iPhonePro"
timestamp = doc.at_css("script#__NEXT_DATA__").content.match(/"created_at":"(.*?)"/)[1]
published_at = Date.parse(timestamp)
slug = description.split[..5]
img = doc.at_css("*[class^=ImageContainer_imageContent] img")
image_url = img["data-srcset"].match(/, (.*?) 2048w,/)[1]
thumbnail_url = doc.at_css("script#__NEXT_DATA__").content.match(/image640x640":"(.*?)"/)[1].gsub("\\u0026", "&")
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:posts, "_posts/pictures/#{published_at.strftime("%Y")}/#{published_at.strftime("%Y-%m-%d")}-#{Bridgetown::Utils.slugify(slug, mode: "ascii")}.md")
model = Bridgetown::Model::Base.new(
published: true,
category: :pictures,
image_details: doc.css(".postInfo li").map(&:content)[-2..],
date: timestamp,
image: image_url,
thumbnail_url:,
glass_url: args.url,
tags: "portland oregonexplored iPhonePro" #default
)
model.content = description.gsub(/(\n)/, " \\1")
model.origin = origin
model.save
puts "Done! Saved in: src/#{origin.relative_path}"
end
task :pixelfed => :environment do
require "nokogiri"
require "rss"
site # init
feed_url = "https://pixelfed.social/users/essentiallife.atom"
feed = RSS::Parser.parse(feed_url)
feed.entries.each do |entry|
parsed_content = Nokogiri::HTML5(entry.content.content)
image_url = parsed_content.at_css("img")[:src]
thumbnail_url = image_url.sub(".jpg", "_thumb.jpg")
description = parsed_content.to_str.strip
timestamp = entry.updated.content
slug = entry.summary.content.split("<br />").first.split(" ")[0..9].join(" ")
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:posts, "_posts/pictures/#{timestamp.strftime("%Y")}/#{timestamp.strftime("%Y-%m-%d")}-#{Bridgetown::Utils.slugify(slug, mode: "ascii")}.md")
unless origin.exists?
model = Bridgetown::Model::Base.new(
published: true,
category: :pictures,
date: timestamp.iso8601,
image: image_url,
thumbnail_url:,
pixelfed_url: entry.id.content,
tags: Builders::Helpers.new.extracted_hashtags(description),
)
model.content = description.gsub(/(\n)/, " \\1")
model.origin = origin
model.save
puts "Done! Saved in: src/#{origin.relative_path}"
end
end
end
task :feedbin => :environment do
require "rss"
site
helpers = Bridgetown::RubyTemplateView::Helpers.new(nil, site)
rss = Faraday.get("https://feedbin.com/starred/77f395c150643002f81012f9aecea283.xml").body
feed = RSS::Parser.parse(rss)
puts "Which article would you like to link to?"
puts
feed.items[0...5].each_with_index do |item, index|
puts "#{index + 1}. #{item.title}"
end
puts
putc ">"
putc " "
answer = STDIN.gets.chomp.to_i
item = feed.items[answer - 1]
today = DateTime.now
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:posts, "_posts/links/#{today.strftime("%Y")}/#{today.strftime("%Y-%m-%d")}-#{Bridgetown::Utils.slugify(item.title, mode: "ascii")}.md")
model = Bridgetown::Model::Base.new(
published: true,
category: :links,
mono_styled: true,
title: item.title,
link_url: item.link,
link_excerpt: helpers.strip_html(item.description).strip.truncate_words(200),
date: today
)
model.content = "Commentary goes here."
model.origin = origin
model.save
puts "Done! Saved in: src/#{origin.relative_path}"
end
task :makertube => :environment do
require "rss"
site # init
helpers = Bridgetown::RubyTemplateView::Helpers.new(nil, site)
feed_url = "https://makertube.net/feeds/videos.xml?videoChannelId=4323"
feed = RSS::Parser.parse(feed_url)
feed.items.each do |item|
description = item.content_encoded
timestamp = item.date
slug = item.title
video_id = item.enclosure.url.split("/")[5]
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:posts, "_posts/videos/#{timestamp.strftime("%Y")}/#{timestamp.strftime("%Y-%m-%d")}-#{Bridgetown::Utils.slugify(slug, mode: "ascii")}.md")
unless origin.exists?
model = Bridgetown::Model::Base.new(
published: true,
category: :videos,
title: item.title,
description: helpers.strip_html(description).split("\n")[0],
date: timestamp.iso8601,
makertube_id: video_id,
tags: "portland oregonexplored vlog" #default
)
model.content = description
model.origin = origin
model.save
puts "Done! Saved in: src/#{origin.relative_path}"
end
end
end
end
namespace :write do
task :thought => :environment do
site
today = DateTime.now
puts "What’s your thought?"
thought_content = $stdin.gets
puts "Slug:"
slug = $stdin.gets.chomp
origin = Bridgetown::Model::RepoOrigin.new_with_collection_path(:posts, "_posts/thoughts/#{today.strftime("%Y")}/#{today.strftime("%Y-%m-%d")}-#{slug}.md")
model = Bridgetown::Model::Base.new(
published: true,
category: :thoughts,
date: today,
tags: Builders::Helpers.new.extracted_hashtags(thought_content),
mono_styled: true
)
model.content = thought_content
model.origin = origin
model.save
puts "Done! Saved in: src/#{origin.relative_path}"
end
end
# Run rake without specifying any command to execute a deploy build by default.
task default: :deploy