-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
257 lines (193 loc) · 7.58 KB
/
index.php
File metadata and controls
257 lines (193 loc) · 7.58 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/*
rss2mastodon - Post an RSS or Atom feed to a Mastodon account.
Uses PHP CLI and Composer. No database or web server required.
Installation:
0. In ~/rss2mastodon run "composer install".
1. See below which variables to update.
2. Run index.php manually in terminal or via cron.
Example: php ~/rss2mastodon/index.php check_user_key
3. Do not expose this directory to the internet.
*/
// Require composer dependencies.
require __DIR__ . '/vendor/autoload.php';
// Verify timezone is set. macOS issue.
if (!ini_get('date.timezone')) {
date_default_timezone_set('America/New_York');
}
// Please read through the variables below and UPDATE as required.
// UPDATE this URL to the feed you want to follow.
$r2m_url = "http://www.espn.com/espn/rss/nba/news";
// UPDATE this URL to your mastodon instance.
$r2m_mastodon_instance = "https://baller.masto-sport.us/api/v1/statuses";
// UPDATE to ATOM or RSS.
$r2m_feed_type = 'RSS';
// UPDATE to any random string without spaces.
// It's merely to stop the script from being accidentally run.
$r2m_check_user_key = 'TEST_NBA';
// UPDATE to your instance authorization code.
$r2m_mastodon_authcode = 'your_code_here';
// UPDATE hash tags you want in posts
$r2m_mastodon_hashtags = '#NBATEST';
// UPDATE this value to what you would like the data file named.
// This is used to store feed text and keep track of new articles.
$r2m_file_prefix = 'nba_feed_test';
// UPDATE this to the path you will store your data files in.
// chdir(dirname(__FILE__));
$r2m_working_directory = '/home/you/some/path/here';
chdir($r2m_working_directory);
/*
You shouldn't have to modify anything below this row.
*/
/*
Verify submitted key is correct before running script.
*/
if (empty($argv[1])) {
// echo error and die
exit('Supply Check User Key' . PHP_EOL);
} else {
// sanatize $r2m_argv[1]
$r2m_check_user_input = strip_tags($argv[1]);
if ($r2m_check_user_key != $r2m_check_user_input) {
exit('Incorrect Check User Key' . PHP_EOL);
}
}
/*
Here we will figure out what type of feed to process and call the needed functions.
*/
switch ($r2m_feed_type) {
case 'ATOM':
atom_status_update();
break;
case 'RSS':
rss_status_update();
break;
default:
exit('Error Switch Feed Type' . PHP_EOL);
}
/*
This is our main function for reading atom
*/
function atom_status_update()
{
global $r2m_url;
global $r2m_mastodon_hashtags;
global $r2m_file_prefix;
global $r2m_item_output;
$r2m_atom = Feed::loadAtom($r2m_url);
//echo htmlSpecialChars($r2m_atom->title);
foreach ($r2m_atom->entry as $r2m_entry) {
// create file name from title
$r2m_title_article = $r2m_entry->title;
// white list letters and numbers only
$r2m_title_article_clean = preg_replace("/[^A-Za-z0-9]/",'',$r2m_title_article);
$r2m_title_article_clean = $r2m_file_prefix . substr($r2m_title_article_clean, 0, "30") . '.txt';
if (!file_exists($r2m_title_article_clean)) {
$r2m_item_output = NULL;
$r2m_fp_nodb = fopen($r2m_title_article_clean, 'w');
$r2m_item_output = $r2m_title_article;
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= htmlSpecialChars($r2m_entry->link['href']);
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= date("j.n.Y H:i", (int) $r2m_entry->timestamp);
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= 'QUOTE: '. PHP_EOL;
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= trim(substr(strip_tags($r2m_entry->content), 0, "200"));
$r2m_item_output .= '...' . PHP_EOL;
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= $r2m_mastodon_hashtags;
fwrite($r2m_fp_nodb, $r2m_item_output);
fclose($r2m_fp_nodb);
// Post our status update using files named $r2m_file_prefix
mastodon_status_update();
}
}
// Clean up old files. Comment out for rarely updated sites.
// Alternatively you can modify clean up time in the function.
clean_up_data();
}
/*
This is our main function for reading RSS
*/
function rss_status_update()
{
global $r2m_url;
global $r2m_mastodon_hashtags;
global $r2m_file_prefix;
global $r2m_item_output;
$r2m_rss = Feed::loadRss($r2m_url);
foreach ($r2m_rss->item as $r2m_entry) {
// create file name from title
$r2m_title_article = $r2m_entry->title;
// white list letters and numbers only
$r2m_title_article_clean = preg_replace("/[^A-Za-z0-9]/",'',$r2m_title_article);
$r2m_title_article_clean = $r2m_file_prefix . substr($r2m_title_article_clean, 0, "30") . '.txt';
if (!file_exists($r2m_title_article_clean)) {
$r2m_item_output = NULL;
$r2m_fp_nodb = fopen($r2m_title_article_clean, 'w');
$r2m_item_output = $r2m_title_article;
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= htmlSpecialChars($r2m_entry->link);
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= date("j.n.Y H:i", (int) $r2m_entry->timestamp);
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= 'QUOTE: '. PHP_EOL;
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= trim(substr(strip_tags($r2m_entry->description), 0, "200"));
$r2m_item_output .= '...' . PHP_EOL;
$r2m_item_output .= PHP_EOL;
$r2m_item_output .= $r2m_mastodon_hashtags;
fwrite($r2m_fp_nodb, $r2m_item_output);
fclose($r2m_fp_nodb);
// Post our status update using files named $r2m_file_prefix
mastodon_status_update();
}
}
// Clean up old files. Comment out for rarely updated sites.
// Alternatively you can modify clean up time in the function.
clean_up_data();
}
/*
This is our main function for updating our mastodon status
*/
function mastodon_status_update()
{
global $r2m_mastodon_instance;
global $r2m_mastodon_authcode;
global $r2m_item_output;
$headers = [
'Authorization: Bearer '.$r2m_mastodon_authcode
];
$status_data = array(
"status" => "$r2m_item_output",
"language" => "eng",
"visibility" => "public"
);
$ch_status = curl_init();
curl_setopt($ch_status, CURLOPT_URL, $r2m_mastodon_instance);
curl_setopt($ch_status, CURLOPT_POST, 1);
curl_setopt($ch_status, CURLOPT_POSTFIELDS, $status_data);
curl_setopt($ch_status, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_status, CURLOPT_HTTPHEADER, $headers);
curl_exec($ch_status);
curl_close ($ch_status);
}
/*
This is our main function for cleaning up old data files.
Called by other functions. Always use last.
*/
function clean_up_data()
{
global $r2m_file_prefix;
global $r2m_old_files_delete;
foreach (glob($r2m_file_prefix."*", GLOB_ERR) as $r2m_old_files_delete) {
$r2m_filelastmodified = filemtime($r2m_old_files_delete);
//30 days a month * 24 hours in a day * 3600 seconds per hour
if ( (time() - $r2m_filelastmodified) > 60*24*3600)
{
unlink($r2m_old_files_delete);
}
}
}
?>