Class: P3::TV::Downloads

Inherits:
Object
  • Object
show all
Defined in:
lib/p3-tv.rb

Constant Summary collapse

REGEX =

s1e2, s01e02, S1E02, S01E2

[ /[sS](\d{1,2})[eE](\d{1,2})/, #s1e2, s01e02, S1E02, S01E2
  /(\d{1,2})x(\d{1,2})/ #1x2, 01x2, 1x02, 01x02
]

Instance Method Summary collapse

Constructor Details

#initialize(settings = Settings.new) ⇒ Downloads

Returns a new instance of Downloads.



352
353
354
355
356
357
# File 'lib/p3-tv.rb', line 352

def initialize( settings = Settings.new )
    @settings = settings
    @transmission = nil
    @paths = nil
    @torrents = nil
end

Instance Method Details

#create_episode_from_filename(path) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
# File 'lib/p3-tv.rb', line 392

def create_episode_from_filename( path )
    e = nil
    path_match( path ) do | series, match_data |
        e = EpisodeFile.new
        e.series_id = series[:id]
        e.series = series[:name]
        e.season = match_data[1].to_i
        e.episode = match_data[ 2 ].to_i
        e.path = path
    end
    return e
end

#create_episode_from_filename_series(path, seriesid, series_name) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/p3-tv.rb', line 379

def create_episode_from_filename_series( path, seriesid, series_name )
    e = nil
    path_match_series( path, series_name ) do | match_data |
        e = EpisodeFile.new
        e.series_id = seriesid
        e.series = series_name
        e.season = match_data[1].to_i
        e.episode = match_data[ 2 ].to_i
        e.path = path
    end
    return e
end

#download!(path) ⇒ Object



438
439
440
441
442
443
444
445
446
# File 'lib/p3-tv.rb', line 438

def download!( path )
    if transmission()
        transmission().create( path )
    else
        cmd = "open #{path}"
        cmd = "xdg-#{cmd} 2>/dev/null" if Gem::Platform::local::os == "linux"
        system( cmd )
    end
end

#download_episode_file!(episode_file) ⇒ Object



448
449
450
451
452
453
454
455
456
# File 'lib/p3-tv.rb', line 448

def download_episode_file!( episode_file )
    if( episode_file.status == :available )
        magnet_link = episode_file.path
        puts magnet_link if @settings[:verbose]
        unless @settings[:dry_run]
            download!( magnet_link )
        end
    end
end

#each_downloading_fileObject



427
428
429
430
431
432
433
434
435
436
# File 'lib/p3-tv.rb', line 427

def each_downloading_file
    torrents().each do |torrent|
        episode_file = create_episode_from_filename( torrent['name'] )
        if( episode_file )
            episode_file.status = :downloading
            episode_file.percent_done = torrent['percentDone']
            yield( episode_file )
        end
    end
end

#each_episode_fileObject



415
416
417
418
419
420
421
422
423
424
425
# File 'lib/p3-tv.rb', line 415

def each_episode_file
    episode_files = paths().collect{|path| create_episode_from_filename( path ) }
    episode_files.each do | episode_file |
        if episode_file
            episode_file.status = :downloaded
            episode_file.percent_done = 1
            yield( episode_file )
        end
    end

end

#each_episode_file_in_series(seriesid) ⇒ Object



405
406
407
408
409
410
411
412
413
# File 'lib/p3-tv.rb', line 405

def each_episode_file_in_series( seriesid )
    series = @settings.get_series( seriesid )
    if( series )
        episode_files = paths().collect{|path| create_episode_from_filename_series( path, series[:id], series[:name] ) }
        episode_files.each do | episode_file |
            yield( episode_file ) if episode_file
        end
    end
end

#get_path_if_exists(episode_file) ⇒ Object



502
503
504
505
506
507
508
509
510
511
# File 'lib/p3-tv.rb', line 502

def get_path_if_exists( episode_file )
    episode_files = paths().collect{|p| create_episode_from_filename_series( p, episode_file.series_id, episode_file.series ) }
    episode_files.select!{|ef| ef }
    episode_files.each do | dn_ep | #download_episode_file
        if( 0 == ( episode_file <=> dn_ep ) )
            return dn_ep.path
        end
    end
    return nil
end

#get_torrent_if_exists(episode_file) ⇒ Object



513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/p3-tv.rb', line 513

def get_torrent_if_exists( episode_file )
    torrents().each do | torrent |
        name = torrent['name']
        torrent_episode = create_episode_from_filename_series( name, episode_file.series_id, episode_file.series )
        if( torrent_episode )
            if( 0 == ( episode_file <=> torrent_episode ) )
                return torrent
            end
        end
    end
    return nil
end

#path_match(path) ⇒ Object



370
371
372
373
374
375
376
377
# File 'lib/p3-tv.rb', line 370

def path_match( path )
    @settings[:series].each do | series |
        path_match_series( path, series[:name] ) do | match_data |
            yield( series, match_data )
            return
        end
    end
end

#path_match_series(path, series_name) ⇒ Object



359
360
361
362
363
364
365
366
367
368
# File 'lib/p3-tv.rb', line 359

def path_match_series( path, series_name )
    return unless( P3::TV::path_contains_series?( path, series_name ) )
    REGEX.each do | regex |
        match_data = path.match( regex )
        if( match_data )
            yield( match_data )
            return
        end
    end
end

#pathsObject



458
459
460
461
462
463
464
# File 'lib/p3-tv.rb', line 458

def paths
    return @paths if @paths
    glob = File::join( @settings[:download_path], '**/*' )
    @paths = Dir::glob( glob )
    @paths = @paths.select{|p| @settings.allowed_type?( p ) }
    return @paths
end

#remove_completed_torrents!Object



491
492
493
494
495
496
497
498
499
500
# File 'lib/p3-tv.rb', line 491

def remove_completed_torrents!
    count = 0
    torrents().each do | torrent |
        count += 1
        transmission().remove( torrent['id'] ) if torrent['percentDone'] == 1
    end

    torrents().reject!{ | torrent | torrent['percentDone'] == 1 }
    return count
end

#torrentsObject



482
483
484
485
486
487
488
489
# File 'lib/p3-tv.rb', line 482

def torrents
    @torrents = [] unless transmission()
    unless @torrents
        @torrents = transmission().all
    end

    return @torrents
end

#transmissionObject



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/p3-tv.rb', line 466

def transmission

    unless @transmission
        config = {}
        [:transmission_username, :transmission_password, :transmission_host, :transmission_port ].each do | transmission_key |
            return @transmission unless @settings[ transmission_key ]
            key = transmission_key.to_s.gsub('transmission_','').to_sym
            config[ key ] = @settings[ transmission_key ]
        end

        @transmission = P3::Transmission::Client.new( config )
    end

    return @transmission
end