-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtenseijingo
executable file
·76 lines (60 loc) · 1.67 KB
/
tenseijingo
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
#!/usr/bin/env ruby
# coding: utf-8
# desc: get today's Tenseijingo column
# author: eric
require 'date'
require 'moji'
Encoding.default_internal = "UTF-8"
# variables
file = Dir.home + '/.sync/diudiudui/tensei/tensei.txt'
today = Date.today
yesterday = today.prev_day
cache_dir = Dir.home + '/.sync/diudiudui/tensei/cache/'
cache_file = cache_dir + "tensei-#{today.strftime '%y%m%d'}.txt"
cache_file_yesterday = cache_dir +
"tensei-#{yesterday.strftime '%y%m%d'}.txt"
title_date = Moji.han_to_zen "#{today.year}年 #{today.mon}月#{today.day}日"
page_url = 'http://www.asahi.com/paper/column.html'
element = '#MainInner .ArticleText'
# main
puts "tenseijingo downloader"
if File.exist? cache_file
puts "today's job is already done."
exit
end
# get today's content
require 'nokogiri'
require 'open-uri'
begin
puts "fetching web content..."
page = Nokogiri::HTML open(page_url, 'r:utf-8')
rescue
p $!
puts "error occured, exit."
exit
else
puts "content got"
end
content = page.css(element).text.strip
success_message = "column is downloaded."
# check newspaper holiday
if File.exist? cache_file_yesterday
content_yesterday = File.read( cache_file_yesterday,
{ encoding: 'utf-8' }
).chomp
if content == content_yesterday
content = "【休刊日】"
success_message = "oops, newspaper holiday :)"
end
end
# write cache
File.open(cache_file, 'w:utf-8') { |f| f.puts content }
# add to file
File.open file, 'a:utf-8' do |f|
f.puts
f.puts "[#5字下げ]#{title_date}[#「#{title_date}」は中見出し]"
f.puts
f.puts content
end
# done
puts success_message