twister-phpbot/habr_twister.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2014-02-28 10:39:51 +04:00
<?php
mb_internal_encoding('UTF-8');
2014-02-28 19:05:18 +04:00
setlocale(LC_CTYPE, 'en_US.UTF-8');
2014-02-28 10:39:51 +04:00
// Load RSS feed
2014-07-23 10:50:26 +04:00
require_once 'rssreader.php';
2014-02-28 10:39:51 +04:00
$feed_uri = 'http://habrahabr.ru/rss/hubs/new/';
2014-07-23 10:50:26 +04:00
$rss = getRssFeed($feed_uri);
if (!$rss) die('Cannot read rss or it is up to date');
2014-02-28 10:39:51 +04:00
// Initialise TwisterPost
require_once 'twisterpost.php';
2014-07-23 10:52:04 +04:00
$twister = new TwisterPost('habr_ru');
2014-02-28 10:39:51 +04:00
// Initialise RSS database
2014-07-23 10:49:39 +04:00
require_once 'rssdb.php';
$db = new RSSDb('habr_ru.dat');
2014-02-28 10:39:51 +04:00
foreach ($rss->channel->item as $item) {
$link = (string)$item->link;
$title = (string)$item->title;
2014-03-01 00:47:12 +04:00
// Note: habrahabr.ru does both special chars encoding and CDATA wrap
$title = htmlspecialchars_decode($title);
2014-02-28 10:39:51 +04:00
// get post id from link
$id = (int)preg_replace('#[^\d]#', '', $link);
if ($db->isPublished($id)) {
continue;
}
2014-03-01 00:47:00 +04:00
// shorten URL [-6 chars do matter]
2014-02-28 14:07:33 +04:00
$link = str_replace('habrahabr.ru', 'habr.ru', $link);
$link = rtrim($link, '/');
2014-03-01 00:47:00 +04:00
2014-02-28 10:39:51 +04:00
$msg = $twister->prettyPrint($title, $link, isset($item->category) ? $item->category : null);
if ($twister->postMessage($msg)) {
$db->setPublished($id);
}
}