| Server IP : 104.21.21.239 / Your IP : 216.73.217.39 Web Server : Apache/2.4.68 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-69-123.ec2.internal 6.1.176-223.369.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Jul 24 13:34:27 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/banners/public_html/bway/oldfeeds/ |
Upload File : |
<?php
// Set a timeout limit for the script execution
set_time_limit(900);
require_once __DIR__ . '/mysql_tls.php';
// Database connection (read for checks)
$linkRead = oldfeeds_mysqli_connect('amazonaurora.cluster-ro-cemzxojvmybt.us-east-1.rds.amazonaws.com', 'admin', 'xxatN6Lb8Kbwb9MiU1At');
mysqli_select_db($linkRead, 'amazonrds');
// Database connection (write for inserts)
$linkWrite = oldfeeds_mysqli_connect('amazonaurora.cluster-cemzxojvmybt.us-east-1.rds.amazonaws.com', 'admin', 'xxatN6Lb8Kbwb9MiU1At');
mysqli_select_db($linkWrite, 'amazonrds');
$insertedCount = 0;
$skippedCount = 0;
$errorCount = 0;
$processedPosts = 0;
$allComments = [];
$commentPosts = [];
// Fetch RSS Feed from r/Broadway - Titanique Search
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://old.reddit.com/r/broadway/search.rss?q=titanique&sort=new&restrict_sr=on&t=all');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; BroadwayWorld/1.0; +https://www.broadwayworld.com)');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/rss+xml,application/atom+xml,application/xml,text/xml,*/*',
'Accept-Language: en-US,en;q=0.9'
]);
$feedContent = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Debug: show what we got back
echo "<strong>HTTP Status:</strong> " . $httpCode . "<br>";
echo "<strong>Content Length:</strong> " . strlen($feedContent) . " bytes<br>";
echo "<strong>First 500 chars:</strong><br>";
echo "<pre>" . htmlspecialchars(substr($feedContent, 0, 500)) . "</pre>";
echo "<br>";
try {
// Parse the Titanique search RSS feed to get posts
if (!empty($feedContent)) {
$feedXML = simplexml_load_string($feedContent);
if ($feedXML === false) {
throw new Exception("Failed to parse XML feed.");
}
// Use xpath to get all entry elements (handles namespaces properly)
$posts = $feedXML->xpath("//*[local-name()='entry']");
if (count($posts) > 0) {
echo "<strong>Found " . count($posts) . " entries in Titanique RSS feed</strong><br>";
// Extract only valid Reddit post URLs (skip subreddit entries)
foreach ($posts as $post) {
$postLink = "";
$postId = "";
// Use xpath to find child elements within each entry
$linkNodes = $post->xpath("*[local-name()='link']");
$idNodes = $post->xpath("*[local-name()='id']");
$categoryNodes = $post->xpath("*[local-name()='category']");
if (count($linkNodes) > 0 && isset($linkNodes[0]['href'])) {
$postLink = str_replace("old.reddit.com", "www.reddit.com", (string)$linkNodes[0]['href']);
}
if (count($idNodes) > 0) {
$postId = (string)$idNodes[0];
}
// Debug output to see what we're getting
echo "Entry ID: " . htmlspecialchars($postId) . " | Link: " . htmlspecialchars($postLink) . "<br>";
// Only process entries that are actual Reddit posts
if (strpos($postLink, "/comments/") !== false || substr($postId, 0, 3) === "t3_") {
// If the link doesn't have /comments/, try to construct it from the ID
if (strpos($postLink, "/comments/") === false && substr($postId, 0, 3) === "t3_") {
$shortId = str_replace("t3_", "", $postId);
$subreddit = "Broadway"; // default
if (count($categoryNodes) > 0 && isset($categoryNodes[0]['term'])) {
$subreddit = (string)$categoryNodes[0]['term'];
}
$postLink = "https://www.reddit.com/r/" . $subreddit . "/comments/" . $shortId . "/";
}
$commentPosts[] = $postLink;
echo " --> Added to process list<br>";
}
}
echo "<strong>Found " . count($commentPosts) . " valid Reddit posts to process</strong><br><br>";
if (count($commentPosts) == 0) {
echo "No valid Reddit posts found in feed. Nothing to process.<br>";
exit;
}
} else {
echo "No entry elements found in RSS feed.<br>";
exit;
}
} else {
echo "Failed to fetch RSS feed.<br>";
exit;
}
} catch (Exception $e) {
echo "An error occurred while fetching the RSS feed: " . $e->getMessage() . "<br>";
exit;
}
if (count($commentPosts) > 0) {
// PHASE 1: Fetch all comments from RSS feeds
echo "<strong>Phase 1: Fetching comments from Reddit RSS feeds...</strong><br><br>";
for ($i = 0; $i < count($commentPosts); $i++) {
$commentFeedURL = trim($commentPosts[$i]) . ".rss";
echo "Processing " . ($i + 1) . " of " . count($commentPosts) . ": " . htmlspecialchars($commentFeedURL) . "<br>";
flush();
try {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $commentFeedURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/atom+xml,application/xml,text/xml,*/*'
]);
$commentFeedContent = curl_exec($ch);
$commentHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (!empty($commentFeedContent)) {
try {
$commentXML = simplexml_load_string($commentFeedContent);
if ($commentXML === false) {
throw new Exception("Failed to parse comment feed XML.");
}
// Use xpath to get all entry elements
$comments = $commentXML->xpath("//*[local-name()='entry']");
if (count($comments) > 0) {
echo "Found " . count($comments) . " comments<br>";
foreach ($comments as $comment) {
$contentNodes = $comment->xpath("*[local-name()='content']");
$titleNodes = $comment->xpath("*[local-name()='title']");
$linkNodes = $comment->xpath("*[local-name()='link']");
$authorNodes = $comment->xpath("*[local-name()='author']");
$idNodes = $comment->xpath("*[local-name()='id']");
$updatedNodes = $comment->xpath("*[local-name()='updated']");
$commentContent = "";
$commentTitle = "";
$commentLink = "";
$commentAuthor = "";
$commentId = "";
$commentUpdated = "";
if (count($contentNodes) > 0) {
$commentContent = (string)$contentNodes[0];
}
if (count($titleNodes) > 0) {
$commentTitle = (string)$titleNodes[0];
}
if (count($linkNodes) > 0 && isset($linkNodes[0]['href'])) {
$commentLink = str_replace("old.reddit.com", "www.reddit.com", (string)$linkNodes[0]['href']);
}
if (count($authorNodes) > 0) {
$nameNodes = $authorNodes[0]->xpath("*[local-name()='name']");
if (count($nameNodes) > 0) {
$commentAuthor = (string)$nameNodes[0];
}
}
if (count($idNodes) > 0) {
$commentId = (string)$idNodes[0];
}
if (count($updatedNodes) > 0) {
$commentUpdated = (string)$updatedNodes[0];
}
// Only add if we have a valid link (skip entries without proper links)
if (!empty($commentLink) && strpos($commentLink, "/comments/") !== false) {
$allComments[] = [
'reddit_link' => $commentPosts[$i],
'content' => $commentContent,
'title' => $commentTitle,
'link' => $commentLink,
'author' => $commentAuthor,
'reddit_id' => $commentId,
'updated' => $commentUpdated
];
}
}
$processedPosts++;
} else {
echo "No entry elements found in feed<br>";
}
} catch (Exception $e) {
$errorCount++;
echo "<strong>Error parsing comments for:</strong> " . htmlspecialchars($commentFeedURL) . "<br>";
echo "Error: " . $e->getMessage() . "<br>";
}
} else {
echo "No content in response or empty response<br>";
echo "Status Code: " . $commentHttpCode . "<br>";
}
} catch (Exception $e) {
$errorCount++;
echo "<strong>Error fetching:</strong> " . htmlspecialchars($commentFeedURL) . "<br>";
echo "Error: " . $e->getMessage() . "<br>";
}
echo "<br>";
}
// PHASE 2: Batch check and insert
echo "<br><strong>Phase 2: Checking for existing comments and inserting new ones...</strong><br>";
echo "Total comments fetched: " . count($allComments) . "<br><br>";
flush();
if (count($allComments) > 0) {
echo "Inserting comments...<br>";
foreach ($allComments as $comment) {
try {
// First check if the comment already exists
$checkQuery = "SELECT COUNT(*) AS commentCount FROM reddit_comments WHERE link = '" . mysqli_real_escape_string($linkRead, $comment['link']) . "'";
$existingComment = mysqli_query($linkRead, $checkQuery);
if (!$existingComment) {
echo "Query error: " . mysqli_error($linkRead) . "<br>";
$errorCount++;
continue;
}
$existingRow = mysqli_fetch_assoc($existingComment);
if ($existingRow['commentCount'] == 0) {
// Insert new comment
$insertQuery = "INSERT INTO reddit_comments (reddit_link, content, title, link)
VALUES ('" . mysqli_real_escape_string($linkWrite, $comment['reddit_link']) . "', '" . mysqli_real_escape_string($linkWrite, $comment['content']) . "', '" . mysqli_real_escape_string($linkWrite, $comment['title']) . "', '" . mysqli_real_escape_string($linkWrite, $comment['link']) . "')";
mysqli_query($linkWrite, $insertQuery);
$insertedCount++;
echo "Inserted: " . htmlspecialchars(substr($comment['title'], 0, 50)) . "...<br>";
} else {
$skippedCount++;
}
} catch (Exception $e) {
$errorCount++;
echo "Error inserting comment: " . $e->getMessage() . "<br>";
}
}
echo "Batch insert complete<br>";
}
echo "<br><strong>Summary:</strong><br>";
echo "Posts processed successfully: " . $processedPosts . " of " . count($commentPosts) . "<br>";
echo "Comments inserted: " . $insertedCount . "<br>";
echo "Comments skipped (duplicates): " . $skippedCount . "<br>";
echo "Errors: " . $errorCount . "<br>";
echo "<br>Reddit comments processed successfully!";
}
// Close database connections
mysqli_close($linkRead);
mysqli_close($linkWrite);
?>