Skip to content

Commit

Permalink
🍻 修复幼儿算术 #23
Browse files Browse the repository at this point in the history
  • Loading branch information
metowolf committed Mar 8, 2018
1 parent f65fcf4 commit ec8ce51
Showing 1 changed file with 82 additions and 52 deletions.
134 changes: 82 additions & 52 deletions Bilibili.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/**
* Website: https://i-meto.com/
* Author: METO
* Version: 0.6.1
* Version: 0.6.2
*/


Class Bilibili
class Bilibili
{
// 主播房间 id
public $roomid = '3746256';
Expand Down Expand Up @@ -84,7 +84,7 @@ public function run()
{
$this->init();
for ($idx=0; $idx<3; $idx++) {
while(true) {
while (true) {
if(!$this->sign())break;
if(!$this->heart())break;
if(!$this->silver())break;
Expand All @@ -95,7 +95,7 @@ public function run()
sleep(10);
}

if(isset($this->callback)){
if (isset($this->callback)) {
call_user_func($this->callback);
exit(1);
}
Expand Down Expand Up @@ -185,8 +185,10 @@ private function giftsend()
return false;
}

foreach ($data['data']['list'] as $vo){
if (abs($vo['expire_at']-time())>3600) continue;
foreach ($data['data']['list'] as $vo) {
if (abs($vo['expire_at']-time())>3600) {
continue;
}
$payload = array(
'uid' => $this->uid,
'gift_id' => $vo['gift_id'],
Expand All @@ -207,15 +209,18 @@ private function giftsend()
$res = $this->curl($api, $payload);
$res = json_decode($res, true);

if ($res['code']) $this->log("{$res['msg']}",'red','投喂');
else $this->log("成功向 https://live.bilibili.com/{$this->roomid} 投喂了 {$vo['gift_num']}{$vo['gift_name']}", 'green', '投喂');
if ($res['code']) {
$this->log("{$res['msg']}", 'red', '投喂');
} else {
$this->log("成功向 https://live.bilibili.com/{$this->roomid} 投喂了 {$vo['gift_num']}{$vo['gift_name']}", 'green', '投喂');
}
}
return true;
}

private function giftheart()
{
if (time()<$this->lock['giftheart']){
if (time()<$this->lock['giftheart']) {
return true;
}
$this->lock['giftheart'] += 5*60;
Expand All @@ -236,26 +241,27 @@ private function giftheart()
}

if ($data['data']['heart_status'] == 0) {
$this->log('没有礼物可以领了呢','magenta','收礼');
$this->log('没有礼物可以领了呢', 'magenta', '收礼');
$this->lock['giftheart'] += 60*60;
}

if(isset($data['data']['gift_list'])){
foreach($data['data']['gift_list'] as $vo){
$this->log("{$data['msg']},礼物 {$vo['gift_name']} ({$vo['day_num']}/{$vo['day_limit']})",'magenta','收礼');
if (isset($data['data']['gift_list'])) {
foreach ($data['data']['gift_list'] as $vo) {
$this->log("{$data['msg']},礼物 {$vo['gift_name']} ({$vo['day_num']}/{$vo['day_limit']})", 'magenta', '收礼');
}
}
return true;
}

private function silver(){
private function silver()
{
if (time() < $this->lock['silver']) {
return true;
}

if (!isset($this->temp['task'])) {
// 查询宝箱数量
$api = $this->prefix . 'FreeSilver/getCurrentTask';
$api = $this->prefix . 'lottery/v1/SilverBox/getCurrentTask';
$raw = $this->curl($api);
$data = json_decode($raw, true);
// 今日已经领完
Expand All @@ -276,16 +282,18 @@ private function silver(){
'end' => $data['data']['time_end'],
);
$this->lock['silver'] = $data['data']['time_end'] + 5;
$this->log(sprintf("等待 %s 领取",
$this->log(sprintf(
"等待 %s 领取",
date('H:i:s', $this->lock['silver'])
),'blue','宝箱');
), 'blue', '宝箱');

return true;
}

$captcha = $this->captcha();

$api = $this->prefix . sprintf('freeSilver/getAward?time_start=%s&time_end=%s&captcha=%s',
$api = $this->prefix . sprintf(
'lottery/v1/SilverBox/getAward?time_start=%s&end_time=%s&captcha=%s',
$this->temp['task']['start'],
$this->temp['task']['end'],
$captcha
Expand All @@ -295,8 +303,7 @@ private function silver(){

if ($data['code'] != 0) {
$this->log($data['msg'], 'bg_red', '宝箱');
}
else {
} else {
$this->log("领取成功,silver: {$data['data']['silver']}(+{$data['data']['awardSilver']})", 'cyan', '宝箱');
}

Expand All @@ -305,24 +312,37 @@ private function silver(){
return true;
}

private function captcha(){
$this->log("开始做小学生算术","blue",'宝箱');
public function captcha()
{
$this->log("开始做幼儿算术", "blue", '宝箱');

$api = $this->prefix . 'freeSilver/getCaptcha?ts='.time();
$raw = $this->curl($api, null, false);
$image = imagecreatefromstring($raw);
$api = $this->prefix . 'lottery/v1/SilverBox/getCaptcha?ts='.time();
$raw = $this->curl($api);
$data = json_decode($raw, true);
$exploded = explode(',', $data['data']['img'], 2);
$encoded = $exploded[1];
$decoded = base64_decode($encoded);

$image = imagecreatefromstring($decoded);
$width = imagesx($image);
$height = imagesy($image);
for($i=0;$i<$height;$i++)
for($j=0;$j<$width;$j++)
$grey[$i][$j]=(imagecolorat($image,$j,$i)>>16)&0xFF;
for($i=0;$i<$width;$i++)$vis[$i]=0;
for($i=0;$i<$height;$i++)
for($j=0;$j<$width;$j++)
for ($i=0;$i<$height;$i++) {
for ($j=0;$j<$width;$j++) {
$grey[$i][$j]=(imagecolorat($image, $j, $i)>>16)&0xFF;
}
}
for ($i=0;$i<$width;$i++) {
$vis[$i]=0;
}
for ($i=0;$i<$height;$i++) {
for ($j=0;$j<$width;$j++) {
$vis[$j]|=$grey[$i][$j]<220;
for($i=0;$i<$height;$i+=2){
for($j=0;$j<$width;$j+=2)
}
}
for ($i=0;$i<$height;$i+=2) {
for ($j=0;$j<$width;$j+=2) {
echo $grey[$i][$j]<220?'':'';
}
echo "\n";
}

Expand All @@ -342,31 +362,37 @@ private function captcha(){
);

$result='';
for($k=0;$k<$width;$k++)if($vis[$k]){
$L=$R=$k;
while($vis[$R]==1)$R++;
$str='';
for($j=$L;$j<$R;$j++)
for($i=4;$i<=34;$i++)
$str.=$grey[$i][$j]<220?'1':'0';
$max=0;
foreach($OCR as $key=>$vo){
similar_text($str,$vo,$per);
if($per>$max){
$max=$per;
$ch=$key;
for ($k=0;$k<$width;$k++) {
if ($vis[$k]) {
$L=$R=$k;
while ($vis[$R]==1) {
$R++;
}
$str='';
for ($j=$L;$j<$R;$j++) {
for ($i=4;$i<=34;$i++) {
$str.=$grey[$i][$j]<220?'1':'0';
}
}
$max=0;
foreach ($OCR as $key=>$vo) {
similar_text($str, $vo, $per);
if ($per>$max) {
$max=$per;
$ch=$key;
}
}
$result.=$ch;
$k=$R;
}
$result.=$ch;
$k=$R;
}

$ans = eval("return $result;");
$this->log("好耶! $result = $ans", 'blue', '宝箱');
return $ans;
}

public function log($message,$color='default',$type='')
public function log($message, $color='default', $type='')
{
$colors = array(
'none' => "",
Expand All @@ -384,12 +410,16 @@ public function log($message,$color='default',$type='')
);
$this->msg = $message;
$date = date('[Y-m-d H:i:s] ');
if(!empty($type)) $type="[$type] ";
if(!$this->color) $color='none';
if (!empty($type)) {
$type="[$type] ";
}
if (!$this->color) {
$color='none';
}
echo sprintf($colors[$color], $date . $type . $message) . PHP_EOL;
}

public function curl($url,$data=null,$log=true)
public function curl($url, $data=null, $log=true)
{
if ($this->debug) {
$this->log('>>> ' . $url, 'lightgray');
Expand Down

0 comments on commit ec8ce51

Please sign in to comment.