-
Notifications
You must be signed in to change notification settings - Fork 18
/
Rakefile
109 lines (88 loc) · 2.49 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
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'],
to: ENV['TO'] || "[email protected]",
authentication: 'plain',
enable_starttls_auto: true
end
class Kindle
def initialize(path = 'data.json')
@path = path
end
def simpleTest
kindle = KindleHighlights::Client.new(email_address: ENV["AMAZON_USER"] || "[email protected]", password: ENV["AMAZON_PASS"] || "youramazonpassword")
# begin
# puts kindle.books.sample
# rescue Exception => e
# puts e
# end
begin
puts kindle.books.sample
rescue Exception => e
puts e
end
return kindle
end
def update(kindle)
html = HTMLEntities.new
# kindle = KindleHighlights::Client.new(email_address: ENV["AMAZON_USER"] || "[email protected]", password: ENV["AMAZON_PASS"] || "youramazonpassword")
@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.simpleTest)
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
puts "OK, sent email"
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]