|
3 | 3 | // Load Composer's autoload |
4 | 4 | require_once __DIR__ . '/vendor/autoload.php'; |
5 | 5 |
|
| 6 | +use Suin\RSSWriter\Feed; |
| 7 | + |
| 8 | +// Load the feed |
6 | 9 | try { |
7 | | - // Load the RSS feed and convert it to an array |
8 | | - $feed = Feed::loadAtom('https://sharpapi.com/feed')->toArray(); |
| 10 | + $feedUrl = 'https://sharpapi.com/feed'; |
| 11 | + $xmlContent = file_get_contents($feedUrl); |
| 12 | + if (!$xmlContent) { |
| 13 | + throw new Exception("Failed to load feed from $feedUrl"); |
| 14 | + } |
| 15 | + $xml = new SimpleXMLElement($xmlContent); |
9 | 16 | } catch (Exception $e) { |
10 | 17 | echo "Failed to load feed: ", $e->getMessage(); |
11 | 18 | exit(1); |
12 | 19 | } |
13 | 20 |
|
14 | | -// Initialize an empty string to store the formatted posts |
| 21 | +// Initialize the posts list |
15 | 22 | $posts = ''; |
16 | | -foreach ($feed['entry'] as $post) { |
17 | | - // Extract title, link, and summary |
18 | | - $title = $post['title'] ?? 'No title'; |
19 | | - $link = $post['link'][0]['@attributes']['href'] ?? '#'; |
20 | | - $description = $post['summary'] ?? ''; |
21 | | - $date = date('d/m/Y', strtotime($post['updated'])); |
22 | | - |
23 | | - // Append each post's details in the required format |
24 | | - $posts .= sprintf( |
25 | | - "\n* **[%s]** [%s](%s \"%s\")\n > %s\n", |
26 | | - $date, |
27 | | - strip_tags($title), |
28 | | - $link, |
29 | | - strip_tags($title), |
30 | | - strip_tags($description) |
31 | | - ); |
| 23 | + |
| 24 | +// Iterate over each entry in the feed |
| 25 | +foreach ($xml->entry as $entry) { |
| 26 | + $title = (string) $entry->title; |
| 27 | + $link = (string) $entry->link['href']; |
| 28 | + $date = date('d/m/Y', strtotime($entry->updated)); |
| 29 | + $summary = strip_tags((string) $entry->summary); // Remove any HTML tags for a clean summary |
| 30 | + |
| 31 | + // Append each entry to the posts list |
| 32 | + $posts .= sprintf("\n* **[%s]** [%s](%s \"%s\")\n > %s", $date, $title, $link, $title, $summary); |
32 | 33 | } |
33 | 34 |
|
34 | 35 | // Load README.md content |
35 | 36 | $readmePath = 'README.md'; |
36 | 37 | $readmeContent = file_get_contents($readmePath); |
37 | 38 |
|
38 | | -// Check if README.md contains the posts section and replace or append |
| 39 | +// Replace or append the posts section in README.md |
39 | 40 | if (strpos($readmeContent, '<!-- posts -->') !== false) { |
40 | | - // Replace content between <!-- posts --> and <!-- /posts --> |
41 | 41 | $newContent = preg_replace( |
42 | 42 | '#<!-- posts -->.*<!-- /posts -->#s', |
43 | 43 | sprintf('<!-- posts -->%s<!-- /posts -->', $posts), |
44 | 44 | $readmeContent |
45 | 45 | ); |
46 | 46 | } else { |
47 | | - // Append new posts section at the end if placeholders are missing |
48 | 47 | $newContent = $readmeContent . "\n\n<!-- posts -->" . $posts . "<!-- /posts -->"; |
49 | 48 | } |
50 | 49 |
|
|
0 commit comments