This repository has been archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeibo.class.php
154 lines (122 loc) · 4.47 KB
/
Weibo.class.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
151
152
153
154
<?php
class Weibo
{
private $availableCommands = array(
'tweet' => 'Send a tweet',
'read' => 'Read your stream',
'list' => 'List all the available commands',
'exit' => 'Exit'
);
private $stdin;
private $username;
private $password;
private $userCommand;
private $db;
private $config;
private $auth = null;
private $user;
private $weiboClient;
public function __construct()
{
$this->stdin = defined('STDIN') ? STDIN : fopen('php://stdin', 'r');
$this->db = new DB('users');
$this->config = Config::getInstance(array('key' => '656326257',
'secret' => 'ca82c8c5a3f0f7b7bd7f3661a3da541a'));
$this->executeAuthCommand();
$this->executeListCommand();
}
public function run()
{
while (true)
{
$this->userCommandInfo = trim(@fread($this->stdin, 80));
$this->userCommandInfo = array_map('trim', explode(' ', $this->userCommandInfo));
$this->userCommand = array_shift($this->userCommandInfo);
if (!array_key_exists($this->userCommand, $this->availableCommands))
{
echo "Command does not exists.\n";
//throw new BadCommandException('Command not eixsts!');
}
else if (!method_exists($this, 'execute'.ucfirst($this->userCommand).'Command'))
{
echo "This command has not been implemented!\n";
//throw new CommandNotImplementException('This command has not been implemented!');
}
else
{
call_user_func_array(array($this, 'execute'.ucfirst($this->userCommand).'Command'), $this->userCommandInfo);
}
}
}
public function executeTweetCommand()
{
echo "Type your text here: \n";
$tweet = trim(fread(STDIN, 280));
$this->weiboClient->update($tweet);
// Read the the update you just sent
$this->executeReadCommand(1);
}
public function executeReadCommand($number = 20)
{
$ms = $this->weiboClient->home_timeline(); // done
$tweets = $ms;
$t = array_splice($tweets, 0, $number);
foreach($t as $index => $content)
{
printf("[%s]: %s: %s\n\n", $index + 1, $content['user']['name'], $content['text']);
if (isset($content['retweeted_status']))
{
printf("转发微博:%s: %s\n\n", $content['retweeted_status']['user']['name'], $content['retweeted_status']['text']);
}
}
}
public function executeListCommand()
{
echo "Available commands: \n";
echo "-----------------\n";
foreach ($this->availableCommands as $cmd => $description)
{
echo $cmd, ': ', $description;
echo "\n";
}
echo "-----------------\n";
}
private function executeAuthCommand()
{
echo "Command Line Weibo v0.1\n\n";
echo "You email please: ";
$this->username = trim(@fread($this->stdin, 80));
// Check username in the db
$this->user = $this->db->getOneUserByUsername($this->username);
if (!$this->user)
{
echo "\nYour password please: ";
$this->password = trim(@fread($this->stdin, 80));
// Auth the user
$auth = new Auth($this->username, $this->password, $this->config);
$accessTokenPair = $auth->getAccessToken();
// Assume that we successfully authenticated the user
// Create a user
$user = new User();
$user->setUsername($this->username);
$user->setPassword($this->password);
$user->setAccessToken($accessTokenPair['oauth_token']);
$user->setAccessTokenSecret($accessTokenPair['oauth_token_secret']);
// Add user into db
$this->db->addUser($user);
// Persist user into db
$this->db->save();
$this->user = $user;
}
// Create weibo client
$this->weiboClient = new WeiboClient($this->config->getParameter('key'),
$this->config->getParameter('secret'),
$this->user->getAccessToken(),
$this->user->getAccessTokenSecret()
);
}
public function executeExitCommand()
{
exit("Bye.\n");
}
}