Class: Actir::CookiesBaidu
- Inherits:
-
Object
- Object
- Actir::CookiesBaidu
- Defined in:
- lib/actir/script/cookies_baidu.rb
Class Method Summary collapse
-
.baifubao_account_file ⇒ Object
百度账号配置文件.
-
.clear_after_failure ⇒ Object
更新baidu_cookies失败后的清理操作。目前需要手动调用,后续优化.
-
.directory ⇒ Object
获取cookies文件的路径 因为涉及到权限问题,路径需要放在个人账号目录下 通过whoami命令获取当前账户名称 暂时存放在/User/xx/目录下.
- .directory_exists? ⇒ Boolean
- .file_exists? ⇒ Boolean
- .file_name ⇒ Object
-
.get_baifubao_cookies ⇒ Object
获取cookies.
- .get_cookies(card) ⇒ Object
-
.get_useful_card ⇒ Object
获取可用的百度账号的hash 返回值=> {“username” => “xxx”, “password”=>“iloveyouzan”, xxx}.
-
.init ⇒ Object
cookies文件初始化.
-
.init_content ⇒ Object
默认的配置文件内容,cookies的key.
-
.login_baifubao(username, password) ⇒ Object
登录百付宝主页.
- .modify_cookies(card, cookies) ⇒ Object
-
.open_baifubao(address = []) ⇒ Object
访问百付宝主页.
-
.re_available ⇒ Object
将所有百度支付卡的available状态恢复为true.
-
.set_useless_card(card) ⇒ Object
设置不可用的卡.
-
.update_all(address = []) ⇒ Object
更新百度账号所有的配置文件.
-
.update_cookies(card, address = []) ⇒ Object
更新配置文件中的baidu_cookies.
Class Method Details
.baifubao_account_file ⇒ Object
百度账号配置文件
210 211 212 |
# File 'lib/actir/script/cookies_baidu.rb', line 210 def baifubao_account_file File.join(Actir::Config.default_config_dir, "/pay.yaml") end |
.clear_after_failure ⇒ Object
更新baidu_cookies失败后的清理操作。目前需要手动调用,后续优化
172 173 174 175 176 |
# File 'lib/actir/script/cookies_baidu.rb', line 172 def clear_after_failure if @browser != nil @browser.close end end |
.directory ⇒ Object
获取cookies文件的路径因为涉及到权限问题,路径需要放在个人账号目录下通过whoami命令获取当前账户名称暂时存放在/User/xx/目录下
200 201 202 203 |
# File 'lib/actir/script/cookies_baidu.rb', line 200 def directory user_name = `whoami` return "/Users/" + user_name.chomp + "/cookies/" end |
.directory_exists? ⇒ Boolean
192 193 194 |
# File 'lib/actir/script/cookies_baidu.rb', line 192 def directory_exists? File::directory?(directory) end |
.file_exists? ⇒ Boolean
188 189 190 |
# File 'lib/actir/script/cookies_baidu.rb', line 188 def file_exists? File::exists?(directory + file_name) end |
.file_name ⇒ Object
205 206 207 |
# File 'lib/actir/script/cookies_baidu.rb', line 205 def file_name "cookies.yaml" end |
.get_baifubao_cookies ⇒ Object
获取cookies
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/actir/script/cookies_baidu.rb', line 159 def sleep 3 # id = @browser.cookies[:BAIDUID][:value] ss = @browser.[baifubao_BDUSS.to_sym][:value] stoken = @browser.[baifubao_STOKEN.to_sym][:value] @browser.close #cookies = " BAIDUID:\s\s\s\s\s\s\"" + id + "\"\n BDUSS:\s\s\s\s\s\s\s\s\"" + ss + "\"\n" #以hash形式返回 # {:BAIDUID => id, :BDUSS => ss } {baifubao_BDUSS.to_sym => ss, baifubao_STOKEN.to_sym => stoken} end |
.get_cookies(card) ⇒ Object
184 185 186 |
# File 'lib/actir/script/cookies_baidu.rb', line 184 def (card) Actir::Config.get("cookies" + "." + card, directory) end |
.get_useful_card ⇒ Object
获取可用的百度账号的hash 返回值=> {“username” => “xxx”, “password”=>“iloveyouzan”, xxx}
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/actir/script/cookies_baidu.rb', line 92 def get_useful_card # old_config = Actir::Config.config_dir # Actir::Config.config_dir = script_config_path #通过配置文件判断取出可用的卡的参数 baidu_card = Actir::Config.get(baifubao_key) card = {} baidu_card.each do |key, value| if value["available"] == true # 顺便取一下cookies = Actir::Config.get("cookies." + key, directory) # value["BAIDUID"] = baidu_card_cookies["BAIDUID"] value[baifubao_BDUSS] = [baifubao_BDUSS] value[baifubao_STOKEN] = [baifubao_STOKEN] #有可用的卡,取出cookies等参数 card.store(key, value) break end end # Actir::Config.config_dir = old_config card end |
.init ⇒ Object
cookies文件初始化
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 |
# File 'lib/actir/script/cookies_baidu.rb', line 7 def init = false #判断文件是否存在 if file_exists? # 读取下配置文件中是否真的有cookies数据,如果没有一样要更新 = ("card1") # 判断存在的cookies文件是否有规范的cookies内容 if == nil = true else if .has_key?(baifubao_BDUSS) && .has_key?(baifubao_STOKEN) if [baifubao_BDUSS] == nil || [baifubao_STOKEN] == nil = true else = false end else = true end end else if directory_exists? #文件不存在则看目录是否存在 #目录存在则创建文件 = true else #目录和文件都不存在则创建目录再创建文件 Dir.mkdir(directory) = true end end if == true file = File.new(directory + file_name, "w") file.syswrite(init_content) file.close # 暂时先只更新一张卡吧 ("card1") end end |
.init_content ⇒ Object
默认的配置文件内容,cookies的key
215 216 217 218 219 |
# File 'lib/actir/script/cookies_baidu.rb', line 215 def init_content "card1:\n" + " " + baifubao_BDUSS + ": \n" + " " + baifubao_STOKEN + ": \n" end |
.login_baifubao(username, password) ⇒ Object
登录百付宝主页
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/actir/script/cookies_baidu.rb', line 147 def login_baifubao(username, password) # 选择账号登陆(默认是短信快捷登录,所以需要点击切换一下) link_baifubao_login_back.wait_until_present link_baifubao_login_back.click # 输入账号密码 text_baifubao_username.wait_until_present text_baifubao_username.set username text_baifubao_password.set password .click end |
.modify_cookies(card, cookies) ⇒ Object
178 179 180 181 182 |
# File 'lib/actir/script/cookies_baidu.rb', line 178 def (card, ) .each do |key, value| Actir::Config.set("cookies" + "." + card + "." + key.to_s , "\"" + value.to_s + "\"", directory) end end |
.open_baifubao(address = []) ⇒ Object
访问百付宝主页
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/actir/script/cookies_baidu.rb', line 135 def open_baifubao(address = []) if address.size == 0 if $mode == :remote address = Actir::Remote.get_remote_address(1) end end @browser = Browser.new(:www, :url => address[0], :mode => $mode, :browser => :chrome, :window_size => nil) @browser.goto "baifubao.com" @browser end |
.re_available ⇒ Object
将所有百度支付卡的available状态恢复为true
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/actir/script/cookies_baidu.rb', line 70 def re_available #每次登陆都判断一下cookies文件的上一次修改时间和当前时间 #如果日期不同,则刷新所有的pay文件中baidu-card的状态 unless Actir::Config.is_same_day?("cookies", directory) Actir::Config.lock("pay") do str_array = IO.readlines(baifubao_account_file) str_array.each_with_index do |line, index| if line =~ /available\:\s*false/ str_array[index] = line.gsub(/false/, 'true') end end = File.open(baifubao_account_file, 'w') str_array.each do |line| .puts line end .close end end end |
.set_useless_card(card) ⇒ Object
设置不可用的卡
115 116 117 |
# File 'lib/actir/script/cookies_baidu.rb', line 115 def set_useless_card(card) Actir::Config.set(baifubao_key + "." + card + "." + "available", "false") end |
.update_all(address = []) ⇒ Object
更新百度账号所有的配置文件
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/actir/script/cookies_baidu.rb', line 52 def update_all(address = []) baidu_card = Actir::Config.get(baifubao_key) # 确认目前可用的卡的数目和cookies文件上的是否匹配 # TODO 先不实现,cookies文件上的cards数量过多目前看不影响 # baidu_card_cookies = Actir::Config.get("cookies", directory) # if baidu_card_cookies != nill && baidu_card != nil # if baidu_card_cookies.size > baidu_card.size # # end # else # raise "no baifubao cards" # end baidu_card.each do |card, value| (card, address) end end |
.update_cookies(card, address = []) ⇒ Object
更新配置文件中的baidu_cookies
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/actir/script/cookies_baidu.rb', line 120 def (card, address = []) #打开百付宝 open_baifubao(address) #获取对应卡的账号密码 args = Actir::Config.get(baifubao_key + "." + card) #登录百付宝 login_baifubao(args["username"], args["password"]) #获取cookies = #清除之前的cookies (card, ) puts "Already update baifubao's cookies" end |