|
1
|
|
|
# Youtube converter checker class |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2013-2018 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
|
|
|
# @param [String] url Is the given URL to the Youtube file |
|
33
|
|
|
# @return [String] url |
|
34
|
|
|
def self.external_url_is_valid?(url) |
|
35
|
|
|
puts 'Checking prefix'.color(:green) |
|
36
|
|
|
if url.include? 'https' |
|
37
|
|
|
puts 'Checking if https URL is valid'.color(:green) |
|
38
|
|
|
https_url_valid?(url) |
|
39
|
|
|
else |
|
40
|
|
|
puts 'Checking if http URL is valid'.color(:green) |
|
41
|
|
|
http_url_valid?(url) |
|
42
|
|
|
end |
|
43
|
|
|
return url |
|
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 |https| |
|
52
|
|
|
https.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 config is available |
|
109
|
|
|
# @return [String] true or false |
|
110
|
|
|
def self.config_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
|
|
|
else |
|
116
|
|
|
puts 'Please run rake setup'.color(:red) |
|
117
|
|
|
raise('Exiting now..').color(:red) |
|
118
|
|
|
end |
|
119
|
|
|
end |
|
120
|
|
|
|
|
121
|
|
|
# This method checks which decoder is available |
|
122
|
|
|
# @return [String] ffmpeg_binary |
|
123
|
|
|
def self.ffmpeg_binary |
|
124
|
|
|
get_ffmpeg = `which ffmpeg` |
|
125
|
|
|
ffmpeg = p get_ffmpeg.chomp |
|
126
|
|
|
ffmpeg_binary = ffmpeg if ffmpeg != '' |
|
127
|
|
|
return ffmpeg_binary |
|
128
|
|
|
end |
|
129
|
|
|
|
|
130
|
|
|
# Cleaner method for unneeded files |
|
131
|
|
|
# @param [String] filename The name of the new produced file |
|
132
|
|
|
def self.cleanup |
|
133
|
|
|
puts 'Cleaning up directory'.color(:green) |
|
134
|
|
|
# @note Cleanup the temp files |
|
135
|
|
|
formats = %w[*.mp4 *.m4a *.webm *.opus *.mkv] |
|
136
|
|
|
formats.each do |format| |
|
137
|
|
|
Dir.glob(format).each do |file| |
|
138
|
|
|
File.delete(file) if File.exist?(file) |
|
139
|
|
|
end |
|
140
|
|
|
end |
|
141
|
|
|
|
|
142
|
|
|
puts 'Finished cleaning up'.color(:green) |
|
143
|
|
|
end |
|
144
|
|
|
end |
|
145
|
|
|
|