Module: Get

Defined in:
lib/nehm/get.rb

Overview

TrackUtils module responds to ‘nehm get/dl …’ commands

Class Method Summary collapse

Class Method Details

.[](get_or_dl, args) ⇒ Object



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
# File 'lib/nehm/get.rb', line 6

def self.[](get_or_dl, args)
  user =
    # If option 'from ...' typed
    if args.include? 'from'
      index = args.index('from')
      permalink = args[index + 1]

      args.delete_at(index + 1)
      args.delete_at(index)
      UserControl.user(permalink)
    else
      UserControl.default_user
    end

  # If option 'to ...' typed
  if args.include? 'to'
    index = args.index('to')
    path = args[index + 1]

    path = PathControl.tilde_to_home(path) if PathControl.tilde_at_top?(path)

    PathControl.temp_dl_path = path

    args.delete_at(index + 1)
    args.delete_at(index)
  end

  tracks = []
  tracks +=
    case args.last
    when 'like'
      user.likes(1)

    when 'post'
      user.posts(1)

    when 'likes'
      count = args[-2].to_i
      user.likes(count)

    when 'posts'
      count = args[-2].to_i
      user.posts(count)

    when %r{https:\/\/soundcloud.com\/}
      track_from_url(args.last)

    else
      puts Paint['Invalid argument(s)', :red]
      puts "Input #{Paint['nehm help', :yellow]} for help"
      exit
    end

  tracks.each do |track|
    dl(track)
    dl(track.artwork)
    tag(track)
    cp(track) unless (get_or_dl == :dl) || (OS.linux?)
    track.artwork.suicide
  end
  puts Paint['Done!', :green]
end

.cp(track) ⇒ Object



105
106
107
108
# File 'lib/nehm/get.rb', line 105

def cp(track)
  puts 'Adding to iTunes library'
  FileUtils.cp(track.file_path, PathControl.itunes_path)
end

.dl(arg) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/nehm/get.rb', line 76

def dl(arg)
  puts 'Downloading ' + arg.name
  path = arg.file_path
  url = arg.url
  command = "curl -# -o '" + path + "' -L " + url
  system(command)
end

.tag(track) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nehm/get.rb', line 84

def tag(track)
  path = track.file_path
  TagLib::MPEG::File.open(path) do |file|
    puts 'Setting tags'
    tag = file.id3v2_tag
    tag.artist = track.artist
    tag.title = track.title
    tag.year = track.year

    # Adding artwork
    apic = TagLib::ID3v2::AttachedPictureFrame.new
    apic.mime_type = 'image/jpeg'
    apic.description = 'Cover'
    apic.type = TagLib::ID3v2::AttachedPictureFrame::FrontCover
    apic.picture = File.open(track.artwork.file_path, 'rb') { |f| f.read }
    tag.add_frame(apic)

    file.save
  end
end

.track_from_url(url) ⇒ Object



71
72
73
74
# File 'lib/nehm/get.rb', line 71

def track_from_url(url)
  hash = Client.get('/resolve', url: url)
  [*Track.new(hash)]
end