forked from 200ok-ch/media_feed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd
executable file
·47 lines (38 loc) · 1011 Bytes
/
add
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
#!/usr/bin/env ruby
# USAGE
#
# add <url>
#
require 'erb'
require 'time'
require 'ostruct'
def curl(url)
output = %x[ curl -s --head --location '#{url}' ]
md = output.match(/Location: (.*)\n/)
location = md ? md[1].chomp : url
OpenStruct.new({
length: output.match(/Content-Length: (.*)\n/)[1].chomp,
type: output.scan(/Content-Type: (.*)\n/).last[0].chomp,
title: url.split('/').last.split('.').tap { |a| a.pop } * '.',
now: Time.now.rfc2822,
url: location
})
end
meta = curl(ARGV[0])
# TODO ask for missing details
scope = meta.instance_eval { binding }
result = ERB.new(DATA.read).result(scope)
media = File.read('media.yml')
media.sub!("feed:\n", result)
File.open('media.yml', 'w') { |f| f.print(media) }
puts 'Now edit `media.yml` to complete the missing details.'
__END__
feed:
- title: <%= title %>
published: <%= now %>
media_url: <%= url %>
media_length: <%= length %>
media_type: <%= type %>
web_url: TODO
description: >-
TODO