Module: Mvtk

Defined in:
lib/mvtk.rb,
lib/mvtk/menu.rb,
lib/mvtk/config.rb,
lib/mvtk/search.rb,
lib/mvtk/proceed.rb,
lib/mvtk/version.rb,
lib/mvtk/namecleaner.rb

Defined Under Namespace

Classes: NameCleaner

Constant Summary collapse

VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

.choosescrap(mfile) ⇒ Object



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
# File 'lib/mvtk/menu.rb', line 33

def self.choosescrap(mfile)
  mysearch = File::basename(self::NameCleaner.new(mfile).clean)
  search = []
  while search.empty? and mysearch != nil
    search = self.search(mysearch)
    if search.empty? then
      mysearch = mysearch[/(.*)\s/,1]
    end
  end

  if search.empty? then
    return self.manualscrap
  end

  foutput = search.collect { |x| x.split('/')[0]}
  foutput.push("[Manual] (Do a manual search)")
  foutput.push("[Return] (Back to file menu)")
  prompt2 = TTY::Prompt.new
  choice = prompt2.select("Your choice ?", foutput)
  if choice == "[Manual] (Do a manual search)" then
    return self.manualscrap
  end
  result = search.keep_if { |v| v.start_with?(choice) }
  return result.first
end

.confcreate(conffilepath) ⇒ Object

create the config



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mvtk/config.rb', line 6

def self.confcreate (conffilepath)
  doc = IniParse.gen do |doc|
    doc.section("mvtk") do |mvtk|
      mvtk.option("scraper","mpdb")
      mvtk.option("saga_enable","true")
      mvtk.option("saga_prefix","1-Saga")
      mvtk.option("source","/home/torrent/finish.seedbox")
      mvtk.option("target","/home/data/Videos/Films")
      mvtk.option("windows_name","true")
      mvtk.option("min_movie_time","60")
      mvtk.option("year_in_filename","false")
      mvtk.option("move_files","false")
    end
  end
  doc.save(conffilepath)
  puts "Conf file created : #{conffilepath}"
  puts "Edit to fit your need then run again"
  #Add exit
  exit
end

.confread(conffilepath) ⇒ Object

Read the config



28
29
30
31
# File 'lib/mvtk/config.rb', line 28

def self.confread (conffilepath)
  confcreate(conffilepath) unless File.exists?(conffilepath)
  IniParse.parse( File.read(conffilepath) )['mvtk']
end

.copy(source, destination) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mvtk/proceed.rb', line 55

def self.copy(source, destination)
  finalpath = self.fulldest(source, destination)
  if finalpath.nil?
    return
  end
  puts "Source => #{File::basename(source)}"
  puts "Target => #{finalpath.sub($conf["target"]+"/","")}"
  puts "Size   => #{Filesize.from(File.size(source).to_s + "B").pretty}"
  puts ""

  FileUtils.mkdir_p File.dirname(finalpath) unless File.directory?(File.dirname(finalpath))
  self.copy_progressbar(source, finalpath)
end

.copy_progressbar(in_name, out_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mvtk/proceed.rb', line 33

def self.copy_progressbar(in_name, out_name)
  in_file = File.new(in_name, "r")
  out_file = File.new(out_name, "w")

  in_size = File.size(in_name)
  batch_bytes = ( in_size / 100 ).ceil
  total = 0
  p_bar = ProgressBar.create(:length => 80, :format => '%e |%b>%i| %p%% %t')

  buffer = in_file.sysread(batch_bytes)
  while total < in_size do
    out_file.syswrite(buffer)
    p_bar.progress += 1 unless p_bar.progress >= 100
    total += batch_bytes
    if (in_size - total) < batch_bytes
      batch_bytes = (in_size - total)
    end
    buffer = in_file.sysread(batch_bytes)
  end
  p_bar.finish
end

.fulldest(source, destination) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mvtk/proceed.rb', line 12

def self.fulldest(source, destination)
  if destination.nil?
      return nil
  end
  movie_file = "#{destination.split('/')[0][0..-8]}#{File.extname(source)}"
  movie_file = "#{destination.split('/')[0]}#{File.extname(source)}" if $conf["year_in_filename"]
  movie_file = self.winname(movie_file) if $conf["windows_name"]
  movie_dir = destination.split('/')[0]
  movie_dir = self.winname(movie_dir) if $conf["windows_name"]

  if $conf["saga_enable"] and destination.split('/')[1] then
    saga = "#{$conf["saga_prefix"]} #{destination.split('/')[1]}"
    saga = self.winname(saga) if $conf["windows_name"]
    fullpath = "#{$conf["target"]}/#{saga}/#{movie_dir}/#{movie_file}"
  else
    fullpath = "#{$conf["target"]}/#{movie_dir}/#{movie_file}"
  end

  return fullpath
end

.listfilesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mvtk/menu.rb', line 12

def self.listfiles
  result = Hash.new
  Dir.glob("#{$conf['source']}/**/*.{mkv,mp4,avi}", File::FNM_CASEFOLD).sort.each do |mfile|
    begin
      movie = FFMPEG::Movie.new(mfile)
      next if movie.duration.round/60.to_i < $conf['min_movie_time']
    rescue
    end
    result[File::basename(mfile)] = mfile
  end
  result["[Proceed]"] = "proceed"
  result["[Quit]"] = "quit"
  return result
end

.manualscrapObject



27
28
29
30
31
# File 'lib/mvtk/menu.rb', line 27

def self.manualscrap
  prompt = TTY::Prompt.new
  answer = prompt.ask("Enter your search terms : ")
  return self.choosescrap(answer)
end

.mediamenuObject



6
7
8
9
10
# File 'lib/mvtk/menu.rb', line 6

def self.mediamenu
  prompt = TTY::Prompt.new
  choices = self.listfiles
  choice = prompt.select("Movie to copy ?", choices)
end

.mediapassion(moviename) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/mvtk/search.rb', line 7

def self.mediapassion(moviename)
  result = []
  searchresult = Nokogiri::HTML(open("http://passion-xbmc.org/scraper/ajax.php?Ajax=Search&Title=#{URI.escape(moviename.tr(' ', '+'))}"), nil , 'UTF-8')
  searchresult.css('a').take(10).each do |link|
    mtitle = link.content.tr('[', '(').tr(']', ')')
    detailpage = Nokogiri::HTML(open("http://passion-xbmc.org/scraper/#{link['href']}"))
    msaga = detailpage.css("#sagaWrapper").css('p').text[5..-1]
    unless msaga.nil? then
      result.push("#{mtitle}/#{msaga.rstrip.lstrip}")
    else
      result.push("#{mtitle}/")
    end
  end
  return result
end

.move(source, destination) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mvtk/proceed.rb', line 69

def self.move(source, destination)
    finalpath = self.fulldest(source, destination)
    if finalpath.nil?
        return
    end
    finaldir = finalpath.split('/')[0..-2].join('/')
    sourcedir = File.dirname(source)

    FileUtils.mkdir_p finaldir
    FileUtils.mv(source, finalpath)

    todelete = Pathname.new(sourcedir).cleanpath
    sourcefiles = Pathname.new($conf["source"]).cleanpath

    unless todelete == sourcefiles
      FileUtils.rm_rf(todelete)
    end
end

.mpdb(moviename) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mvtk/search.rb', line 41

def self.mpdb(moviename)
  result = []
  searchresult = Nokogiri::HTML(open("http://mpdb.tv/site/search?q=#{URI.escape(moviename.tr(' ', '+'))}"), nil , 'UTF-8')
  searchresult.css('div.item a').take(10).each do |link|
    msaga = nil
    mtitle = link.css('h4.list-group-item-heading').text.strip
    detailpage = Nokogiri::HTML(open("http://mpdb.tv#{link['href']}"))
    begin
      havehref = detailpage.css('p a').first['href']
      havesaga = true
      havesaga = nil unless havehref.start_with?('/saga/')
    rescue
      havesaga = nil
    end
    msaga = detailpage.css('p').first.text.strip if havesaga
    unless msaga.nil? then
      result.push("#{mtitle}/#{msaga}")
    else
      result.push("#{mtitle}/")
    end
  end
  return result
end

.search(moviename) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mvtk/search.rb', line 65

def self.search (moviename)
  if $conf['scraper'] == "mediapassion" then
    result = self.mediapassion(moviename)
  elsif $conf['scraper'] == "themoviedb" then
    result = self.themoviedb(moviename)
  elsif $conf['scraper'] == "mpdb" then
    result = self.mpdb(moviename)
  else
    puts "Sorry #{$conf['scraper']} is not a valid scraper"
    exit 1
  end
  return result
end

.themoviedb(moviename) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mvtk/search.rb', line 23

def self.themoviedb(moviename)
  result = []
  searchresult = Nokogiri::HTML(open("https://www.themoviedb.org/search?query=#{URI.escape(moviename.tr(' ', '+'))}"), nil , 'UTF-8')
  searchresult.css("div.info").take(10).each do |link|
    msaga = nil
    myear = link.css('span.release_date').text.split("-")[0].strip
    mtitle = "#{link.css('a').first.content} (#{myear})"
    detailpage = Nokogiri::HTML(open("https://www.themoviedb.org#{link.css('a').first['href']}"))
    msaga = detailpage.at('p:contains("Part of the")').css('a').text if detailpage.at('p:contains("Part of the")')
    unless msaga.nil? then
      result.push("#{mtitle}/#{msaga}")
    else
      result.push("#{mtitle}/")
    end
  end
  return result
end

.winname(fullpath) ⇒ Object



7
8
9
10
# File 'lib/mvtk/proceed.rb', line 7

def self.winname(fullpath)
  charfilter = /[\<\>\:\"\|\?\*]/
  return fullpath.gsub(charfilter,'_')
end