# frozen_string_literal: true require 'httparty' require 'zip' require 'fileutils' require 'colored2' class LanguageDownloader HeaderString = %Q{Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate, br Accept-Language:zh Content-Length:194 Content-Type:application/json Cookie:_ga=GA1.2.1025116346.1676013076; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%224902805f3ea41882.692841132392792065%22%2C%22first_id%22%3A%221859aeaeae8ac6-062e0ea9cca7ea-17525635-2073600-1859aeaeae9f16%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fuc.aqara.cn%2F%22%7D%2C%22%24device_id%22%3A%221859aeaeae8ac6-062e0ea9cca7ea-17525635-2073600-1859aeaeae9f16%22%7D; Token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0OTAyODA1ZjNlYTQxODgyLjY5Mjg0MTEzMjM5Mjc5MjA2NSIsImlzcyI6Imh0dHBzOi8vYWlvdC1ycGMuYXFhcmEuY24iLCJhY2NvdW50IjoiamluZ3lhLmx1LWExMDc5QGFxYXJhLmNvbSIsImlhdEdtdDgiOjE2OTM1NzE0MDksImp0aSI6ImU4Y2YzNGMyNGRlYzQ0YWZiNzM5ODg3YzhiMGI3ZWE0YWM2OWFhNjViZmFjNDcxNWFlM2M1NmFkOGVhNDJlZGUifQ.INPHQkRwwRpXyRYpbRpNm6MfoYKUm7O4QjW7sOGNd6I; Userid=4902805f3ea41882.692841132392792065; currentSid=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0OTAyODA1ZjNlYTQxODgyLjY5Mjg0MTEzMjM5Mjc5MjA2NSIsImlzcyI6Imh0dHBzOi8vYWlvdC1ycGMuYXFhcmEuY24iLCJpYXRHbXQ4IjoxNjkzNTcxNDA5LCJqdGkiOiI4M2JmOWRkNDlmZDU0ZTUwOWVmODFjNjJhZjMwYTg4NmVjN2E1N2QzMGVkYTQ3ZTM5MDEzMGVhNWI5MWUxZTBkIn0.wHuZsMUkvTg4i-C4nHDhCpAiq-1acfl_G6KeycekIiw; currentAccount=jingya.lu-a1079@aqara.com; userInfo=%7B%22accountCategory%22%3A%220%22%2C%22company%22%3A%7B%22companyId%22%3A1%2C%22companyName%22%3A%22%E7%BB%BF%E7%B1%B3%E8%81%94%E5%88%9B%22%7D%2C%22user%22%3A%7B%22avatarUrl%22%3A%22%22%2C%22companyId%22%3A1%2C%22email%22%3A%22jingya.lu-a1079%40aqara.com%22%2C%22nickName%22%3A%22%22%2C%22userName%22%3A%22%E9%B2%81%E9%9D%99%E4%BA%9A%22%2C%22userType%22%3A0%7D%7D Lang:zh Nonce:980m43cn1qezpnplek4ovr8ahkg2enq1 Operate-Platform:40 Origin:https://intl-lang.aqara.com Projectid:5 Referer:https://intl-lang.aqara.com/ Sec-Ch-Ua:"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116" Sec-Ch-Ua-Mobile:?0 Sec-Ch-Ua-Platform:"macOS" Sec-Fetch-Dest:empty Sec-Fetch-Mode:cors Sec-Fetch-Site:same-origin Time:1693571557934 Timestamp:1693571557934 User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 } def self.makeHeader headerArray = HeaderString.split("\n") header = {} headerArray.each do |str| header[str.split(":",2)[0].strip] = str.split(":",2)[1].strip end header end def self.download # puts makeHeader #下载文件 params = {"projectIdList":[5],"langIds":[1,2,3,9,10,11,12,13,14,15,16,17,18],"fileTypeList":["excel"],"auditState":1,"valueState":0,"userId":"","value":"","tagIds":nil,"bizUseTagIds":nil,"pkeys":[]} response = HTTParty.post('https://intl-lang.aqara.com/v1.0/lumi/language/file/export', body: JSON.generate(params), headers:LanguageDownloader.makeHeader) # puts response.body unless response.body["code"] == nil 3.times do puts "资源包下载失败了!!!!! code:#{response.body}".red end return end destation_path = "./localize.zip" if File.exist? destation_path FileUtils.rm_rf destation_path end File.open(destation_path,"w") do |io| io.binmode io.write response.body end # 解压缩文件到指定目录 f_path = '' Zip::File.open(destation_path) do |zip_file| zip_file.each do |f| f_path = File.join('./','hit_all.xls') File.delete f_path if File.exist? f_path # FileUtils.mkdir_p(File.dirname(f_path)) f.extract(f_path) end end if File.exist? destation_path FileUtils.rm_rf destation_path end f_path end end #LanguageDownloader.download