1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Quản trị mạng >

Hack 31. Publish Your Yahoo! Bookmarks

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (15.01 MB, 888 trang )


Torunthishack,you'llneedacoupleofnonstandardPerl

modulesinstalled:WWW::Yahoo::LoginandWWW::Mechanizeforlogging

intoYahoo!andfetchingthebookmarks.htmlexport,

respectively.



2.10.1.TheCode

ThisscriptlogsintoYahoo!andfetchesyourexported

bookmarks.Fromthere,itgoesthroughthebookmarkslineby

line,addingfoldernamesandlinkstoafilenameyouspecifyin

$file.Besuretoreplacethe$userand$passvalueswithyour

Yahoo!usernameandpasswordsothatthescriptcanloginon

yourbehalf.CreateafilecalledgetBookmarks.plandaddthe

followingcode:











#!/usr/bin/perl

#getBookmarks.pl

#AscripttodownloadYahoo!Bookmarksandformatthem

#Usage:getBookmarks.pl











useWWW::Yahoo::Loginqw(loginlogout);

useWWW::Mechanize;

usestrict;

















#Setsomeuservariables

my$user="insertYahoo!ID";

my$pass="insertYahoo!Password";

my$file="links.html";

my$bookmarkurl="http://bookmarks.yahoo.com/config/";

$bookmarkurl.="export_bookmark?.commit=1";







my$mech=WWW::Mechanize->new();











#LogintoYahoo!

my$resp=login(



mech=>$mech,



















);



uri=>$bookmarkurl,

user=>$user,

pass=>$pass,















#Ifloginsucceeded,loopthroughtheHTML

if($resp){



print"Loginok!\n";



my$bookmarks=$mech->content;



my@bookmarks=split(/\n/,$bookmarks);















#Opentheoutputfile

openFILE,">$file"ordie"Can'topen$file:















#Loopthroughthebookmarks,printingtofile

foreachmy$line(@bookmarks){





















































































#Ifthelineisfolder,printit



if($line=~m!].*>(.*?)!gi





printFILE"




printFILE$1;





printFILE"

\n";





}









#Ifthelineisabookmark,pr





if($line=~m!




printFILE"




printFILE"$2




printFILE"
\n";





}



}



#Closetheoutputfile



closeFILE;

}else{



warn$WWW::Yahoo::Login::ERROR;

}



2.10.2.RunningtheHack

Torunthecode,simplycallthescriptfromacommandprompt:





perlgetBookmarks.pl



Afterthescripthasrun,youshouldfindanewfilecalled

links.html,fullofyourYahoo!Bookmarks.Eachlinkshouldbe

onitsownline,surroundedbyHTML
tags.Thisletsyou

createyourownlookandfeelforthelinkswithanexternal

stylesheet.ThefoldershaveaCSSclassoffolder,andlinks

haveaCSSclassoflink.Thefileisallsetforyoutodropitinto

anexistingwebsiteasaserver-sideinclude,orwithabitmore

formattingitcouldbeitsownpage.

Nowthatthisprocessisautomatedthroughascript,youcan

runitonaregularscheduletoreapitsbenefits.Anychanges

youmakeatYahoo!Bookmarkswillbereflectedonyourremote

sitethenexttimethisscriptruns;onceevery24hoursshould

doit.InWindows,youcansetittorunasascheduledtask

fromtheControlPanel,callingPerlfromtheRunline,likethis:





C:\Perl\bin\perl.exe"C:\insertyourlocation\



AndfromLinux-basedservers,youcansetitasacronjob.

Nowthatyou'reabletouseYahoo!asapubliclinksmanager,

theprocessofsharingresourcesshouldbemuchsimpler.



Hack32.TracktheMedia'sAttentionSpanover

Time



Visualizemediatrendsbycountingthetotalnumberof

Yahoo!Newsmentionsofaspecificphraseoveraseries

ofdates.

Thenatureofnewsisthatitreportsaboutwhat'snewinthe

worldeachday.Butintherushtobringthelatestnewstothe

public,newsorganizationsoftenhaveapackmentality.The

newsbeingcoveredbythetopmediaoutletsthisweekis

differentfromwhatwascoveredlastweek.Andsometimes,

lookingatwhatnewsorganizationsdecidetocovercanbemore

interestingthanthenewsitself.

Inthatspirit,thishackisabouttrackingatopic'sebbsand

flowsthroughthenewscycle.BecauseYahoo!Newsbrings

togetherover7,000differentnewssourcesfromaroundthe

worldintoonesite,it'stheperfectplacetospottrendsand

trackwhatthemediaistracking.Onedrawbacktotracking

7,000newssourcesisthatstorageofthatinformationbecomes

anissue.SoYahoo!Newsstoresonlythelast30days'worthof

articles.Butinour24-hour-a-daynewsworld,30daysagocan

seemlikeancienthistory.



Thishackwasinspiredby"TrackingResultCountsoverTime"[Hack

#63]fromthefirsteditionofGoogleHacks.Ifyou'dliketoseehowto

implementasimilarhackforGoogleSearchresults,trackdownacopy

ofthefirsteditionofGoogleHacks.



Thekeytobeingabletotrackakeywordinnewsarticlesover

timeisbeingabletoisolatearticlesbyday.Luckily,theYahoo!

Newsadvancedsearchinterface

(http://news.search.yahoo.com/news/advanced)givesthe

optiontolimitsearchesbytime.So,ifyouwantjustthestories

aboutApplefromMarch1,2005,theadvancedsearchinterface

letsyoubringthemupbyspecifyingMarch1asthestartand

enddate.Anothergreatfeatureoftheadvancedsearch

interfaceistheabilitytolimityoursearchtoaspecificcategory.

ByselectingTechnologyinadditiontospecifyingthedate,you

canbesuretoweedoutstoriesaboutthefruitthatgrowson

treesandstickwithstoriesaboutthecompanythatmakes

computers.

Onceyouisolatestoriestoaparticularday,youcanfindout

howmanystoriescontainedthetermyou'reinterestedinon

thatday.Forexample,therewere143technologystoriesthat

mentionedAppleonMarch1,2005,butonly115stories

mentionedAppleonMarch2.Atthetimeofthiswriting,the

newsdataavailableatYahoo!SearchWebServicesdoesn't

includetheabilitytolimitrequeststoaspecificdate,sothis

hackusesscreenscrapingtogatherthedata.

Screenscrapinginvolvesprogrammaticallydownloadingthe

HTMLforawebpageandpickingthroughthesourcetofindthe

bitsofinformationyou'relookingfor.Screenscrapingisa

notoriouslybrittleprocess,becauseitreliesonfindingpatterns

withintheHTML.IfYahoo!decidestochangeitsHTML

tomorrow,thecodeinthishackthatpicksupthetotalresults

foraquerywillfail.Evenknowingthis,we'reinterestedinonly

onebitofdataonYahoo!Newssearchresultspages:the

estimatedtotalnumberofarticlesforourquery.Figure2-23

showsthebitwe'relookingforinasearchforApplestories

fromMarch1,2005.

Searchingthroughthealmost250linesofHTMLinaresults

page,youcanpickoutthetotalresultsnumberfromthisline:



Xem Thêm
Tải bản đầy đủ (.pdf) (888 trang)

×