|
3 | 3 | // Load Composer's autoload |
4 | 4 | require_once __DIR__ . '/vendor/autoload.php'; |
5 | 5 |
|
6 | | -// Load the RSS feed |
7 | | -$feed = Feed::loadAtom('https://sharpapi.com/feed')->toArray(); |
| 6 | +try { |
| 7 | + // Load the RSS feed and convert to an array |
| 8 | + $feed = Feed::loadAtom('https://sharpapi.com/feed')->toArray(); |
| 9 | +} catch (Exception $e) { |
| 10 | + echo "Failed to load feed: ", $e->getMessage(); |
| 11 | + exit(1); |
| 12 | +} |
8 | 13 |
|
9 | | -// Generate the list of blog posts |
| 14 | +// Limit to the latest 5 posts (adjust if needed) |
10 | 15 | $posts = ''; |
11 | | -foreach (array_slice($feed['item'], 0, 555) as $post) { |
12 | | - $date = date('d/m/Y', strtotime($post['pubDate'])); |
| 16 | +foreach (array_slice($feed['item'], 0, 5) as $post) { |
| 17 | + $date = date('d/m/Y', strtotime($post['pubDate'])); |
13 | 18 | $posts .= sprintf("\n* **[%s]** [%s](%s \"%s\")", $date, $post['title'], $post['link'], $post['title']); |
14 | 19 | } |
15 | 20 |
|
16 | | -// Generate the new content |
17 | | -$content = preg_replace( |
18 | | - '#<!-- posts -->.*<!-- /posts -->#s', |
19 | | - sprintf('<!-- posts -->%s<!-- /posts -->', $posts), |
20 | | - file_get_contents('README.md') |
21 | | -); |
| 21 | +// Load README.md content |
| 22 | +$readmePath = 'README.md'; |
| 23 | +$readmeContent = file_get_contents($readmePath); |
| 24 | + |
| 25 | +// Check if README.md contains the posts section and replace or append |
| 26 | +if (strpos($readmeContent, '<!-- posts -->') !== false) { |
| 27 | + // Replace content between <!-- posts --> and <!-- /posts --> |
| 28 | + $newContent = preg_replace( |
| 29 | + '#<!-- posts -->.*<!-- /posts -->#s', |
| 30 | + sprintf('<!-- posts -->%s<!-- /posts -->', $posts), |
| 31 | + $readmeContent |
| 32 | + ); |
| 33 | +} else { |
| 34 | + // Append new posts section at the end if placeholders are missing |
| 35 | + $newContent = $readmeContent . "\n\n<!-- posts -->" . $posts . "<!-- /posts -->"; |
| 36 | +} |
| 37 | + |
| 38 | +// Write the updated content to README.md |
| 39 | +file_put_contents($readmePath, $newContent); |
22 | 40 |
|
23 | | -// Overwrite the file |
24 | | -file_put_contents('README.md', $content); |
| 41 | +echo "README.md updated successfully with the latest blog posts.\n"; |
0 commit comments