This repository has been archived by the owner on May 26, 2023. It is now read-only.
forked from tobi/highlights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
87 lines (72 loc) · 1.93 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
require 'bundler'
require 'kindle_highlights'
require 'fileutils'
require 'json'
require 'cgi'
require 'mail'
require 'htmlentities'
Mail.defaults do
delivery_method :smtp,
address: ENV['MAILGUN_SMTP_SERVER'] || "smtp.mailgun.org",
port: ENV['MAILGUN_SMTP_PORT'] || 587,
user_name: ENV['MAILGUN_SMTP_LOGIN'],
password: ENV['MAILGUN_SMTP_PASSWORD'],
authentication: 'plain',
enable_starttls_auto: true
end
class Kindle
def initialize(path = 'data.json')
@path = path
end
def update
html = HTMLEntities.new
kindle = KindleHighlights::Client.new(ENV["AMAZON_USER"], ENV["AMAZON_PASS"])
@highlights = []
kindle.books.each do |key, title|
kindle.highlights_for(key).each do |highlight|
highlight["book"] = html.decode(title)
highlight["highlight"] = html.decode(highlight["highlight"])
@highlights << highlight
end
end
end
def save
File.open(@path, "w+") do |fp|
fp << @highlights.to_json
end
end
def highlights
@highlights ||= JSON.load(open(@path))
end
def random_highlight
highlights[rand(highlights.length)]
end
end
task :download do
data = Kindle.new
data.update
data.save
end
task :print do
data = Kindle.new
highlight = data.random_highlight
puts "\"#{highlight["highlight"]}\""
puts
puts " -- #{highlight["book"]}, #{highlight["howLongAgo"]}"
puts
end
task :email do
data = Kindle.new
highlight = data.random_highlight
mail = Mail.new do
from 'Kindle Highlights <[email protected]>'
to ENV['TO']
subject "#{Time.now.strftime("%b %d")}: #{highlight["book"]}"
html_part do
content_type 'text/html; charset=UTF-8'
body "<p><i>#{highlight["highlight"]}</i><p><br><p>— #{highlight["book"]}, #{highlight["howLongAgo"]}</p>"
end
end
mail.deliver
end
task default: [:download, :email]