2017-04-21 19:45:58 +00:00
# vim: set foldmarker={,}:
use strict ;
2017-04-23 18:51:48 +00:00
use RSSTootalizer::Base ;
2017-04-21 19:45:58 +00:00
2017-04-23 18:51:48 +00:00
package RSSTootalizer::Feed ;
@ RSSTootalizer:: Feed:: ISA = qw( RSSTootalizer::Base ) ;
2017-04-21 19:45:58 +00:00
use JSON ;
use Data::Dumper ;
2017-04-23 18:51:48 +00:00
use RSSTootalizer::Filter ;
use RSSTootalizer::Entry ;
2017-04-22 19:28:21 +00:00
use XML::Feed ;
use URI ;
2017-04-21 19:45:58 +00:00
sub dbTable :lvalue { "feeds" ; }
2017-04-24 11:23:51 +00:00
sub orderBy :lvalue { "ID ASC" ; }
2017-04-21 19:45:58 +00:00
# Class functions
sub get_by_user_instance {
my $ class = shift ;
my $ user = shift ;
my $ instance = shift ;
2017-04-23 18:51:48 +00:00
my $ retVal = RSSTootalizer::DB - > doSELECT ( "SELECT * FROM " . $ class - > dbTable . " WHERE username = ? AND instance = ? ORDER BY " . $ class - > orderBy , $ user , $ instance ) ;
2017-04-21 19:45:58 +00:00
my @ retVal ;
foreach my $ r ( @$ retVal ) {
push @ retVal , $ class - > new ( $ r ) ;
}
return @ retVal ;
}
2017-04-22 19:28:21 +00:00
sub create_and_fetch {
my $ class = shift ;
my % data = @ _ ;
my $ self = $ class - > create ( % data ) ;
my $ feeddata = $ self - > fetch_entries ( ) ;
foreach my $ entry ( $ feeddata - > items ) {
my % ne ;
$ ne { feed_id } = $ self - > { data } - > { ID } ;
$ ne { entry_link } = $ entry - > link ( ) ;
2017-04-23 18:51:48 +00:00
RSSTootalizer::Entry - > create ( % ne ) ;
2017-04-22 19:28:21 +00:00
}
}
2017-04-21 19:45:58 +00:00
# Object methods
2017-04-22 18:49:49 +00:00
sub filters {
my $ self = shift ;
2017-04-23 18:51:48 +00:00
my $ filters = RSSTootalizer::DB - > doSELECT ( "SELECT * FROM filters WHERE feed_id = ? ORDER BY ID ASC" , $ self - > { data } - > { ID } ) ;
2017-04-22 18:49:49 +00:00
my @ retVal ;
foreach my $ r ( @$ filters ) {
2017-04-23 18:51:48 +00:00
push @ retVal , RSSTootalizer::Filter - > new ( $ r ) ;
2017-04-22 18:49:49 +00:00
}
return @ retVal ;
}
2017-04-22 19:28:21 +00:00
sub fetch_entries {
my $ self = shift ;
$ XML:: Feed:: MULTIPLE_ENCLOSURES = 1 ;
return XML::Feed - > parse ( URI - > new ( $ self - > { data } - > { url } ) ) ;
}
2017-04-23 18:49:07 +00:00
sub entry_by {
my $ self = shift ;
my $ key = shift ;
my $ value = shift ;
2017-04-23 18:51:48 +00:00
my $ sth = RSSTootalizer::DB - > doSELECT ( "SELECT * FROM entries WHERE feed_id = ? AND $key = ?" , $ self - > { data } - > { ID } , $ value ) ;
2017-04-23 18:49:07 +00:00
my @ retVal ;
foreach my $ r ( @$ sth ) {
2017-04-23 18:51:48 +00:00
push @ retVal , RSSTootalizer::Entry - > new ( $ r ) ;
2017-04-23 18:49:07 +00:00
}
return @ retVal ;
}
sub user {
my $ self = shift ;
2017-04-23 18:51:48 +00:00
my $ sth = RSSTootalizer::DB - > doSELECT ( "SELECT * FROM users WHERE username = ? and instance = ?" , $ self - > { data } - > { username } , $ self - > { data } - > { instance } ) ;
2017-04-23 18:49:07 +00:00
$ sth = $$ sth [ 0 ] ;
2017-04-23 18:51:48 +00:00
return RSSTootalizer::User - > new ( $ sth ) ;
2017-04-23 18:49:07 +00:00
}
2017-04-22 19:28:21 +00:00
2017-04-21 19:45:58 +00:00
1 ;