Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zoranbogoevski committed Feb 3, 2024
1 parent 266d7d9 commit 92b2595
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .idea/RssFeed.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions src/RssFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function parseRssFeeds(array $feedUrls, $jobId = null): array
$fullContent = $this->retrieveFullContent($itemLink);

// Save the image to storage
$images = $this->saveImageToStorage($fullContent);
$images = $this->saveImagesToStorage($fullContent['images']);

// Add the extracted item data to the parsedItems array
$parsedItems[] = [
Expand All @@ -71,15 +71,15 @@ public function parseRssFeeds(array $feedUrls, $jobId = null): array
* @return array|bool
* @throws CantOpenFileFromUrlException
*/
public function saveImageToStorage(array $images): array
public function saveImagesToStorage(array $images): array
{
$savedImageNames = [];

foreach ($images as $image) {
$file = UrlUploadedFile::createFromUrl($image);
$imageName = Str::random(15) . '.' . $file->extension();
$file->storeAs('images', $imageName, 'public');
$savedImageNames[] = $imageName;
$file = UrlUploadedFile::createFromUrl($image);
$imageName = Str::random(15) . '.' . $file->extension();
$file->storeAs('images', $imageName, 'public');
$savedImageNames[] = $imageName;

}

Expand Down Expand Up @@ -109,9 +109,10 @@ public function retrieveFullContent(string $postLink): bool|array
// Initialize an array to hold the image URLs
$imageUrls = [];
$selectedContent = '';
$contentElementXPaths = config('rssfeed.content_element_xpaths', []);

// Process each XPath query in the configuration
foreach ($config['content_element_xpaths'] as $xpathQuery) {
foreach ($contentElementXPaths as $xpathQuery) {
$elements = $xpath->query($xpathQuery);

// Check if elements were found for the current XPath query
Expand All @@ -130,7 +131,7 @@ public function retrieveFullContent(string $postLink): bool|array
}
}

// Optionally, you might want to remove <script> and <style> from $selectedContent
// remove <script> and <style> from $selectedContent
$selectedContent = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $selectedContent);
$selectedContent = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', "", $selectedContent);

Expand All @@ -140,6 +141,11 @@ public function retrieveFullContent(string $postLink): bool|array
'images' => $imageUrls, // This is an array of image URLs found in the selected content
];
}




// The cURL fetching function from previous examples
private function fetchContentUsingCurl(string $url): bool|string
{
$ch = curl_init();
Expand All @@ -153,4 +159,6 @@ private function fetchContentUsingCurl(string $url): bool|string

return $httpCode === 200 ? $data : false;
}


}

0 comments on commit 92b2595

Please sign in to comment.