This repository has been archived by the owner on Sep 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
biliupload.rb
executable file
·62 lines (56 loc) · 1.58 KB
/
biliupload.rb
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
#!/usr/bin/env ruby
#encoding: utf-8
require 'json'
require 'net/http'
require 'uri'
class NoSessionError < RuntimeError
end
module BiliUpload
module_function
@@tasklist = []
def upload(local_filename)
begin
listitem = {:local => local_filename}
begin
open(local_filename, "r") {|f| f.read(1) } # if the local file fails, ruby will raise a exception
rescue
listitem[:result] = :failed
return
end
begin
cookies = open(File.dirname(__FILE__) + "/bilicookies","r") {|f|f.read}
rescue Errno::ENOENT
cookies = "NO_COOKIE_ERROR"
end
http = Net::HTTP.new("member.bilibili.com")
response = http.get('/get_upload_url', {"Cookie" => cookies})
json = JSON.parse(response.body)
if json["error_code"]
raise NoSessionError.new(json["error_msg"])
end
upload_url = json["url"]
remote_filename = json["file_name"]
#print "> curl -F \"file=@#{local_filename};filename=#{remote_filename}\" '#{upload_url}'\n"
IO.popen("curl -F \"file=@#{local_filename};filename=#{remote_filename}\" '#{upload_url}'") do |pipe|
json = JSON.parse(pipe.read)
if json["code"] == 0
listitem[:result] = :success
listitem[:vu] = remote_filename.split('|').first
else
listitem[:result] = :failed
end
end
ensure
@@tasklist.push listitem
end
end
def tasklist
@@tasklist
end
end
begin
ARGV.each {|file| BiliUpload.upload(file) }
ensure
print "\n\nUpload results:\n"
BiliUpload.tasklist.each {|h| p h }
end