Skip to content

Commit

Permalink
🎉 Init
Browse files Browse the repository at this point in the history
  • Loading branch information
netbrothers-sw committed Jun 23, 2023
1 parent a8de08f commit 687b610
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
tmp
vendor
test
composer.lock
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# NetBrothers NbFeed
This library pulls RSS-Feed to your hard disk, transforms the items to array and save the result as json on hard disk.

## Installation
On the command prompt, change into your project's root directory and execute:

```console
composer require netbrothers-gmbh/nb-feed
```

## Configuration
*NbFeed* needs a readable and writeable directory. All other configurations are optional.
How to configure depends on your environment. See the content of [ConfigService](./src/Service/ConfigService.php).

| Variable | Description | Default |
|--------------------|-----------------------------------------------------|----------|
| *maxEntriesToSave* | How many items from the feed should be saved. | 0 (all) |
| *cacheMaxAge* | How long should the file been cached | 1800 sec |
| *storagePath* | Absolute path to a readable and writeable directory | null |
| *feedFileName* | Name of the file to write to/read from | nb-feed |

## Example
There is an example under [netbrothers-feed.php](./example/netbrothers-feed.php). To use it:
1. Checkout the repository
2. Call `composer install` after the checkout
3. Enter `php ./example/netbrothers-feed.php`
4. Feel free to manipulate the file [netbrothers-feed.php](./example/netbrothers-feed.php)

## Licence

MIT

## Authors

- [Stefan Wessel, NetBrothers GmbH](https://netbrothers.de)
- [Thilo Ratnaweera, NetBrothers GmbH](https://netbrothers.de)

[![nb.logo](https://netbrothers.de/wp-content/uploads/2020/12/netbrothers_logo.png)](https://netbrothers.de)
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "netbrothers-gmbh/nb-feed",
"description": "Get RSS-Feeds and save to json",
"keywords": ["rss"],
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Stefan Wessel",
"email": "info@netbrothers.de"
},
{
"name": "Thilo Ratnaweera",
"email": "info@netbrothers.de"
}
],
"require": {
"php": ">=8.0",
"ext-curl": "*",
"ext-json": "*",
"ext-simplexml": "*"
},
"autoload": {
"psr-4": {
"NetBrothers\\NbFeed\\": "src/"
}
}
}
54 changes: 54 additions & 0 deletions example/netbrothers-feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* NbFeed
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 23.06.23
*/

/**
* To use this example, call `composer install` after the checkout.
* The script tries to load the `autoload.php` automatically. If this does not work, please set the path manually.
*/
$libModeAutoload = __DIR__ . '/../../../autoload.php';
if (file_exists($libModeAutoload)) {
// used as a binary script of a composer package (vendor/bin/nb-feed)
require $libModeAutoload;
} else {
// used entirely standalone (e.g. git clone)
require __DIR__ . '/../vendor/autoload.php';
}

/**
* Make some Configurations
* How you set the configuration depends on the system you are using. Here, as an example, simply by hand.
*/

// URL RSS-Feed (required)
$feedUrl = 'https://www.heise.de/security/rss/alert-news.rdf';

// Init ConfigService and set some values
$configService = new \NetBrothers\NbFeed\Service\ConfigService();
$configService->setStoragePath(__DIR__ . '/../tmp');
$configService->setFeedFileName('heise-security');
$configService->setCacheMaxAge(300);


/**
* Now using the logic
*/
$feedService = new \NetBrothers\NbFeed\Service\FeedService($configService);

/**
* Now getting the Feeds:
* => As we have never loaded anything before, the feed is now being pulled, manipulated and saved on disk
* => If there is a file on the hard disk that is not older than allowed, the results are pulled from the cache.
*/
$feedArray = $feedService->getFeed($feedUrl, true);
print PHP_EOL;
var_dump($feedArray);
print PHP_EOL;
unset($feedArray);



37 changes: 37 additions & 0 deletions src/Helper/CurlHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* NbFeed
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 23.06.23
*/

namespace NetBrothers\NbFeed\Helper;

/**
* Class CurlHelper
* @package NetBrothers\NbFeed\Helper
*/
class CurlHelper
{
/** get feed from url via curl and save response to file
*
* @param string $feedUrl Url to fetch
* @param string $file Save content to this file
* @return void
* @throws \Exception
*/
public static function getFeedWithCurl(string $feedUrl, string $file): void
{
$ch = curl_init($feedUrl);
$fp = fopen($file, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
if(curl_error($ch)) {
throw new \Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
fclose($fp);
}
}
48 changes: 48 additions & 0 deletions src/Helper/StorageHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* NbFeed
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 23.06.23
*/

namespace NetBrothers\NbFeed\Helper;

/**
* Class StorageHelper
* @package NetBrothers\NbFeed\Helper
*/
class StorageHelper
{

/**
* @param string $path
* @return string
* @throws \RuntimeException
*/
public static function createPath(string $path): string
{
if (!is_dir($path) && !mkdir($path, 0777, true)) {
throw new \RuntimeException(sprintf('Cannot create %s', $path));
} elseif (!(is_writable($path) && is_readable($path))) {
throw new \RuntimeException(sprintf('Check permissions for writing and/or reading in %s', $path));
}
return (true !== str_ends_with($path, "/")) ? $path . "/" : $path;
}

/**
* @param string $file
* @return void
* @throws \RuntimeException
*/
public static function removeFile(string $file): void
{
if (file_exists($file)) {
if (is_readable($file) && is_writable($file)) {
unlink($file);
} else {
throw new \RuntimeException('Cannot remove file: ' . $file);
}
}
}
}
111 changes: 111 additions & 0 deletions src/Service/ConfigService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php
/**
* NbFeed
*
* @author Stefan Wessel, NetBrothers GmbH
* @date 23.06.23
*/

namespace NetBrothers\NbFeed\Service;
use NetBrothers\NbFeed\Helper\StorageHelper;
/**
* Class ConfigService
* @package NetBrothers\NbFeed\Service
*/
class ConfigService
{

/** How many items to save
*
* set to 0 to save all
*
* @var int
*/
private int $maxEntriesToSave = 0;

/** cache in seconds
*
* If there is a file on the hard disk that is not older than allowed, the results are pulled from the cache
*
* @var int Seconds
*/
private int $cacheMaxAge = 1800;


/** Storage-Path for saving files
*
* @var string|null
*/
private ?string $storagePath = null;

/**
* @var string
*/
private string $feedFileName = 'nb-feed';

/**
* @return int
*/
public function getMaxEntriesToSave(): int
{
return $this->maxEntriesToSave;
}

/**
* @param int $maxEntriesToSave
*/
public function setMaxEntriesToSave(int $maxEntriesToSave): void
{
$this->maxEntriesToSave = $maxEntriesToSave;
}

/**
* @return int
*/
public function getCacheMaxAge(): int
{
return $this->cacheMaxAge;
}

/**
* @param int $cacheMaxAge
*/
public function setCacheMaxAge(int $cacheMaxAge): void
{
$this->cacheMaxAge = $cacheMaxAge;
}

/**
* @return string|null
*/
public function getStoragePath(): ?string
{
return $this->storagePath;
}

/**
* @param string $storagePath
* @throws \RuntimeException
*/
public function setStoragePath(string $storagePath): void
{
$this->storagePath = StorageHelper::createPath($storagePath);
}

/**
* @return string
*/
public function getFeedFileName(): string
{
return $this->feedFileName;
}

/**
* @param string $feedFileName
*/
public function setFeedFileName(string $feedFileName): void
{
$this->feedFileName = $feedFileName;
}

}
Loading

0 comments on commit 687b610

Please sign in to comment.