Test Failed
Push — master ( 992577...a792d9 )
by Sascha
01:23
created

Checker.get_ffmpeg_binary()   A

Complexity

Conditions 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
1
# Youtube converter checker class
2
#
3
# Copyright (C) 2013-2017 Sascha Manns <[email protected]>
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
# Class Documentation:
19
# http://www.rubydoc.info/github/saigkill/youtube_dlhelper/Checker
20
21
# Dependencies
22
require 'rainbow/ext/string'
23
require 'fileutils'
24
require 'highline/import'
25
require 'net/http'
26
require 'uri'
27
require 'xdg'
28
29
# The Checker module contains different methods to check anything
30
module Checker
31
  # This method checks if a url is valid
32
  # This method smells of :reek:TooManyStatements
33
  # @param [String] url Is the given URL to the Youtube file
34
  # @return [String] url
35
  def self.external_url_is_valid?(url)
36
    puts 'Checking prefix'.color(:green)
37
    if url.include? 'https'
38
      puts 'Checking if https URL is valid'.color(:green)
39
      https_url_valid?(url)
40
      # return url
41
    else
42
      puts 'Checking if http URL is valid'.color(:green)
43
      http_url_valid?(url)
44
      # return url
45
    end
46
  end
47
48
  # Method to check https
49
  # @param [String] url Is the given URL to the Youtube file
50
  def self.https_url_valid?(url)
51
    # @param [String] url Is the given URL to the Youtube file
52
    uri = URI.parse(url)
53
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
54
      http.head(uri.path)
55
    end
56
    response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
57
  end
58
59
  # Method to check http
60
  # @param [String] url Is the given URL to the Youtube file
61
  def self.http_url_valid?(url)
62
    # @param [String] url Is the given URL to the Youtube file
63
    uri = URI.parse(url)
64
    response = Net::HTTP.start(uri.host, uri.port) do |http|
65
      http.head(uri.path)
66
    end
67
    response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
68
  end
69
70
  # Ask for names, creates the folders and puts all into a $folder variable
71
  # This method smells of :reek:TooManyStatements
72
  # @return [String] folder
73
  def self.check_target
74
    entry = ask 'What kind of entry do you have? (Interpret or Group)'
75
76
    subdir = case entry
77
             when 'Interpret'
78
               [
79
                 ask('Whats the surname of your interpret?'),
80
                 ask('Whats the first name of your interpret?')
81
               ].join('_')
82
83
             when 'Group'
84
               ask 'Whats the name of the group?'
85
86
             else
87
               puts 'Just the entries "Interpret" or "Group" are allowed'.color(:red)
88
               abort('Aborted')
89
             end
90
    subdir.tr!(' ', '_')
91
    directory = "#{subdir}/Youtube-Music"
92
    return directory
93
  end
94
95
  # Checks if the target directory are present. If not, it creates one
96
  # @param [String] music_dir Path to the music directory
97
  # @param [String] directory Path to the target folder
98
  def self.check_dir(music_dir, directory)
99
    # @note Checking if music dir exists
100
    if Dir.exist?("#{music_dir}/#{directory}")
101
      puts 'Found directory. Im using it.'.color(:green)
102
    else
103
      puts 'No directory found. Im creating it.'.color(:green)
104
      # @note Creates the new directory in $music_dir/$folder
105
      FileUtils.mkdir_p("#{music_dir}/#{directory}")
106
      puts 'Created new directory...'.color(:green) if Dir.exist?("#{music_dir}/#{directory}")
107
    end
108
  end
109
110
  # This method checks if a oldconfig is available
111
  # @return [String] true or false
112
  def self.oldconfig_exists?
113
    sys_xdg = XDG['CONFIG_HOME']
114
    sysconf_dir = "#{sys_xdg}/youtube_dlhelper"
115 View Code Duplication
    if File.exist?("#{sysconf_dir}/youtube_dlhelper.conf")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
116
      puts 'Found configuration file and using it...'.color(:green)
117
    else
118
      # @raise
119
      puts 'Please run rake setup'.color(:red)
120
      raise('Exiting now..').color(:red)
121
    end
122
  end
123
124
  # This method checks which decoder is available
125
  # This method smells of :reek:TooManyStatements
126
  # @return [String] ffmpeg_binary
127
  def self.get_ffmpeg_binary
128
    get_ffmpeg = `which ffmpeg`
129
    ffmpeg = p get_ffmpeg.chomp
130
    ffmpeg_binary = ffmpeg if ffmpeg != ''
131
    return ffmpeg_binary
132
  end
133
134
  # Cleaner method for unneeded files
135
  # @param [String] filename The name of the new produced file
136
  def self.cleanup
137
    puts 'Cleaning up directory'.color(:green)
138
    # @note Cleanup the temp files
139
    formats = %w(*.mp4 *.m4a *.webm *.opus *.mkv)
140
    formats.each do |format|
141
      Dir.glob(format).each do |file|
142
        File.delete(file) if File.exist?(file)
143
      end
144
    end
145
146
    puts 'Finished cleaning up'.color(:green)
147
  end
148
end
149