<?php
/*
Function: Extracts "latest news headlines" from DAWN.COM
Usage: Simply upload this file as anything.php and call in browser. (Modify the unique start and end tags if they source site changes the template)
Warning: This code is meant for educational purposes only. I, Ihtesham Ali is not responsible of any damages that may occur, illegal use and/or theft of data using this script.
I advise not to fetch data without prior consent of the source website owner(s).
*/
//Get current date according to GMT
$date = explode(" ", gmdate("Y m d"));
//Create news URL
$url = "http://www.dawn.com/".$date[0]."/".$date[1]."/".$date[2]."/nat.htm";
//Open URL (I used @ to hide failure warning)
$handle = @fopen($url, "rb");
//If the URL is not opened successfully (possibly because the news page is not updated yet)
if (!$handle){
//change TODAY to YESTERDAY ($date[2] holds the value of current day)
$prevday = $date[2] - 1;
//Re-create the URL (based on yesterday)
$url = "http://www.dawn.com/".$date[0]."/".$date[1]."/".$prevday."/nat.htm";
//Open new URL
$handle = @fopen($url, "rb");
}
//If the URL is opened successfully
if($handle){
//initialize the raw data container
$contents = "";
//Get data together in chunks
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
//We've got what we needed, now close the news URL
fclose($handle);
$stag = "<TD VALIGN=TOP WIDTH=\"49%\">"; //unique start tag
$etag = "<br><a"; //unique end tag
//Create array of $contents by splitting up the raw data according to our unique start tag
$contents = explode($stag, $contents);
//Count total news
$total = count($contents);
//Start printing the unordered list of news headlines
echo "<ul>";
//Please note that $i = 1 because $contents[0] has no news headline
for($i = 1; $i <= $total-1; $i++){
//Calculate the number of chars we need to obtain after the unique start tag
$uptochar = strpos($contents$i], $etag);
//Get the news headline
$link = substr($contents$i], 0, $uptochar);
//Print the news headline
echo "<li>$link";
//ok close the loop
}
//Close the unordered list
echo "</ul>";
}else{
//This is explained in the error message
echo "Error: Latest news page is older than yesterday. If you want to display very old data then: change the - 1 in $prevday to - 4 or something even more.";
}
//cool hun? ;)
?>
Found a little silly bug.
The relative links were being created.
Bug Fixed!
Following is the updated code.
<?php
/*
Function: Extracts "latest news headlines" from DAWN.COM
Usage: Simply upload this file as anything.php and call in browser. (Modify the unique start and end tags if they source site changes the template)
Warning: This code is meant for educational purposes only. I, Ihtesham Ali is not responsible of any damages that may occur, illegal use and/or theft of data using this script.
I advise not to fetch data without prior consent of the source website owner(s).
*/
//Get current date according to GMT
$date = explode(" ", gmdate("Y m d"));
//Create news URL
$url = "http://www.dawn.com/".$date[0]."/".$date[1]."/".$date[2]."/nat.htm";
//Open URL (I used @ to hide failure warning)
$handle = @fopen($url, "rb");
//If the URL is not opened successfully (possibly because the news page is not updated yet)
if (!$handle){
//change TODAY to YESTERDAY ($date[2] holds the value of current day)
$prevday = $date[2] - 1;
//Re-create the URL (based on yesterday)
$url = "http://www.dawn.com/".$date[0]."/".$date[1]."/".$prevday."/nat.htm";
//Open new URL
$handle = @fopen($url, "rb");
}
//If the URL is opened successfully
if($handle){
//initialize the raw data container
$contents = "";
//Get data together in chunks
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
//We've got what we needed, now close the news URL
fclose($handle);
$stag = "<TD VALIGN=TOP WIDTH=\"49%\">"; //unique start tag
$etag = "<br><a"; //unique end tag
//Create array of $contents by splitting up the raw data according to our unique start tag
$contents = explode($stag, $contents);
//Count total news
$total = count($contents);
//Start printing the unordered list of news headlines
echo "<ul>";
//Please note that $i = 1 because $contents[0] has no news headline
for($i = 1; $i <= $total-1; $i++){
//Calculate the number of chars we need to obtain after the unique start tag
$uptochar = strpos($contents$i], $etag);
//Get the news headline
$link = substr($contents$i], 0, $uptochar);
//Choose specific part of the URL
$alink = substr($url, 0, 31);
//Create custom hyperlink
$link = str_replace("<a href=\"", "<a target=\"_blank\" href=\"".$alink, $link);
//Print the news headline
echo "<li>$link</li>";
//ok close the loop
}
//Close the unordered list
echo "</ul>";
}else{
//This is explained in the error message
echo "Error: Latest news page is older than yesterday. If you want to display very old data then: change the - 1 in $prevday to - 4 or something even more.";
}
//cool hun? ;)
?>
This is actually a simple algorithm, which you can translate in almost any programming language. I have used PHP.
You are simply reading a file and extracting your desired part from it. There is nothing in this code but common sense.
btw.. we all know rss is not a programming language its a way to represent information.. :) so nothing wrong with using rss feeds since they make it really easy to pass on information...
However, there are functions for accurate time in previous days like yesterday, last week, last month etc. Read the manual for enahncement of this script.
[quote]
You are simply reading a file and extracting your desired part from it. There is nothing in this code but common sense.
[/quote]
OK fine ... what exactly you wanted to say ???
I have been looking at your comments on GS from last few days .... don mind but they are kinda proudy ... you should not act like that... there are lots of better engineers on this community but they don act like this... learn something bro !!
Kind of proudy?
Well, they sure are.
I dont just talk, I’ve proved hence, have a reason to be proud of. What’s wrong with you by the way if my work makes me feel good?
You were also given the equal time to show your intellect.
So now you know, why did I say that - You are simply reading a file and extracting your desired part from it.* There is nothing in this code but common sense.*
Like you?
Why dont they showup on right time? gahhhahahaha
When did I say that I know everything? I frankly admit that I stand there where I am learning the ABC of my field.
One of my major interests is eternal pursuit of knowledge. I will definitely keep learning bro!
I suggest you not to think so low if you want to stand somewhere. Focus on yourself rather than picking on every teeny tiny irritating people like me.
Ansoon , Ihtesham & others. Thank you very much for the hard work. I will look into implementing the code and let you guys know where else I might need some tweaking help.
I had been working as a freelancer for several years then due to workload I established a small software house in Rawalpindi, last year. Many thanks to Allah the almighty for today we have a stable clientele. Our prominent clients are PTCL, Pakistan Customs, JICA, SAARC and Islamabad Chamber of Commerce and Industry.
I was planning to setup a branch in Karachi lately but recently I am offered to invest in similar business on partnership basis, in Dubai. So now I’m planning to avail this opportunity for expansion, first.
Well, Ihtesham.
Go and search in the archives of Comp. forum and you will find out. I have been too much busy with work at office and have not been visiting GS on regular basis thats why could not reply.
But whenever I helped anyone on this forum I never said like 'hahahaah main nain aik banday par us ki zindgi ka sab se bara ehsaan ker dia' ...... not this way sunny !
just search for your posts in this forum and realize them collectivily, you will find everything in it.
Your contributions are good and I do respect them, but what I wanted to say is k banday mein thori si Aaajzi aur Inksaari honi chahiye :) ......