-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_and_play_video.rb
132 lines (120 loc) · 2.95 KB
/
fetch_and_play_video.rb
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
# encoding: utf-8
#require "logger"
require "open-uri"
require "mail"
require "yaml"
Mail.defaults do
delivery_method :smtp, {
address: "smtp.gmail.com",
user_name: "xinye181920",
password: "xinye.181920",
authentication: "plain",
}
end
def mail_notification(msg)
mail = Mail.deliver do
to "[email protected]"
from "[email protected]"
subject "xinye video program error!"
text_part do
body msg
end
end
puts "notification mail sent!"
rescue => e
puts e
end
def location
YAML.load(File.read(File.join(File.dirname(__FILE__),"config/location.yml")))["loc"]
end
def current_path
File.dirname(__FILE__)
end
def download_path
File.join(File.dirname(__FILE__),"downloads")
end
def video_in_folder
video_path = Dir.glob(File.join(download_path,"*")).first
if video_path
video_path.split("/").last
else
nil
end
end
def net_connected?
loop do
break if open("http://www.xinyegroup.com/dalaoju/s.aspx?id=#{location}")
#system "zenity --timeout=3 --error --text='网络异常!'"
system "qiv -W 35 --center #{current_path}/config/offline.jpg &"
sleep 3
system "killall qiv"
end
system "killall qiv"
true
rescue => e
puts e
system "qiv -W 35 --center #{current_path}/config/offline.jpg &"
sleep 3
system "killall qiv"
mail_notification e.to_s
end
def clean_old_files(new_video_name)
puts "clean all old files"
Dir.glob(File.join(download_path,"*")).each do |f|
unless f.split("/").last == new_video_name
puts "deleted file: #{f}" if File.delete f
end
end
rescue => e
puts e
mail_notification e.to_s
end
def play_video(video)
#system "mplayer -fs -loop 0 #{video} &"
system "cvlc -fL --one-instance --no-video-title-show --no-video-on-top #{video} -d"
rescue => e
puts e
mail_notification e.to_s
end
def new_video
if net_connected?
video_url = open("http://www.xinyegroup.com/dalaoju/s.aspx?id=#{location}").read.strip
video_name = video_url.split("/").last
if video_name == video_in_folder then
return false
else
return video_url
end
end
rescue => e
puts e
mail_notification e.to_s
end
play_video File.join(download_path,video_in_folder) if video_in_folder
loop do
begin
puts "检查是否有更新--->"
if video_path = new_video
puts "updated..."
#system "zenity --info --text='更新视频中' &"
download = system "wget -c --progress=bar:force --directory-prefix=#{download_path} #{video_path} 2>&1 | zenity --title='更新视频' --progress --auto-close --auto-kill"
if download then
clean_old_files video_path.strip.split("/").last
#system "killall zenity"
#system "killall mplayer"
#system "killall vlc"
play_video File.join(download_path,video_in_folder) if video_in_folder
else
puts "download error!"
raise "download error!"
end
else
puts "no update."
end
sleep 30
rescue => e
puts e
mail_notification e.to_s
sleep 30
end
end