White Wolf

برمجة عميل rss الطريقة السهلة

Posted in لينكس, برمجة و بيرل by regex2008 on يونيو 26, 2011

بسم الله الرحمن الرحيم

سنحاول في هذا الموضوع ان ننشيء قاريء RSS باقصر طريق ممكن و ذلك باستخدام مكتبة XML::RSS::Parser و هي تتمتع بميزة جيدة الا و هي اخفاء مسألة التعامل مع اصدار الـ RSS … في هذا الموضوع لن نتكلم عن XML او RSS و كيف تعمل كل ما يهمنا فقط هو اننا وجدنا موقع يوفر RSS و نحب ان نستخدم هذه الميزة لنقل مثلا في موقع او برنامج او اي شيء …

الخطوة الاولى :
ننفذ الامر ادناه من سطر الاوامر لكي يتم تنصيب المكتبة المطلوبة :

cpan XML::RSS::Parser

الخطوة الثانية :

use strict;
use FileHandle;
use LWP::Simple;
use XML::RSS::Parser;
use feature ':5.10';

بعض المكتبات المساعدة الموجودة في كل نسخة من بيرل ، اذا فقط نحتاج الى استدعائها …

الخطوة الثالثة :
نحاول ان نجلب ملف الـ RSS من خلال الرابط الذي يوفره الموقع :

my $file = getstore("http://rss.slashdot.org/Slashdot/slashdotLinux", "file.rss"); 

الان هنا يمكن ان تفشل عملية التحميل سواء من طرفنا او من طرف السيرفر المطلوب اذا يجب ان نتعامل مع هذا فنعمل جملة شرطية ، اذا نجح التنزيل نواصل و اذا فشل نعطي رسالة خطأ و ننهي البرنامج … اذا سأضع كود الفشل اولا فقط لانه اقصر :

else
{
say "error downloading file: $file";
}

الخطوة الرابعة :
لنكتب الجملة الشرطية في حال نجحنا في تنزيل الملف و من ثم ننشيء كائن جديد من المكتبة XML::RSS::Parser

if ( is_success($file) )
{
my $parser = XML::RSS::Parser->new;

الان نقرا الملف و نعمل له Parse :

my $fh = FileHandle->new('file.rss');
my $feed = $parser->parse_file($fh);

انتهينا ! الان فقط بقيت مسألة كيفية استرجاع البيانات …

الخطوة الخامسة :
كما قلنا الان لدينا كائن جاهز فقط المسألة مسألة تنفيذ ميثودز و لكن من هذه النقطة ستختلف الاوامر بحسب المخرج المطلوب مثلا هل سنطبع البيانات الى برنامج ويب ؟ او برنامج سطح مكتب ؟ لنقل اننا سنطبعه على سطر الاوامر لتبسيط المسألة …
اذا لنطبع عنوان الفيد و عدد المواضيع التي يحتويها :

my $feed_title = $feed->query('/channel/title');
print $feed_title->text_content;
my $count = $feed->item_count;
say " ($count)";

الان بقيت دائرة التكرار لنطبع كل عنوان خبر و رابطه :

foreach my $item ( $feed->query('//item') ) {
my $title = $item->query('title');
say '  '.$title->text_content;
my $link = $item->query('link');
say $link->text_content;
}
}#end if

و الحمد لله رب العالمين

 

مثال على المخرج :

Slashdot: Linux (15)
KDE 4.7 RC Is Here: GRUB2 Integration, KWin Mobile

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/MVMplrutXl0/KDE-47-RC-Is-Here-GRUB2-Integration-KWin-Mobile

Linux 3.0 Will Be Faster Than 2.6.39

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/JEJQF2e-GLE/Linux-30-Will-Be-Faster-Than-2639

Who Killed the Netbook?

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/s34Se91fC9k/Who-Killed-the-Netbook

Synaptic Dropped From Ubuntu 11.10

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/lu-nXIQFaM0/Synaptic-Dropped-From-Ubuntu-1110

Thinking of Publishing Your Own $0.99 Kindle Book?

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/j-ISop_DJlI/Thinking-of-Publishing-Your-Own-099-Kindle-Book

Nokia Introduces MeeGo-Powered N9 Phone

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/HYBu0MCHc7s/Nokia-Introduces-MeeGo-Powered-N9-Phone

13-Year-Old Password Security Bug Fixed

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/aIhIK7e5V1U/13-Year-Old-Password-Security-Bug-Fixed

The Ugly State of ARM Support On Linux

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/BQUOHkYrP9Q/The-Ugly-State-of-ARM-Support-On-Linux

Court Case To Test GNU GPL

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/Hv57ikIYJy4/Court-Case-To-Test-GNU-GPL

NanoNote Goes Wireless

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/MgQ491bKM2o/NanoNote-Goes-Wireless

Ask Slashdot: Tools For Linux Disk Encryption and Integrity?

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/-l78O5jVtgk/Ask-Slashdot-Tools-For-Linux-Disk-Encryption-and-Integrity

Linus’ Other Gift to the World

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/vZgWnU7ZEr8/Linus-Other-Gift-to-the-World

Shuttleworth: Chrome Nearly Replaced FF In Ubuntu

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/bALGeWGI6HA/Shuttleworth-Chrome-Nearly-Replaced-FF-In-Ubuntu

Tom’s Hardware Dissects Ubuntu 11.4′s Interface and Performance

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/gb_-MAiRMwE/Toms-Hardware-Dissects-Ubuntu-114s-Interface-and-Performance

Ask Slashdot: Linux Support In Universities?

http://rss.slashdot.org/~r/Slashdot/slashdotLinux/~3/BUSB89tnXLQ/Ask-Slashdot-Linux-Support-In-Universities

اترك رد

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / تغيير )

Twitter picture

You are commenting using your Twitter account. Log Out / تغيير )

Facebook photo

You are commenting using your Facebook account. Log Out / تغيير )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 25 other followers