-
Notifications
You must be signed in to change notification settings - Fork 11
/
executor.php
executable file
·150 lines (132 loc) · 3.95 KB
/
executor.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require 'config.inc.php';
require 'sign.php';
/**
* 执行签到操作类
* @author Newton <[email protected]>
*/
class Executor
{
//错误次数
private $errCount = 0;
//日志文本
private $logText;
private $allSigned = true;
//Weibo 实例
private static $weibo = NULL;
private static $token = NULL;
/**
* 构造函数,记录时间戳并执行签到
* @param array $accounts 需要签到的账户信息数组
*/
public function __construct($accounts)
{
date_default_timezone_set('PRC');
$timestamp = date('Y-m-d H:i:s', time());
$this->logText = $timestamp.PHP_EOL;
echo $timestamp,'<br>';
set_time_limit(60);
$this->execute($accounts);
}
/**
* 签到方法
* @param array $accounts 需要签到的账户信息数组
*/
public function execute($accounts)
{
foreach ($accounts as $userInfo)
{
$svcName = $userInfo[0];
try {
$this->fileLoader(DIR_ROOT.'/services/'.strtolower($svcName).'.php'); //载入要签到服务的子类
} catch (Exception $e) {
die($e->getMessage());
}
//获取实例并初始化
$instance = $svcName::getInstance();
$instance->init($userInfo[1], $userInfo[2]);
$errCount = $this->errCount;
while($errCount < RETRY_LIMIT)
{
try {
$instance->sign();
} catch (Exception $e) {
$errCount++;
continue;
}
break;
}
if($errCount >= RETRY_LIMIT)
{
$this->allSigned = false;
if(NOTIFY_FAILED)
try {
$status = $svcName.' 失败多次,请检查日志 #AutoSign#';
$this->weiboNotify($status);
} catch (Exception $e) {
$instance->appendLog($e->getMessage());
}
}
//记录日志
$this->logText .= $instance->getLog();
}
//输出日志
if(LOG)
$this->log();
if(NOTIFY_SUCCESS && $this->allSigned)
$this->weiboNotify('All signed', 2);
}
/**
* 发送微博通知
* @param string $status 要发布的微博内容
*/
private function weiboNotify($status, $visible = 0, $listId = NULL)
{
if(is_null(self::$weibo))
{
$this->fileLoader('weibo/config.php');
$this->fileLoader('weibo/saetv2.ex.class.php');
if(file_exists(OAUTH_FILE))
{
self::$token = file_get_contents(OAUTH_FILE);
self::$token = unserialize(self::$token);
}
else
throw new Exception("微博认证文件不存在");
self::$weibo = new SaeTClientV2( WB_AKEY , WB_SKEY , self::$token['access_token']);
}
$weiboName = WEIBO_NAME;
if(empty($weiboName))
$weiboName = self::$token['name'];
$status = '@'.$weiboName.' '.$status;
$resp = self::$weibo->update($status, $visible, $listId);
if(isset($resp->error_code))
throw new Exception($resp->error);
}
/**
* 输出日志到文件
*/
private function log()
{
file_put_contents(DIR_ROOT.'/sign.log', $this->logText, FILE_APPEND);
}
/**
* 返回本次签到状态
* @return boolean 全部成功: True, 存在失败: False
*/
public function isAllSigned()
{
return $this->allSigned;
}
/**
* 载入指定路径的文件
* @param string $filePath
*/
private function fileLoader($filePath)
{
if(!file_exists($filePath))
throw new Exception("文件丢失 ".$filePath, 1);
require_once($filePath);
}
}
?>