-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDownloader.rb
More file actions
293 lines (227 loc) · 7.43 KB
/
Downloader.rb
File metadata and controls
293 lines (227 loc) · 7.43 KB
1
2
3
4
5
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# encoding: UTF-8
require 'fileutils'
require 'open-uri'
require 'mediawiki_api'
require 'faraday'
#require 'openssl'
# In case if you have problems with SSL verification, uncomment first and last
# lines of this message. Use this SSL-solve method on your own risk!
#OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
module RubyDownloader
PATH = File.dirname(__FILE__)
CFG_PATH = PATH + '/config.json'
I18N_PATH = PATH + '/i18n.json'
ERR_PATH = PATH + '/errors.log'
FILE_PATH = PATH + '/Files'
class << self
attr_accessor :cfg, :msg
def init
puts "Loading..."
# Running file check system
unless File.exists?( CFG_PATH )
puts "Config file was not found. Creating new one in downloader folder."
File.open( CFG_PATH, 'w+' ) {|f| f.write( JSON.pretty_generate({
'nickname' => 'Your nickname here',
'password' => 'Your password here',
'script_lang' => 'en',
'wikiname' => {
'from' => 'https://community.fandom.com/',
'to' => 'https://community.fandom.com/ru/',
'page' => ''
},
'list' => 'filelist.txt',
'comment' => 'Uploading image',
'text' => '',
'direct' => false
}))}
puts "Done! Fill it up and run again!"
quit
end
# Load i18n file
unless File.exists?( I18N_PATH )
puts "Downloading i18n"
open( I18N_PATH, 'wb' ) do | f |
f << open( "https://raw.githubusercontent.com/Kopcap94/RubyWikiDownloader/master/i18n.json" ).read
end
end
# Check folder
unless File.directory?( FILE_PATH )
FileUtils.mkdir_p( FILE_PATH )
end
load_cfg
unless File.exists?( PATH + '/' + @cfg[ 'list' ] )
puts @msg[ 'list-not-exist' ]
quit
end
menu( false, '' )
end
def load_cfg
# Load config and i18n file
@cfg = JSON.parse( File.read( CFG_PATH ) )
i18n = JSON.parse( File.read( I18N_PATH ) )
lang = @cfg[ 'script_lang' ]
@msg = i18n[ 'langs' ].include?( lang ) ? i18n[ lang ] : i18n[ 'en' ]
end
def menu( s, m )
cls
puts ( s ? "#{ m }\n\n" : "" ) + @msg[ 'menu' ] +
"\n[ 1 ] " + @msg[ 'menu-program' ] +
"\n[ 2 ] " + @msg[ 'menu-helper' ] +
"\n[ 3 ] " + @msg[ 'menu-reload' ] +
"\n" + @msg[ 'menu-select' ]
opt = gets.chomp.to_i
case opt
when 1
_t = RubyDownloader::Main.new( self )
when 2
_t = RubyDownloader::Helper.new( self )
when 3
load_cfg
menu( true, @msg[ 'menu-reload-done' ] )
else
menu( true, @msg[ 'menu-wrong-input' ] )
end
_t = _t.start
end
def cls
Gem.win_platform? ? ( system "cls" ) : ( system "clear" )
end
def main
puts @msg[ 'menu-return' ]
gets
menu( false, '' )
end
def quit
puts "Press enter to exit"
gets
exit
end
end
class Main
def initialize( s )
@cfg = s.cfg
@msg = s.msg
file = PATH + '/' + @cfg[ 'list' ]
if File.zero?( file ) then
puts @msg[ 'list-is-empty' ]
RubyDownloader.quit
end
@filelist = File.read( file ).split( /\n/ )
log_in
end
def log_in
RubyDownloader.cls
# Logging on wiki
@c_from = MediawikiApi::Client.new( "#{ @cfg[ 'wikiname' ][ 'from' ] }api.php" )
# Checking if user seleted direct uploading
if @cfg[ 'direct' ] then
# Logging on target to upload wiki
puts @msg[ 'log-in' ]
begin
@c_to = MediawikiApi::Client.new( "#{ @cfg[ 'wikiname' ][ 'to' ] }api.php" )
@c_to.log_in( @cfg['nickname'], @cfg['password'] )
rescue => err
msg = @msg[ 'log-in-error' ] % @cfg[ 'wikiname' ][ 'to' ]
log_err( "#{ msg }\n #{ err.inspect } #{ err.backtrace.join( "\n" ) }" )
puts msg
RubyDownloader.quit
end
# Token
puts @msg[ 'token' ]
@token = @c_to.prop( :info, titles: 'ItsARandomRubyDownloaderPage', intoken: 'edit' ).data[ 'pages' ][ '-1' ][ 'edittoken' ]
end
end
def start
# Progress variables
all_f = @filelist.count()
done_f = 0
err_counter = 0
# Reading file list
@filelist.each do | line |
begin
done_f += 1
progress = ( done_f.to_f / all_f.to_f ) * 100
system( "echo \"\033]0;#{ @msg[ 'status-msg' ] % [ "#{ progress.floor }%", done_f, all_f, err_counter, line ] }\007\"" )
RubyDownloader.cls
puts @msg[ 'status-msg' ] % [ "#{ progress.floor }%", done_f, all_f, err_counter, line ]
@c_from.query( titles: "File:#{ line } ", prop: 'imageinfo', iiprop: 'url' ).data[ 'pages' ].each do | k, v |
f_name = line.gsub( /["?]/, '-' ) #"/
path = FILE_PATH + "/" + f_name
url = v[ 'imageinfo' ][ 0 ][ 'url' ]
open( path, 'wb' ) do | f |
puts @msg[ 'dwn-file' ]
f << open( url ).read
end
if @cfg[ 'direct' ] then
puts @msg[ 'upload-file' ]
io_f = Faraday::UploadIO.new( path, 'image/png' )
@c_to.action( :upload, filename: f_name, file: io_f, comment: @cfg[ 'comment' ], ignorewarnings: 1, text: @cfg[ 'text' ], token: @token )
end
puts @msg[ 'done' ]
end
rescue => err
err_counter += 1
puts @msg[ 'dwn-error' ] % [ line, err.inspect ]
log_err( "="*80 + "\n#{ line }:\n#{ err.inspect } #{ err.backtrace.join( "\n" ) }\n" )
next
end
end
# On done
RubyDownloader.cls
puts @msg[ 'is-done' ]
RubyDownloader.main
end
def log_err( m )
File.open( ERR_PATH, 'a' ) { | f | f.write( "#{ m }\n" )}
end
end
class Helper
def initialize( s )
@page = s.cfg[ 'wikiname' ][ 'page' ]
if @page == "" then
puts @msg[ 'helper-empty-value' ]
RubyDownloader.main
end
@file = s.cfg[ 'list' ]
@msg = s.msg
@wiki = MediawikiApi::Client.new( "#{ s.cfg[ 'wikiname' ][ 'from' ] }api.php" )
@list = ""
end
def start
RubyDownloader.cls
puts @msg[ 'helper-intro' ] + "\n" + ( @msg[ 'helper-page' ] % @page )
gets
RubyDownloader.cls
puts @msg[ 'helper-progress' ]
res = @wiki.query( titles: @page, prop: 'images', imlimit: 5000, bot: 1 )
d = res.data[ 'pages' ]
if !d[ "-1" ].nil? then
puts @msg[ 'helper-missing-page' ] % @page
RubyDownloader.main
end
d.each do | k, v |
v[ 'images' ].each { | obj | @list += "#{ obj[ 'title' ].gsub( /^[^:]+:/, '') }\n" }
end
if !res[ 'continue' ].nil? then
r = true
c = res[ 'continue' ][ 'imcontinue' ]
while r
res = @wiki.query( titles: @page, prop: 'images', imlimit: 5000, imcontinue: c )
d = res.data[ 'pages' ]
d.each do | k, v |
v[ 'images' ].each { | obj | @list += "#{ obj[ 'title' ].gsub( /^[^:]+:/, '') }\n" }
end
if res[ 'continue' ].nil? then
r = false
else
c = res[ 'continue' ][ 'imcontinue' ]
end
end
end
File.open( PATH + '/' + @file, 'w+' ) {|f| f.write( @list )}
puts @msg[ 'helper-done' ] % @file
RubyDownloader.main
end
end
end
RubyDownloader.init