-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.go
164 lines (138 loc) · 3.09 KB
/
fetch.go
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
155
156
157
158
159
160
161
162
163
package main
import(
"fmt"
"flag"
"exec"
"json"
)
type STWeetsError struct{
Request string
Error_code string
Error string
}
type STWeetsUser struct{
Id int64
Screen_name string
Name string
Province string
City string
Location string
Description string
Url string
Profile_image_url string
Domain string
Gender string
Followers_count uint
Friends_count uint
Statuses_count uint
Favourites_count uint
Created_at string
Following bool
Allow_all_act_msg bool
Geo_enabled bool
Verified bool
}
type STWeetsRetweet struct{
created_at string
Id int64
Text string
Source string
Favorited bool
Truncated bool
In_reply_to_status_id string
In_reply_to_user_id string
In_reply_to_screen_name string
Thumbnail_pic string
Bmiddle_pic string
Original_pic string
Geo string
Mid string
User STWeetsUser
}
type STWeets struct{
Created_at string
Id int64
Text string
Source string
Favorited bool
Truncated bool
In_reply_to_status_id string
In_reply_to_user_id string
In_reply_to_screen_name string
Geo string
Mid string
User STWeetsUser
Retweeted_status STWeetsRetweet
}
type STweetsFields struct {
Name string
Text string
Date string
Picture string
}
func ParseTweets(b []byte) (string,bool,[]STWeets){
var tFilter []STWeets
err:=json.Unmarshal(b,&tFilter);
if err == nil {
fmt.Println("Parse Success");
} else {
fmt.Println(err.String());
}
for index,value := range tFilter {
fmt.Printf("%d|%s|%s|%s\n",index,value.User.Name,value.Text,value.Created_at);
}
return "",true,tFilter;
}
func ParseError(b []byte) bool {
var tErrorFilter STWeetsError;
err:=json.Unmarshal(b,&tErrorFilter);
if err == nil {
fmt.Println("");
fmt.Println(tErrorFilter.Request);
return true;
} else {
fmt.Println(err.String());
return false;
}
return false;
}
func main(){
var username string;
var password string;
var action string;
var source string = "3215873400";
var base_url string ="http://api.t.sina.com.cn/";
var api_map = map[string]string{"public":"statuses/public_timeline","friends":"statuses/friends_timeline","user":"user_timeline","mention":"statuses/mentions","respost":"statuses/repost","update":"statuses/update","comments":"comments/show"}
flag.StringVar(&username,"u","","Set Your weibo Account");
flag.StringVar(&password,"p","","Set Your weibo Password");
flag.StringVar(&action,"a","","Set Your weibo Action");
flag.Parse();
if username == "" || password=="" || action == "" {
fmt.Println("Do not empty the Username or Password or Action");
}
var target_api string;
var bTest bool;
target_api,bTest=api_map[action];
if bTest == false {
fmt.Println("Use the wrong action");
return;
}
pcmd := exec.Command("curl","-u",username+":"+password,base_url+target_api+".json?source="+source);
output,err:= pcmd.Output();
if err != nil {
fmt.Println("Get tweets Error!");
return;
}
switch action {
case "public","friends":
if ParseError(output) {
fmt.Println("Wrong account info");
return;
}
ParseTweets(output);
default :
fmt.Println("To be compeleted");
}
// outstr:=string(output);
// fmt.Println(outstr);
}