1
0
Files
Bridges/CosppiBridge.php
2025-07-03 21:58:26 +02:00

156 lines
5.4 KiB
PHP

<?php
class CosppiBridge extends BridgeAbstract
{
const MAINTAINER = 'Brawl, GPT-4';
const NAME = 'Cosppi';
const URI = 'https://cosppi.net/';
const CACHE_TIMEOUT = 10800; // 10800 = 3h
const DESCRIPTION = 'Tweets from Cosplayers scraped by Cosppi';
const PARAMETERS = [
'global' => [
'sort' => [
'name' => 'Sort by',
'type' => 'list',
'required' => true,
'values' => [
'Latest' => 'new',
'Popularity' => 'rank'
]
]
],
'By Search Query' => [
'query' => [
'name' => 'Query',
'title' => 'See https://cosppi.net/tag-list',
'type' => 'text',
'required' => true,
'exampleValue' => '雷電将軍',
]
],
'By User' => [
'user' => [
'name' => 'Username',
'title' => 'See https://cosppi.net/sort/all-rank',
'type' => 'text',
'required' => true,
'exampleValue' => 'enako_cos',
]
]
];
private $query = "";
public function getName()
{
if (empty($this->queriedContext)) {
return parent::getName();
}
switch ($this->queriedContext) {
case 'By Search Query':
return parent::getName() . ': ' . $this->query;
case 'By User':
return parent::getName() . ' (' . $this->query . ')';
default:
returnServerError('Unimplemented Context (getName)');
}
return parent::getName();
}
public function getURI()
{
if (empty($this->queriedContext)) {
return parent::getURI();
}
switch ($this->queriedContext) {
case 'By Search Query':
return parent::getURI() . 'search-images?word=' . $this->query . '&sort=' . $this->getInput('sort');
case 'By User':
return parent::getURI() . 'user/' . $this->query . '?sort=' . $this->getInput('sort');
default:
returnServerError('Unimplemented Context (getURI)');
}
return parent::getURI();
}
public function getIcon()
{
return 'https://cosppi.net/wp-content/uploads/2020/01/cropped-favicon-1-192x192.png';
}
public function collectData()
{
// Retrieve webpage
$this->query = $this->getInput('query') ?: $this->getInput('user');
$pageUrl = $this->getURI();
$html = getSimpleHTMLDOM($pageUrl)
or returnServerError('Could not request webpage: ' . $pageUrl);
// Process articles
foreach ($html->find('div.img_wrapper') as $element) {
if (count($this->items) >= 10) {
break;
}
$name = "";
$username = "";
switch ($this->queriedContext) {
case 'By Search Query':
$profile_area = $element->find('div.all_tweet_profile_wrap', 0);
$name = trim(strip_tags($profile_area->find('div.all_tweet_profile_name', 0)->innertext));
$username = trim(strip_tags($profile_area->find('div.all_tweet_profile_screenName', 0)->innertext));
break;
case 'By User':
$profile_area = $html->find('div.prof_right_wrap', 0);
$name = trim(strip_tags($profile_area->find('div.prof_name', 0)->innertext));
$username = trim(strip_tags($profile_area->find('div.prof_scname', 0)->innertext));
break;
default:
returnServerError('Unimplemented Context (collectData)');
}
$media_link_element = $element->find('.img_a', 0);
$media_link = $media_link_element->getAttribute('data-link') ?: $media_link_element->href;
$media_type = $media_link_element->getAttribute('data-link') ? 'video' : 'image';
$date_str = trim(strip_tags($element->find('div.img_footer span', 0)->innertext));
$title = trim(strip_tags($element->find('div.tweet_text', 0)->innertext));
$tweet_link_element = $element->find('div.img_footer a', 0);
$tweet_link = $tweet_link_element ? $tweet_link_element->href : $media_link;
// Constructing the title
$article_title = $name . " (" . $username . ")";
if (!empty($title)) {
$article_title .= ": " . $title;
}
// Convert date_str to timestamp
$timestamp = strtotime($date_str);
// Store article in items array
if (!empty($name)) {
$item = array();
$item['uid'] = $tweet_link;
$item['uri'] = $tweet_link;
$item['title'] = $article_title;
$item['content'] = $title . '<br>';
if ($media_type === 'image') {
$item['content'] .= '<img src="' . $media_link . ':orig">';
} else {
$item['content'] .= '<video controls><source src="' . $media_link . '" type="video/mp4"></video>';
}
// $item['enclosures'] = array($image_link);
$item['timestamp'] = $timestamp;
$this->items[] = $item;
}
}
}
}