From 76fd21bccd1b87916f149a257e07d02f5e40335a Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Tue, 2 Apr 2013 13:48:16 +0200 Subject: added simple twitter fetcher implementation --- utility/feedfetcher.php | 2 ++ utility/twitterfetcher.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 utility/twitterfetcher.php (limited to 'utility') diff --git a/utility/feedfetcher.php b/utility/feedfetcher.php index 54c416d71..284c40ba7 100644 --- a/utility/feedfetcher.php +++ b/utility/feedfetcher.php @@ -55,6 +55,7 @@ class FeedFetcher implements IFeedFetcher { * @return array an array containing the new feed and its items */ public function fetch($url) { + // TODO: write unittests! $simplePie = new \SimplePie_Core(); $simplePie->set_feed_url( $url ); $simplePie->enable_cache( false ); @@ -158,6 +159,7 @@ class FeedFetcher implements IFeedFetcher { $page = $this->api->getUrlContent($absoluteUrl); if ( FALSE !== $page ) { + // FIXME: dont use regex to get xml, use xpath! preg_match ( '/<[^>]*link[^>]*(rel=["\']icon["\']|rel=["\']shortcut icon["\']) .*href=["\']([^>]*)["\'].*>/iU', $page, $match ); if (1. +* +*/ + +namespace OCA\News\Utility; + + +class TwitterFetcher implements IFeedFetcher { + + + private $fetcher; + private $regex; + + // FIXME: implement twitter api to be future proof + public function __construct(FeedFetcher $fetcher){ + $this->fetcher = $fetcher; + $this->regex = '/^(?:https?:\/\/)?(?:www\.)?' . + 'twitter.com\/([\pL\pN\pM]+)$/u'; + } + + + public function canHandle($url){ + return preg_match($this->regex, $url) == true; + } + + + public function fetch($url){ + preg_match($this->regex, $url, $match); + $rssUrl = 'https://api.twitter.com/1/statuses/user_timeline.' . + 'rss?screen_name=' . $match[1]; + return $this->fetcher->fetch($rssUrl); + } + + +} \ No newline at end of file -- cgit v1.2.3