rss-bridge/bridges/NewgroundsBridge.php

82 lines
2.1 KiB
PHP
Raw Normal View History

2022-06-24 12:59:24 +03:00
<?php
2022-06-24 12:59:24 +03:00
declare(strict_types=1);
class NewgroundsBridge extends BridgeAbstract
{
const NAME = 'Newgrounds';
const URI = 'https://www.newgrounds.com';
const DESCRIPTION = 'Get the latest art from a given user';
const MAINTAINER = 'KamaleiZestri';
const PARAMETERS = [
'User' => [
'username' => [
'name' => 'Username',
'type' => 'text',
'required' => true,
'exampleValue' => 'TomFulp'
]
]
];
2022-06-24 12:59:24 +03:00
public function collectData()
{
$username = $this->getInput('username');
if (!preg_match('/^\w+$/', $username)) {
throw new \Exception('Illegal username');
}
2022-06-24 12:59:24 +03:00
$html = getSimpleHTMLDOM($this->getURI());
2022-06-24 12:59:24 +03:00
$posts = $html->find('.item-portalitem-art-medium');
2022-06-24 12:59:24 +03:00
foreach ($posts as $post) {
$item = [];
2022-06-24 12:59:24 +03:00
$item['author'] = $username;
$item['uri'] = $post->href;
2022-06-24 12:59:24 +03:00
$titleOrRestricted = $post->find('h4')[0]->innertext;
2022-06-24 12:59:24 +03:00
// Newgrounds doesn't show public previews for NSFW content.
if ($titleOrRestricted === 'Restricted Content: Sign in to view!') {
$item['title'] = 'NSFW: ' . $item['uri'];
$item['content'] = <<<EOD
2022-06-24 12:59:24 +03:00
<a href="{$item['uri']}">
{$item['title']}
</a>
EOD;
} else {
$item['title'] = $titleOrRestricted;
$item['content'] = <<<EOD
2022-06-24 12:59:24 +03:00
<a href="{$item['uri']}">
<img
style="align:top; width:270px; border:1px solid black;"
alt="{$item['title']}"
src="{$post->find('img')[0]->src}"
title="{$item['title']}" />
</a>
EOD;
}
2022-06-24 12:59:24 +03:00
$this->items[] = $item;
}
}
2022-06-24 12:59:24 +03:00
public function getName()
{
if ($this->getInput('username')) {
return sprintf('%s - %s', $this->getInput('username'), self::NAME);
}
return parent::getName();
}
2022-06-24 12:59:24 +03:00
public function getURI()
{
if ($this->getInput('username')) {
return sprintf('https://%s.newgrounds.com/art', $this->getInput('username'));
}
return parent::getURI();
}
2022-06-24 12:59:24 +03:00
}