milesj / utility

[Deprecated] A CakePHP plugin for common utility classes.
MIT License
69 stars 24 forks source link

Feed order #5

Closed tonylisanti closed 11 years ago

tonylisanti commented 11 years ago

I'm not able to get the sort order for the feeds to work. For some reason, maybe i'm doing something wrong, the feeds never order by date. It looks like it orders within the month correctly but the months and years are not.

I have this in my controller:

    $this->set('feed', $this->Aggregator->find('all', array(
            'conditions' => array(
                'cnn.com' => 'http://rss.cnn.com/rss/cnn_topstories.rss',
                'cnn.com' => 'http://rss.cnn.com/rss/cnn_tech.rss'
            )
    )));

And this is the result I'm getting:

array( 'Fri, 03 Aug 2012 11:59:24 EDT' => array( 'title' => 'An open letter to texting-mad teenagers', 'guid' => 'http://www.cnn.com/2012/08/01/tech/netiquette-open-letter-teenagers/index.html', 'date' => 'Fri, 03 Aug 2012 11:59:24 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/mFKZeL3oHyM/index.html', 'description' => 'Put your phone down for a second, teen texters -- there are some things you've got to hear.

', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 05:28:45 EST' => array( 'title' => 'Worst fast-food meals for sodium', 'guid' => 'http://www.cnn.com/2013/01/04/health/gallery/fast-food-worst-sodium-meals/index.html', 'date' => 'Fri, 04 Jan 2013 05:28:45 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/x1PcJu3Dom8/index.html', 'description' => 'Worst fast-food meals for sodium
', 'source' => 'cnn.com Tech' ), 'Fri, 04 Jan 2013 09:31:30 EST' => array( 'title' => 'Six gadget trends to look for at CES', 'guid' => 'http://www.cnn.com/2013/01/04/tech/innovation/ces-preview-trends/index.html', 'date' => 'Fri, 04 Jan 2013 09:31:30 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/n5pXMRw6PXY/index.html', 'description' => 'Smartphones inch closer to becoming remote controls for your life at next week's 2013 Consumer Electronics Show in Las Vegas. The annual event is the largest gadget conference in the world, where major technology companies and scrappy startups can show off their latest innovations.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 10:20:34 EST' => array( 'title' => 'Make Create Innovate', 'guid' => 'http://www.cnn.com/2012/12/21/tech/innovation/raspberry-pi-computer-upton/index.html', 'date' => 'Fri, 04 Jan 2013 10:20:34 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/2HMzKe6-YzI/index.html', 'description' => 'In a world where computers are increasingly powerful and are concealed within ever more glossy slabs of aluminum, the Raspberry Pi (RPi) offers surprising proof for the virtue of moderation.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 10:25:25 EST' => array( 'title' => 'Website tracks big beasts', 'guid' => 'http://www.cnn.com/2012/12/31/tech/web/kruger-latest-sightings-nadav-ossendryver/index.html', 'date' => 'Fri, 04 Jan 2013 10:25:25 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/f6VWAKy1iFE/index.html', 'description' => 'A website is letting users post sightings of wildlife in Kruger National Park, so that tourists on safari have more chance of spotting elusive animals.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 11:10:04 EST' => array( 'title' => 'Apparently This Matters: Ruzzle', 'guid' => 'http://www.cnn.com/2013/01/04/tech/social-media/apparently-this-matters-ruzzle/index.html', 'date' => 'Fri, 04 Jan 2013 11:10:04 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/cJ5l4WYWCXw/index.html', 'description' => 'On Sunday morning, a strange word suddenly started trending for me on Twitter. The word was Ruzzle. I had no idea what it meant, so I did the smart thing and asked my dog.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 11:16:21 EST' => array( 'title' => 'Apparently This Matters: Ruzzle', 'guid' => 'http://www.cnn.com/2013/01/04/tech/social-media/apparently-this-matters-ruzzle/index.html', 'date' => 'Fri, 04 Jan 2013 11:16:21 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/cJ5l4WYWCXw/index.html', 'description' => 'On Sunday morning, a strange word suddenly started trending for me on Twitter. The word was Ruzzle. I had no idea what it meant, so I did the smart thing and asked my dog.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 11:47:48 EST' => array( 'title' => 'The secret to a better brain', 'guid' => 'http://www.cnn.com/2013/01/04/health/chopra-better-brain/index.html', 'date' => 'Fri, 04 Jan 2013 11:47:48 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/Js-KOonnz5w/index.html', 'description' => 'Chopra: Think your way to an inspired brain.
', 'source' => 'cnn.com Tech' ), 'Fri, 04 Jan 2013 11:52:32 EST' => array( 'title' => '2013's must-see TV', 'guid' => 'http://www.cnn.com/2013/01/04/showbiz/tv/2013-tv-preview/index.html', 'date' => 'Fri, 04 Jan 2013 11:52:32 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/hcKNy9qeloo/index.html', 'description' => 'Your favorite shows are almost back.
', 'source' => 'cnn.com Tech' ), 'Fri, 04 Jan 2013 15:00:52 EST' => array( 'title' => 'Jim McKelvey : 21st century Renaissance man', 'guid' => 'http://www.cnn.com/video/#/video/tech/2013/01/04/tnl-mckelvey-weekend.cnn', 'date' => 'Fri, 04 Jan 2013 15:00:52 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/cB8dw2jvfVk/', 'description' => 'Jim McKelvey is an engineer, artist, community activist, and co-Founder of Square. See a profile of him on Sunday at 2 p.m. ET on CNN.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 15:02:22 EST' => array( 'title' => 'Have you heard of Snapchat?', 'guid' => 'http://www.cnn.com/2013/01/03/tech/mobile/snapchat/index.html', 'date' => 'Fri, 04 Jan 2013 15:02:22 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/OwaVcq1GpDc/index.html', 'description' => 'You may not have heard of Snapchat. But if there are teenagers or 20-somethings in your life, it's a safe bet that they have.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 15:55:32 EST' => array( 'title' => 'Shark finning thrives on Hong Kong's roofs', 'guid' => 'http://www.cnn.com/2013/01/04/asia/gallery/hong-kong-roof-shark-fins/index.html', 'date' => 'Fri, 04 Jan 2013 15:55:32 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/0TuIW0k4giM/index.html', 'description' => 'Shark finning thrives on Hong Kong's rooftops
', 'source' => 'cnn.com Tech' ), 'Fri, 04 Jan 2013 17:40:28 EST' => array( 'title' => 'Time Has Come' for '60s singer', 'guid' => 'http://www.cnn.com/2012/12/24/tech/web/lester-chambers-kickstarter/index.html', 'date' => 'Fri, 04 Jan 2013 17:40:28 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_tech/~3/OrtY7BsWRSc/index.html', 'description' => 'His song, "Time Has Come Today" is one of the enduring anthems of the turbulent 1960s. But in 2012, Lester Chambers had little to show for it when he turned to the Web for help.
', 'source' => 'cnn.com Top Stories' ), 'Fri, 04 Jan 2013 20:25:40 EST' => array( 'title' => '10 European hot spots', 'guid' => 'http://www.cnn.com/2013/01/04/travel/europe-top-destinations/index.html', 'date' => 'Fri, 04 Jan 2013 20:25:40 EST', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/DT451tcmFPc/index.html', 'description' => 'Put these destinations on your travel radar
', 'source' => 'cnn.com Tech' ), 'Fri, 20 Apr 2012 09:38:33 EDT' => array( 'title' => 'Vanessa Williams' perfect man', 'guid' => 'http://www.cnn.com/video/#/video/showbiz/2012/04/20/piers-williams-love.cnn', 'date' => 'Fri, 20 Apr 2012 09:38:33 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/XdISDjJ8ssM/', 'description' => 'Entertainer Vanessa Williams talks to CNN's Piers Morgan about "being properly in love" and relationships.
', 'source' => 'cnn.com Tech' ), 'Fri, 20 Apr 2012 09:38:44 EDT' => array( 'title' => 'Political dog tale duel', 'guid' => 'http://www.cnn.com/video/#/video/bestoftv/2012/04/20/ac-ridiculist-obama-romney-dogs.cnn', 'date' => 'Fri, 20 Apr 2012 09:38:44 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/f2pQv1EmVbY/', 'description' => 'In the RidicuList, Anderson Cooper examines which candidate's dog tale will have a greater election impact.
', 'source' => 'cnn.com Tech' ), 'Fri, 20 Apr 2012 09:41:33 EDT' => array( 'title' => ''Only in America': Defending Axl Rose', 'guid' => 'http://www.cnn.com/video/#/video/bestoftv/2012/04/20/piers-morgan-only-in-america-in-defense-of-axl-rose.cnn', 'date' => 'Fri, 20 Apr 2012 09:41:33 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/CE0-oIRG7AY/', 'description' => 'Piers Morgan defends Axl Rose for deciding not to attend the Rock'N'Roll Hall of Fame induction of Guns N' Roses.
', 'source' => 'cnn.com Tech' ), 'Fri, 20 Apr 2012 11:29:57 EDT' => array( 'title' => 'Man makes dating spreadsheet', 'guid' => 'http://www.cnn.com/video/#/video/bestoftv/2012/04/20/exp-get-real-dating-spreadsheet.cnn', 'date' => 'Fri, 20 Apr 2012 11:29:57 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/vx0ARutyCFQ/', 'description' => 'Soledad O'Brien discusses the investment banker who created an elaborate spreadsheet to keep track of his dates.
', 'source' => 'cnn.com Tech' ), 'Fri, 20 Apr 2012 16:43:37 EDT' => array( 'title' => 'Working moms of CNN', 'guid' => 'http://www.cnn.com/video/#/video/bestoftv/2012/04/19/cnn-working-moms-share-their-stories.cnn', 'date' => 'Fri, 20 Apr 2012 16:43:37 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/P9wFf4tbbyw/', 'description' => 'CNN goes behind the scenes with the working moms on Suzanne Malveaux's team to see how they juggle it all.
', 'source' => 'cnn.com Tech' ), 'Fri, 20 Apr 2012 16:43:45 EDT' => array( 'title' => 'Levon Helm remembered', 'guid' => 'http://www.cnn.com/video/#/video/bestoftv/2012/04/20/ac-levon-helm-obit.cnn', 'date' => 'Fri, 20 Apr 2012 16:43:45 EDT', 'link' => 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/JYYIEYXqn34/', 'description' => 'Anderson Cooper takes a look back at the life of The Band's Levon Helm and his many contributions to music.
', 'source' => 'cnn.com Tech' ) )

Any help would be greatly appreciated.

Thanks

tonylisanti commented 11 years ago

Looks like the date within the feed(s) needs to be a timestamp in order for the sort to work correctly but I'm not 100% sure.

milesj commented 11 years ago

Weird, it should be cast to a timestamp: https://github.com/milesj/cake-utility/blob/master/Model/Datasource/FeedSource.php#L313

I will look into this.

tonylisanti commented 11 years ago

Actually, I think there may be an issue with the sorting on lines 162-166 in FeedSource.php that causing the issue.

I'm a novice but I changed it to the below and it seems to work. Not sure if this is the issue though:

            uksort($results, array($this, 'cmp'));
            if ($query['feed']['order'] == 'DESC') {
                $results = array_reverse($results);
            }
            /*
            if ($query['feed']['order'] == 'ASC') {
                krsort($results);
            } else {
                ksort($results);
            }
            */
            if ($cache) {
                Cache::set(array('duration' => $query['feed']['expires']));
                Cache::write($cache, $results, 'feeds');
            }
        }

        return $this->_truncate($results, $query['limit']);
    }

    return array();
}

function cmp($a, $b) {
    $a = strtotime($a);
    $b = strtotime($b);
  if ($a == $b) {
    return 0;
  }

  return ($a < $b) ? -1 : 1;
}
milesj commented 11 years ago

Pushed some changes in RC3.

milesj commented 11 years ago

Tested just now with no problems. Release 1.3.0.