Fangdidididi 发表于 2022-3-19 22:26

bilibili自动上传视频脚本(纯ruby)

本帖最后由 Fangdidididi 于 2022-3-19 22:36 编辑

> 纯ruby版本的bilibili视频上传脚本,可以将指定目录下的视频依次上传到bilibili(需要对应账号的cookie)

> 主要分为4个步骤:预上传,获取推荐标签,通知上传完成,上传

## 一,视频预上传

> 获取上传视频所需的各项参数,并将视频切割 进行分片上传

```ruby
def get_preupload(name,cookie,path)
    size = File.size("#{path}/#{name}")
    puts "#{Time.now}-------------------preupload预上传"
    lk = "https://member.bilibili.com/preupload?name=#{URI.encode(name)}&size=#{size}&r=upos&profile=ugcupos/bup&ssl=0&version=2.7.1&build=2070100&upcdn=bda&probe_version=20200224"
    headers = {"cookie"=>cookie,
               "user-agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36"}
    #1
    str =RestClient.get(lk,headers=headers)
    doc = JSON.parse(str)
    auth = doc["auth"]
    upos_uri = doc["upos_uri"]
    chunk_size = doc["chunk_size"]
    biz_id = doc["biz_id"]
    #2
    puts "#{Time.now}-------------------准备上传视频"
    lk = "https://upos-sz-upcdnbda2.bilivideo.com/#{upos_uri.gsub("upos://","")}?uploads&output=json"
    data = "uploads&output=json"
    headers = {"cookie"=>cookie,
               "user-agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36",
               "x-upos-auth"=>auth}
    str = RestClient.post(lk,data,headers=headers)
    doc = JSON.parse(str)
    upload_id = doc["upload_id"]
    #
    puts "#{Time.now}-------------------分批上传视频"
    chunkss = size.to_f/chunk_size
    if chunkss > chunkss.to_i
      chunks =chunkss.to_i + 1
    end
    file = File.open("#{path}/#{name}")
    if chunks < 3
      sleep(15)
    end
    for i in 1..chunks
      puts "#{Time.now}-------------------上传第#{i}部分"
      partnumber = 0+i
      chunk = i-1
      starts = file.tell()
      data = file.read(chunk_size)
      sizes = data.size
      ends = file.tell()
      lk = "https://upos-sz-upcdnbda2.bilivideo.com/#{upos_uri.gsub("upos://","")}?partNumber=#{partnumber}&uploadId=#{upload_id}&chunk=#{chunk}&chunks=#{chunks}&size=#{sizes}&start=#{starts}&end=#{ends}&total=#{size}"
      params = "partNumber=#{partnumber}&uploadId=#{upload_id}&chunk=#{chunk}&chunks=#{chunks}&size=#{sizes}&start=#{starts}&end=#{ends}&total=#{size}"
      headers = {"cookie"=>cookie,
               "user-agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36",
               "x-upos-auth"=>auth}
      str = RestClient.put(lk,data=data,headers=headers)
      puts "-----上传视频状态码#{str.code}"
    end
    puts "-------------------通知上传完毕"
    lk = "https://upos-sz-upcdnbda2.bilivideo.com/#{upos_uri.gsub("upos://","")}?output=json&name=#{URI.encode(name)}&profile=ugcupos%2Fbup&uploadId=#{upload_id}&biz_id=#{biz_id}"
    params = "output=json&name=#{URI.encode(name)}&profile=ugcupos%2Fbup&uploadId=#{upload_id}&biz_id=#{biz_id}"
    data= {}
    data[:parts] = chunks.times.map{|x| {"partNumber":x+1,"eTag":"etag"}}
    headers4 = {"cookie"=>cookie,
                "user-agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
                "X-Upos-Auth"=>auth}
    puts str = RestClient.post(lk,payload=data.to_json,headers=headers)
    return upos_uri,biz_id
end
```

## 二,获取推荐标签

> 获取bilibili给出的视频推荐标签,我这边是使用默认的前5个标签

```ruby
def get_tag(name,cookie,upos_uri)
    sleep(2)
    puts "-------------------获取视频推荐标签"
    puts lk = "https://member.bilibili.com/x/vupre/web/archive/tags?typeid=&title=#{URI.encode(name.gsub(".mp4",""))}&filename=#{upos_uri.split("/")[-1].gsub(/\..+/,"")}&desc=&cover=&groupid=0&vfea="

    headers ={"cookie"=>cookie}
    str = RestClient.get(lk,headers=headers)
    doc = JSON.parse(str)
    tags = doc["data"].map{|x| x["tag"]}.join(",")
    puts "----当前标签:#{tags}"
    return tags
end
```

## 三,预发布

```ruby
def get_inform(cookie)
    puts "#{Time.now}-------------------预发布"
    lk = "https://member.bilibili.com/x/geetest/pre/add"
    headers ={"cookie"=>cookie}
    str = RestClient.get(lk,headers=headers)
end
```

## 四,发布视频

```ruby
def get_up(upos_uri,name,biz_id,tags,cookie,bili_jct)
    sleep(2)
    lk = "https://member.bilibili.com/x/vu/web/add?csrf=#{bili_jct}"
    data="{\"copyright\":2,\"videos\":[{\"filename\":\"#{upos_uri.split("/")[-1].gsub(/\..+/,"")}\",\"title\":\"#{name.gsub(".mp4","")}\",\"desc\":\"\",\"cid\":#{biz_id}}],\"source\":\"\",\"tid\":174,\"cover\":\"\",\"title\":\"#{name.gsub(".mp4","")}\",\"tag\":\"#{tags}\",\"desc_format_id\":0,\"desc\":\"\",\"dynamic\":\"\",\"subtitle\":{\"open\":0,\"lan\":\"\"},\"up_close_reply\":false,\"up_close_danmu\":false}"
    headers = {"content-type"=>"application/json;charset=UTF-8",
               "cookie"=>cookie,
               "dnt"=>"1",
               "origin"=>"https://member.bilibili.com",
               "pragma"=>"no-cache",
               "referer"=>"https://member.bilibili.com/video/upload.html",
               "sec-fetch-dest"=>"empty",
               "sec-fetch-mode"=>"cors",
               "sec-fetch-site"=>"same-origin",
               "user-agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"}
    puts str = RestClient.post(lk,data,headers=headers)
    puts "#{Time.now}------------------发布成功"
end
```

艾莉希雅 发表于 2022-3-19 23:36

配合爬虫等是不是就能成为自动搬运小能手了
营销号狂喜

wangmi5200 发表于 2022-4-20 11:10

/Users/mick/Desktop/bili/update_bilibili.rb:32:in `get_preupload': undefined method `encode' for URI:Module (NoMethodError)

    lk = "https://member.bilibili.com/preupload?name=#{URI.encode(name)}&size=#{size}&r=upos&profile=ugcupos/bup&ssl=0&version=2.7.1&build=2070100&upcdn=bda&probe_version=20200224"
                                                          ^^^^^^^
        from /Users/mick/Desktop/bili/update_bilibili.rb:15:in `block in main'
        from /Users/mick/Desktop/bili/update_bilibili.rb:10:in `each'
        from /Users/mick/Desktop/bili/update_bilibili.rb:10:in `each_with_index'
        from /Users/mick/Desktop/bili/update_bilibili.rb:10:in `main'
        from /Users/mick/Desktop/bili/update_bilibili.rb:128:in `<main>'

FBIleon 发表于 2022-3-19 23:11

这个看起来不错,B站自己的上传好麻烦

ying656 发表于 2022-3-19 23:15

好东西可惜看不懂 哈哈

gz200 发表于 2022-3-19 23:15

我很赞同!

zxcvbn173164 发表于 2022-3-19 23:37

可以做个软件方便我们小白用{:1_919:}

DeathLYH 发表于 2022-3-19 23:41

好东西,下载了

MSMKin 发表于 2022-3-20 00:39

感谢分享!

AMA7 发表于 2022-3-20 00:41

很不错啊,强!

曾经的歌42 发表于 2022-3-20 00:49

推荐标签能够精确么
页: [1] 2 3 4
查看完整版本: bilibili自动上传视频脚本(纯ruby)