Calling all Coding Gurus

Re: Calling all Coding Gurus

 <?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? ;)
?>


Re: Calling all Coding Gurus

The PHP tags on this forum suck.

Copy the exact code from http://pkdn.net/news.txt

I will delete this file after 3 days.

Re: Calling all Coding Gurus

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? ;)
?>

<A href=“http://www.paklinks.com/”“,” target=‘"_blank"’ p $link);< ?
<A href=“
http://www.paklinks.com/” target=‘"_blank"’ p $link);< ?<a ?,?>
Copy the exact code from http://pkdn.net/news.txt

Happy Coding!

Re: Calling all Coding Gurus

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.

I’ve just uploaded the working version too.

http://pkdn.net/news.php

Re: Calling all Coding Gurus

Good job Ihtesham, works like a charm. And with ur comments its easy to follow too.
Once it is explained ( :) ) it does seem simple.

Re: Calling all Coding Gurus

nice work Ihtesham

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...

Re: Calling all Coding Gurus

Thanks!

Re: Calling all Coding Gurus

//change TODAY to YESTERDAY ($date[2] holds the value of current day)
$prevday = $date[2] - 1;

what if the Date is 2005 , 03 , 01
then $prevday = 00 and no such /2005/03/00/nat.htm exits!!

//Re-create the URL (based on yesterday)
$url = “http://www.dawn.com/“.$date[0].”/“.$date[1].”/“.$prevday.”/nat.htm”;

Re: Calling all Coding Gurus

That’s why I added an Else clause in the code.

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. :slight_smile:

Re: Calling all Coding Gurus

[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 !!

Re: Calling all Coding Gurus

Exactly what I said.

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.*
:wink:

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.

cheers :slight_smile:

Re: Calling all Coding Gurus

Ihtesham, which year are you in software engineering? graduated yet ?

Re: Calling all Coding Gurus

I graduated from Moscow State Technological University in 2003 with 3.83 GPa (final year).

Re: Calling all Coding Gurus

Nice MashaAllah.. that’s great.. so what do you do for living now? where you working?

Re: Calling all Coding Gurus

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.

Great stuff.

Re: Calling all Coding Gurus

Your welcome Azkar bhai. :slight_smile:

Re: Calling all Coding Gurus

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.

I need your prays very much.

Re: Calling all Coding Gurus

You are most welcome.

And Azkar I’m really sorry for one of my posts in this thread where I wrote something when I was angry on someone. It’s over.

Do let me know when I can be of any use for this website.

Re: Calling all Coding Gurus

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 :) ......

Re: Calling all Coding Gurus

burnol lagao. aur apne sunehri asool apne pass rakho.