-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.php
55 lines (44 loc) · 1.66 KB
/
posts.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
<?php
// Load Composer's autoload
require_once __DIR__ . '/vendor/autoload.php';
use Suin\RSSWriter\Feed;
// Load the feed
try {
$feedUrl = 'https://sharpapi.com/feed';
$xmlContent = file_get_contents($feedUrl);
if (!$xmlContent) {
throw new Exception("Failed to load feed from $feedUrl");
}
$xml = new SimpleXMLElement($xmlContent);
} catch (Exception $e) {
echo "Failed to load feed: ", $e->getMessage();
exit(1);
}
// Initialize the posts list
$posts = '';
// Iterate over each entry in the feed
foreach ($xml->entry as $entry) {
$title = (string) $entry->title;
$link = (string) $entry->link['href'];
$date = date('d/m/Y', strtotime($entry->updated));
$summary = strip_tags((string) $entry->summary); // Remove any HTML tags for a clean summary
// Append each entry to the posts list
//$posts .= sprintf("\n* **[%s]** [%s](%s \"%s\")\n > %s", $date, $title, $link, $title, $summary);
$posts .= sprintf("\n* **[%s]** [%s](%s \"%s\")\n > %s\n\n", $date, $title, $link, $title, trim($summary));
}
// Load README.md content
$readmePath = 'README.md';
$readmeContent = file_get_contents($readmePath);
// Replace or append the posts section in README.md
if (strpos($readmeContent, '<!-- posts -->') !== false) {
$newContent = preg_replace(
'#<!-- posts -->.*<!-- /posts -->#s',
sprintf('<!-- posts -->%s<!-- /posts -->', $posts),
$readmeContent
);
} else {
$newContent = $readmeContent . "\n\n<!-- posts -->" . $posts . "<!-- /posts -->";
}
// Write the updated content to README.md
file_put_contents($readmePath, $newContent);
echo "README.md updated successfully with all blog posts.\n";