Test Failed
Push — master ( 1ba02b...9a0e4e )
by Sascha
01:50
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
    else
41
      puts 'Checking if http URL is valid'.color(:green)
42
      http_url_valid?(url)
43
    end
44
  end
45
46
  # Method to check https
47
  # @param [String] url Is the given URL to the Youtube file
48
  def self.https_url_valid?(url)
49
    # @param [String] url Is the given URL to the Youtube file
50
    uri = URI.parse(url)
51
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
52
      http.head(uri.path)
53
    end
54
    response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
55
  end
56
57
  # Method to check http
58
  # @param [String] url Is the given URL to the Youtube file
59
  def self.http_url_valid?(url)
60
    # @param [String] url Is the given URL to the Youtube file
61
    uri = URI.parse(url)
62
    response = Net::HTTP.start(uri.host, uri.port) do |http|
63
      http.head(uri.path)
64
    end
65
    response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPRedirection)
66
  end
67
68
  # Ask for names, creates the folders and puts all into a $folder variable
69
  # This method smells of :reek:TooManyStatements
70
  # @return [String] directory
71
  def self.check_target
72
    entry = ask 'What kind of entry do you have? (Interpret or Group)'
73
74
    subdir = case entry
75
             when 'Interpret'
76
               [
77
                 ask('Whats the surname of your interpret?'),
78
                 ask('Whats the first name of your interpret?')
79
               ].join('_')
80
81
             when 'Group'
82
               ask 'Whats the name of the group?'
83
84
             else
85
               puts 'Just the entries "Interpret" or "Group" are allowed'.color(:red)
86
               abort('Aborted')
87
             end
88
    subdir.tr!(' ', '_')
89
    directory = "#{subdir}/Youtube-Music"
90
    return directory
91
  end
92
93
  # Checks if the target directory are present. If not, it creates one
94
  # @param [String] music_dir Path to the music directory
95
  # @param [String] directory Path to the target folder
96
  def self.check_dir(music_dir, directory)
97
    # Checking if music dir exists
98
    if Dir.exist?("#{music_dir}/#{directory}")
99
      puts 'Found directory. Im using it.'.color(:green)
100
    else
101
      puts 'No directory found. Im creating it.'.color(:green)
102
      # Creates the new directory in $music_dir/$folder
103
      FileUtils.mkdir_p("#{music_dir}/#{directory}")
104
      puts 'Created new directory...'.color(:green) if Dir.exist?("#{music_dir}/#{directory}")
105
    end
106
  end
107
108
  # This method checks if a oldconfig is available
109
  # @return [String] true or false
110
  def self.oldconfig_exists?
111
    sys_xdg = XDG['CONFIG_HOME']
112
    sysconf_dir = "#{sys_xdg}/youtube_dlhelper"
113
    if File.exist?("#{sysconf_dir}/youtube_dlhelper.conf")
114
      puts 'Found configuration file and using it...'.color(:green)
115 View Code Duplication
    else
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
116
      # @raise
117
      puts 'Please run rake setup'.color(:red)
118
      raise('Exiting now..').color(:red)
119
    end
120
  end
121
122
  # This method checks which decoder is available
123
  # This method smells of :reek:TooManyStatements
124
  # @return [String] ffmpeg_binary
125
  def self.ffmpeg_binary
126
    get_ffmpeg = `which ffmpeg`
127
    ffmpeg = p get_ffmpeg.chomp
128
    ffmpeg_binary = ffmpeg if ffmpeg != ''
129
    return ffmpeg_binary
130
  end
131
132
  # Cleaner method for unneeded files
133
  # @param [String] filename The name of the new produced file
134
  def self.cleanup
135
    puts 'Cleaning up directory'.color(:green)
136
    # @note Cleanup the temp files
137
    formats = %w[*.mp4 *.m4a *.webm *.opus *.mkv] # changed normale klammer to eckige
138
    formats.each do |format|
139
      Dir.glob(format).each do |file|
140
        File.delete(file) if File.exist?(file)
141
      end
142
    end
143
144
    puts 'Finished cleaning up'.color(:green)
145
  end
146
end
147