-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.php
235 lines (176 loc) · 6.25 KB
/
run.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
# フォルダパス
$folder = 'ここにフルパス(/で終わる)を記述';
# ライブラリ読み込み
require $folder . 'vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
# 投稿の有無 (投稿: true)
$posting = true;
$posting_twtr = false;
$posting_msky = true;
$posting_nostr = false;
$posting_bsky = false;
# 旧Twitter API Key
$twtr_apikey = 'API Key (Consumer Key)';
$twtr_apisecret = 'API Secret (Consumer Secret)';
$twtr_accesstoken = 'OAuth Access Token';
$twtr_tokensecret = 'OAuth Access Token Secret';
# 設定
$userid = 'Foursquare の設定ページに書かれているユーザーID (数字) を記述';
$token = 'アクセストークンを記述';
# API Endpoint
$api = 'https://api.foursquare.com/v2/users/' . $userid . '/checkins?v=20220722&oauth_token=' . $token;
# 取得
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'Foursquare_API');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$json = mb_convert_encoding($response, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$result = json_decode($json, true);
# ファイル読み込み
$filename = $folder . 'data.csv';
$filer = fopen($filename, 'r');
$rcsv = fgetcsv($filer);
$checkin_data[0] = $rcsv[1];
$checkin_data[1] = $rcsv[2];
$checkin_data[2] = $rcsv[3];
$checkin_data[3] = $rcsv[4];
$checkin_data[4] = $rcsv[5];
fclose($filer);
# チェックインID
$now_checkin[0] = $result['response']['checkins']['items'][0]['id'];
# チェックイン数
$now_checkin[1] = $result['response']['checkins']['count'];
# ベニュー名
$now_checkin[2] = $result['response']['checkins']['items'][0]['venue']['name'];
# プライベートかどうか
if ($result['response']['checkins']['items'][0]['private'] == true) {
$now_checkin[3] = 1;
} else {
$now_checkin[3] = 0;
}
# 共有用リンク取得
$api2 = 'https://api.foursquare.com/v2/checkins/' . $now_checkin[0] . '?v=20220722&oauth_token=' . $token;
$ch = curl_init($api2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, 'Foursquare_API');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response2 = curl_exec($ch);
curl_close($ch);
$json2 = mb_convert_encoding($response2, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$result2 = json_decode($json2, true);
$now_checkin[4] = $result2['response']['checkin']['checkinShortUrl'];
# ファイル書き込み
if (!empty($result['response'])) {
$filew = fopen($filename, 'w');
$req_date = date("Y-m-d H:i T", $_SERVER['REQUEST_TIME']);
$wcsv = array($req_date, $now_checkin[0], $now_checkin[1], $now_checkin[2], $now_checkin[3], $now_checkin[4]);
fputcsv($filew, $wcsv);
fclose($filew);
}
# 投稿データ作成
if (($checkin_data[0] != $now_checkin[0]) && isset($now_checkin[0])) {
if ($now_checkin[3] == 0) {
$post_arr = array();
$post_arr[] = "I'm at ";
$post_arr[] = $now_checkin[2];
$post_arr[] = "\n";
$post_arr[] = $now_checkin[4];
$post_arr[] = "\n";
$post_arr[] = "Total Checkin Count: ";
$post_arr[] = $now_checkin[1];
$post_data = implode($post_arr);
} else {
$post_data == "";
}
} else {
$post_data == "";
}
# 投稿
if ($posting == true) {
# 旧Twitter
if ($posting_twtr == true) {
if (!empty($post_data)) {
$data = [
'text' => $post_data
];
$twtr_connection = new TwitterOAuth($twtr_apikey, $twtr_apisecret, $twtr_accesstoken, $twtr_tokensecret);
$twtr_connection->setApiVersion("2");
$twtr_result = $twtr_connection->post('tweets', $data, true);
}
}
# Misskey
# misskey.io の場合、サーバーとアクセストークンを設定するだけで動きます
if ($posting_msky == true) {
if (!empty($post_data)) {
$data = [
'i' => 'Misskey Access Token',
'text' => $post_data,
'visibility' => 'public'
];
$json_data = json_encode($data);
$put_url = 'https://misskey.io/api/notes/create';
$ch = curl_init($put_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
curl_exec($ch);
curl_close($ch);
}
}
# Nostr
# https://github.com/mattn/algia を使う前提のサンプル
if ($posting_nostr == true) {
if (!empty($post_data)) {
$data2 = [
'note' => $post_data
];
$json_data2 = json_encode($data2);
$put_url2 = 'http://127.0.0.1:10000/post';
$ch = curl_init($put_url2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data2);
curl_exec($ch);
curl_close($ch);
}
}
# Bluesky
# https://github.com/mattn/bsky を使う前提のサンプル
if ($posting_bsky == true) {
if (!empty($post_data)) {
$data3 = [
'note' => $post_data
];
$json_data3 = json_encode($data3);
$put_url3 = 'http://127.0.0.1:10010/post';
$ch = curl_init($put_url3);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data3);
curl_exec($ch);
curl_close($ch);
}
}
}
?>