From 3740de74d5f47d6623291f92320c60eb5c0fbdb4 Mon Sep 17 00:00:00 2001 From: Phantop Date: Mon, 2 Sep 2024 01:22:47 -0400 Subject: [PATCH] [GovTrackBridge] leaner items array and limit implementation --- bridges/GovTrackBridge.php | 40 ++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/bridges/GovTrackBridge.php b/bridges/GovTrackBridge.php index 22e6314e..4674668a 100644 --- a/bridges/GovTrackBridge.php +++ b/bridges/GovTrackBridge.php @@ -34,17 +34,18 @@ class GovTrackBridge extends BridgeAbstract $html = defaultLinkTo($html, parent::getURI()); $limit = $this->getInput('limit') ?? 10; - $count = 0; foreach ($html->find('section') as $element) { - $item = []; - - $item['title'] = $element->find('a', 0)->innertext; - $item['uri'] = $element->find('a', 0)->href; - $item['content'] = $element->find('p', 1); + if (--$limit == 0) { + break; + } $info = explode(' ', $element->find('p', 0)->innertext); - $item['timestamp'] = strtotime(implode(' ', array_slice($info, 0, 3))); - $item['categories'][] = implode(' ', array_slice($info, 4)); + $item = [ + 'categories' => [implode(' ', array_slice($info, 4))], + 'timestamp' => strtotime(implode(' ', array_slice($info, 0, 3))), + 'title' => $element->find('a', 0)->innertext, + 'uri' => $element->find('a', 0)->href, + ]; $html = getSimpleHTMLDOMCached($item['uri']); $html = defaultLinkTo($html, parent::getURI()); @@ -56,10 +57,6 @@ class GovTrackBridge extends BridgeAbstract $item['author'] = implode(' ', array_slice($info, 1)); $item['content'] = $content->innertext; - if ($count++ == $limit) { - break; - } - $this->items[] = $item; } } @@ -84,24 +81,25 @@ class GovTrackBridge extends BridgeAbstract $html = defaultLinkTo(str_get_html($html), parent::getURI()); foreach ($html->find('.tracked_event') as $event) { - $item = []; - $bill = $event->find('.event_title a, .event_body a', 0); - preg_match('/Sponsor:(.*)\n/', $event->plaintext, $opt); $date = explode(' ', $event->find('.event_date', 0)->plaintext); + preg_match('/Sponsor:(.*)\n/', $event->plaintext, $opt); - $item['author'] = $opt[1] ?? ''; - $item['content'] = $event->find('td', 1)->innertext; - $item['enclosures'][] = $event->find('img', 0)->src; - $item['timestamp'] = strtotime(implode(' ', array_slice($date, 2))); - $item['title'] = explode(': ', $bill->innertext)[0]; - $item['uri'] = $bill->href; + $item = [ + 'author' => $opt[1] ?? '', + 'content' => $event->find('td', 1)->innertext, + 'enclosures' => [$event->find('img', 0)->src], + 'timestamp' => strtotime(implode(' ', array_slice($date, 2))), + 'title' => explode(': ', $bill->innertext)[0], + 'uri' => $bill->href, + ]; foreach ($event->find('.event_title, .event_type span') as $tag) { if (!$tag->find('a', 0)) { $item['categories'][] = $tag->plaintext; } } + $this->items[] = $item; } }