Skip to content

Commit 6d57931

Browse files
authored
Update posts.php
1 parent dffb9e6 commit 6d57931

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

posts.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,47 @@
33
// Load Composer's autoload
44
require_once __DIR__ . '/vendor/autoload.php';
55

6+
use Suin\RSSWriter\Feed;
7+
8+
// Load the feed
69
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);
916
} catch (Exception $e) {
1017
echo "Failed to load feed: ", $e->getMessage();
1118
exit(1);
1219
}
1320

14-
// Initialize an empty string to store the formatted posts
21+
// Initialize the posts list
1522
$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);
3233
}
3334

3435
// Load README.md content
3536
$readmePath = 'README.md';
3637
$readmeContent = file_get_contents($readmePath);
3738

38-
// Check if README.md contains the posts section and replace or append
39+
// Replace or append the posts section in README.md
3940
if (strpos($readmeContent, '<!-- posts -->') !== false) {
40-
// Replace content between <!-- posts --> and <!-- /posts -->
4141
$newContent = preg_replace(
4242
'#<!-- posts -->.*<!-- /posts -->#s',
4343
sprintf('<!-- posts -->%s<!-- /posts -->', $posts),
4444
$readmeContent
4545
);
4646
} else {
47-
// Append new posts section at the end if placeholders are missing
4847
$newContent = $readmeContent . "\n\n<!-- posts -->" . $posts . "<!-- /posts -->";
4948
}
5049

0 commit comments

Comments
 (0)