my-lib 0.0.1.1 → 0.0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/my-lib.rb CHANGED
@@ -1,442 +1,44 @@
1
1
  #-*- coding:utf-8 -*-
2
2
 
3
- # make String Colored
4
- require 'rubygems'
5
- require 'term/ansicolor'
6
- class String
7
- include Term::ANSIColor
8
- end
9
-
10
- # Class For Log to /var/log/system.log
11
- # @author modeverv@gmail.com
12
- # @example
13
- # MyLogger.ln("message")
14
- # MyLogger.lw("message")
15
- class MyLogger
16
-
17
- Version = "0.0.1"
18
-
19
- # log notice
20
- # @param [String] message message for log
21
- def self.ln(message)
22
- self.before
23
- Syslog.log(Syslog::LOG_NOTICE, "%s", message)
24
- self.after
25
- end
26
-
27
- # log warning
28
- # @param [String] message message for log
29
- def self.lw(message)
30
- self.before
31
- Syslog.log(Syslog::LOG_WARNING, "%s", message)
32
- self.after
33
- end
34
-
35
- private
36
- # open syslog
37
- def self.before
38
- require 'syslog'
39
- include Syslog::Constants
40
- Syslog.open("ruby_syslog.rb")
41
- end
42
- # close syslog
43
- def self.after
44
- Syslog.close();
45
- end
46
-
47
- end
48
-
49
- # Job Class for Fetch Anisoku
50
- # Function:
51
- # access "youtubeanisoku1.blog106.fc2.com" and crawl web site.
52
- # find a link to "say-move.org" and access "say-move.org".
53
- # finary find video link of Animetion,then fetch video file to your local.
54
- # save video directory is supplied by machine.
55
- # Notice:
56
- # job is automatically generated on after another.
57
- # This Class Needs to be handle by Machine Class
58
- # @example
59
- # # inside machine class
60
- # job = MyJobAnisoku.new(
61
- # :machine => self
62
- # )
63
- # job.run
64
- #
65
- class MyJobAnisoku
66
-
67
- Version = "0.0.1"
68
-
69
- def initialize(args = { })
70
- require 'rubygems'
71
- require 'kconv'
72
- require 'mechanize'
73
- require 'net/http'
74
- @a = args
75
- @a[:url] ||= 'http://youtubeanisoku1.blog106.fc2.com/'
76
- @a[:url] = URI.parse @a[:url] unless @a[:url].class == URI::HTTP
77
- @agent = Mechanize.new
78
- @a[:status] ||= :new
79
- raise "job have no machine error" unless @a[:machine]
80
- end
81
-
82
- # check kousin page
83
- def tokkakari
84
- @agent.get @a[:url]
85
- links_kousins = @agent.page.links_with(:text => /#{"更新状況".toutf8}/)
86
- targs = []
87
- links_kousins.each do |link|
88
- targs << link.uri
89
- end
90
- targs.each_with_index do |link,i|
91
- break if i > 6
92
- job = MyJobAnisoku.new(
93
- :url => link,
94
- :status => :second,
95
- :machine => @a[:machine]
96
- )
97
- @a[:machine].retry job
98
- end
99
- end
100
-
101
- # check shoukai page
102
- def second
103
- @agent.get @a[:url]
104
- links_kousin = @agent.page/"/html/body/table/tr[2]/td/table/tr/td[2]/div[4]/ul/li/a/@href"
105
- # links_kobetu
106
- links_kobetu = []
107
- links_kousin.each do |link|
108
- links_kobetu << $1 if link.value =~ /(http:\/\/youtubeanisoku.*)/
109
- end
110
-
111
- # make job for each links_kobetu
112
- links_kobetu.each do |link|
113
- job = MyJobAnisoku.new(
114
- :url => link,
115
- :status => :kobetu,
116
- :machine => @a[:machine]
117
- )
118
- @a[:machine].retry job
119
- end
120
- end
121
-
122
- #access say-move and make video job
123
- def third
124
- #sm has title and url
125
- sm = { :title => @a[:title],:url => @a[:url]}
126
- @agent.get(sm[:url])
127
- set = @agent.page/"/html/body/div/div[2]/div[7]/div[2]/input/@value"
128
- if set[0]
129
- sm[:videourl] = set[0].value
130
- end
131
-
132
- job = MyJobAnisoku.new(
133
- :url => sm[:videourl],
134
- :title => sm[:title],
135
- :status => :video,
136
- :machine => @a[:machine]
137
- )
138
- @a[:machine].retry job
139
- end
140
-
141
- #access say-move and make video job
142
- def kobetu
143
- @agent.get @a[:url]
144
- nodeset = @agent.page/"/html/body/table/tr[2]/td/table/tr/td[2]/div[4]/div[2]"
145
- begin
146
- titles = nodeset[0].inner_text.gsub(' ','').gsub(' ','').
147
- gsub('「Daum」|','').
148
- gsub('【Yahoo!】','').
149
- gsub('veoh','').
150
- gsub('SM|','').
151
- gsub('|','').
152
- split("\r\n").
153
- select{|e| $1 if e=~/(第.*)/ }.
154
- map{|k| k =~ /(第(\d{1,2}).*)/; { :episode => $2, :title => $1} }
155
- titles.reverse!
156
-
157
-
158
- _tt = @agent.page.title.gsub(' ★ You Tube アニ速 ★','')
159
-
160
- #hard coding for adjust fetch limit
161
- title = _tt + titles.shift[:title].to_s
162
- title2 = _tt + titles.shift[:title].to_s
163
- title3 = _tt + titles.shift[:title].to_s
164
- title4 = _tt + titles.shift[:title].to_s
165
- title5 = _tt + titles.shift[:title].to_s
166
-
167
- rescue => ex
168
- p ex
169
- return
170
- end
171
- nodeset_vs = @agent.page/"/html/body/table/tr[2]/td/table/tr/td[2]/div[4]/div[2]/a/@href"
172
- _dd = []
173
- nodeset_vs.each do |va|
174
- _dd << $1 if va.value =~ /(http:\/\/say-move\.org\/comeplay\.php.*)/
175
- end
176
- _dd.reverse!
177
-
178
- #hard coding for adjust fetch limit
179
- url = _dd.shift
180
- url2 = _dd.shift
181
- url3 = _dd.shift
182
- url4 = _dd.shift
183
- url5 = _dd.shift
184
-
185
- #hard coding for adjust fetch limit
186
- unless url.nil?
187
- job = MyJobAnisoku.new(
188
- :url => url,
189
- :title => title,
190
- :status => :third,
191
- :machine => @a[:machine]
192
- )
193
- @a[:machine].retry job
194
- end
195
-
196
- unless url2.nil?
197
- job = MyJobAnisoku.new(
198
- :url => url2,
199
- :title => title2,
200
- :status => :third,
201
- :machine => @a[:machine]
202
- )
203
- @a[:machine].retry job
204
- end
205
-
206
- unless url3.nil?
207
- job = MyJobAnisoku.new(
208
- :url => url3,
209
- :title => title3,
210
- :status => :third,
211
- :machine => @a[:machine]
212
- )
213
- @a[:machine].retry job
214
- end
215
-
216
- unless url4.nil?
217
- job = MyJobAnisoku.new(
218
- :url => url4,
219
- :title => title4,
220
- :status => :third,
221
- :machine => @a[:machine]
222
- )
223
- @a[:machine].retry job
224
- end
225
-
226
- unless url5.nil?
227
- job = MyJobAnisoku.new(
228
- :url => url5,
229
- :title => title5,
230
- :status => :third,
231
- :machine => @a[:machine]
232
- )
233
- @a[:machine].retry job
234
- end
235
-
236
- end
237
-
238
- #fetch video
239
- def video
240
- # save video directory is supplied by machine.
241
- savedir = @a[:machine].savedir
242
- Dir.chdir savedir
243
- filename = "#{@a[:title]}.mp4"
244
- savepath = "#{savedir}/#{filename}"
245
-
246
- puts "Fetching ".green.bold + savepath
247
-
248
- if File.exist?(savepath) && File.size(savepath) > 1024 * 1024 * 3
249
- puts "File Already Saved ".yellow.bold + savepath
250
- return
251
- else
252
- MyLogger.ln "Fetch Attempt Start ".green.bold + savepath
253
- end
254
- # use curl command
255
- command = "curl -# -L -R -o '#{filename}' '#{@a[:url].host}#{@a[:url].path}'"
256
- system command
257
- =begin
258
- consume too much memory!!!!
259
- begin
260
- @http = EventMachine::Protocols::HttpClient.request(
261
- :host => @a[:url].host,
262
- :port => @a[:url].port,
263
- :request => @a[:url].path
264
- )
265
- rescue => ex
266
- p ex
267
- return
268
- end
269
-
270
- @http.callback {|response|
271
- if response[:status] == 200
272
- puts "# 200".green
273
- open(savepath,"wb") do |io|
274
- io.write response[:content]
275
- end
276
- puts "saved:".green.bold + "/Users/seijiro/Desktop/#{@a[:title]}.mp4"
277
- elsif response[:status] == 302
278
- puts "302".red.bold
279
- location = ""
280
- response[:headers].each do |elem|
281
- p elem
282
- location = $1 if elem =~ /Location:\s(.*)/
283
- end
284
- job = MyJobAnisoku.new(
285
- :url => location,
286
- :title => @a[:title],
287
- :status => :video,
288
- :machine => @a[:machine]
289
- )
290
- @a[:machine].retry job
291
- else
292
- puts response[:status].to_s.red.bold
293
- puts response[:headers].to_s.red.bold
294
- # raise "HTTP Status Error"
295
- end
296
- }
297
- =end
298
- end
299
-
300
- # run in thread
301
- def run
302
- t = Thread.new do
303
- case @a[:status]
304
- when :new then
305
- tokkakari
306
- when :second then
307
- second
308
- when :kobetu then
309
- kobetu
310
- when :third then
311
- third
312
- when :video then
313
- video
314
- end
315
- end
316
- end
317
-
318
- end
319
-
320
3
  #
321
- # Module of Machine
4
+ # 俺俺な典型的な動作を提供する
322
5
  #
323
- # this class has queue of jobs.controll jobs and run jobs
324
- module MyMachine
325
-
326
- Version = "0.0.1"
327
-
328
- attr_accessor :queue
329
-
330
- def initialize
331
- require 'thread'
332
- @queue = Queue.new
333
- end
334
-
335
- def setup
336
- setupjobs
337
- setupmachine
338
- end
339
-
340
- def go
341
- puts "You need write the go method!"
342
- end
343
-
344
- def retry(job)
345
- @queue.push job
346
- end
347
-
348
- private
349
-
350
- def setupjobs
351
- puts "You need write the setupjobs method!"
352
- end
353
-
354
- def setupmachine
355
- puts "You need write the setupmachine method!"
356
- end
357
-
358
- end
359
-
6
+ # こんな感じで使うと良い
360
7
  #
361
- # Class of Machine by EventMachine.
362
- # this class controll jobs for Anisoku
363
- # @example
364
- # machine = MyMachineAnisoku.new("YourVideoSaveDir")
365
- # machine.setup
366
- # machine.go
8
+ # require "/Users/seijiro/code/ruby/my-lib.rb"
9
+ # class This < MyObject
10
+ # include RunPerSecModule
11
+ # include MyDBModule
12
+ # include MyPusherModule
13
+ # .....
14
+ #
15
+ # end
367
16
  #
368
- class MyMachineAnisoku
369
- include MyMachine
370
-
371
- Version = "0.0.1"
372
-
373
- # directory of save video files default "#{ENV['HOME']}/Desktop/video"
374
- attr_accessor :savedir
375
-
376
- # set video save dir
377
- # @param [Hash] args
378
- # @option args [String] :savedir save dir
379
- # default "#{ENV['HOME']}/Desktop/video"
380
- def initialize(args={ })
381
- super()
382
- args[:savedir] ||= "#{ENV['HOME']}/Desktop/video"
383
- @savedir = args[:savedir]
384
- begin
385
- Dir::mkdir(@savedir, 0777)
386
- rescue => ex
387
- warn ex
388
- end
389
- require 'rubygems'
390
- require 'eventmachine'
391
- # @gaman controll eventmachine end
392
- @gaman = 0
393
- end
394
-
395
- # machine go to run eventmachine
396
- def go
397
- EM.run do
398
- EM.add_periodic_timer(0.00001) do
399
- # print "loop".green
400
- if should_stop_machine?
401
- finalize_files
402
- EM.stop
403
- end
404
- @queue.pop.run unless @queue.empty?
405
- end
406
- end
407
- puts "End of fetch".green.bold
408
- end
409
-
410
- private
411
-
412
- # delete tiny fail files
413
- def finalize_files
414
- delete_entries = []
415
- Dir.entries(@savedir).select!{ |entry|
416
- !File.directory?(entry) &&
417
- File.size("#{ @savedir }/#{entry}") < 1024 * 1024 * 2
418
- }.each do |entry|
419
- # p File.size("#{ @savedir }/#{entry}")
420
- # p ("#{@savedir }/#{entry}")
421
- File.delete("#{@savedir }/#{entry}")
422
- end
423
- end
424
-
425
-
426
- # setup jobs
427
- def setupjobs
428
- ajob = MyJobAnisoku.new(
429
- :machine => self
430
- )
431
- @queue.push ajob
432
- end
433
17
 
434
- # should stop machine or not
435
- def should_stop_machine?
436
- @gaman += 1 if @queue.empty?
437
- print @gaman
438
- return @queue.empty? && @gaman > 1500
439
- end
18
+ # make String Colored
19
+ require 'rubygems'
20
+ require 'term/ansicolor'
21
+ class String
22
+ include Term::ANSIColor
440
23
  end
441
24
 
442
-
25
+ # MyObject
26
+ require 'lib/myobject.rb'
27
+ # MyConfig
28
+ require 'lib/myconfig.rb'
29
+ # MyLogger
30
+ require 'lib/mylogger.rb'
31
+ # MyMachine
32
+ require 'lib/machine.rb'
33
+ # MyJobAnisoku
34
+ require 'lib/anisoku.rb'
35
+ # RunPerSecModule
36
+ require 'lib/runpersec.rb'
37
+ # MyDBModule
38
+ require 'lib/mydb.rb'
39
+ # MyPusherModule
40
+ require 'lib/mypusher.rb'
41
+ # MyGCalModule
42
+ require 'lib/mygcal.rb'
43
+ # MyAtModule
44
+ require 'lib/myat.rb'