Tải bản đầy đủ - 0 (trang)
Hack 63. Add an API to Your Yahoo! 360 Blog

Hack 63. Add an API to Your Yahoo! 360 Blog

Tải bản đầy đủ - 0trang

3.14.1.WhatYouNeed

Toimplementthishack,you'llneedaccesstoapublicly

availablewebserverthatcanrunPerlscripts,andseveral

externalmodules.Here'salookatthemodulesyouneed,what

theyprovide,andwhereyoucanfindthem:



XMLRPC::Transport::HTTP

ThismodulehandlesalloftheformattingofXML-RPC

requestsandresponsesrequiredforthemetaWeblogAPI

interface.Themoduleispartofalargerpackagecalled

SOAP::Lite.Toreadthedocumentation,goto

http://search.cpan.org/~byrne/SOAP-Lite0.60a/lib/XMLRPC/Transport/HTTP.pm.



LWP::Simple

Thismoduleallowsyoutodownloadthecontentsofpages

fromtheWeb.Itisavailableat

http://search.cpan.org/~gaas/libwww-perl5.803/lib/LWP/Simple.pm.



WWW::Mechanize

AlsoknownasMech,thismodulecanautomateinteractions

withawebsite.ThishackusesMechtologintoYahoo!and

postitemstoaYahoo!360weblog.Todownloadthemodule

andreaditsdocumentation,goto

http://search.cpan.org/~petdance/WWW-Mechanize1.12/lib/WWW/Mechanize.pm.



WWW::Yahoo::Login

ThismoduleisanextensionofMechthathandlesloggingin

toYahoo!sites.Finditar

http://search.cpan.org/~struan/WWW-Yahoo-Login0.10/lib/WWW/Yahoo/Login.pm.



Image::Size

Thismodulecanfindthedimensionsinpixelsofanyimage.

It'susedinthishacktofindthedimensionofimagesposted

fromFlickr.Youcanreadmoreat

http://search.cpan.org/~rjray/Image-Size-2.992/Size.pm.

Ifyou'remissinganyofthesemodules,youcaninstalleachof

themwithCPAN,likethis:







perlMCPANeshell

cpan>installinsertpackagename



Withthemodulesinplace,you'rereadytomoveontothe

script.



3.14.2.TheCode

Thiscodedoesn'tfullyimplementallofthefunctionsyou'llfind

inthemetaWeblogAPI.Instead,itimplementstwomethods:

getUsersBlogsandnewPost.Thesetwomethodsarethebare

minimumneededtointerfacewithothersystemsandaddnew

weblogposts.



Asyou'dexpect,getUsersBlogsreturnsalistofweblogsthata

particularusercanposttoataweblogservice.BecauseYahoo!

360usersarelimitedtooneblogperYahoo!ID,thisfunction

simplylogsintoYahoo!andfetchesthenameoftheuser's

Yahoo!360blog.ThenewPostfunctionalsologsintoYahoo!,

changestheincomingtextofapostifnecessary,andaddsthe

textasanewposttotheYahoo!360weblog.

SavethefollowingcodetoafilecalledY360_api.pl:























#!/usr/bin/perl

#Y360_api.pl

#ImplementsaminimalistmetaWeblogAPIforYahoo!360

#YoucanreadmoreaboutthemetaWeblogAPIhere:

#http://www.xmlrpc.com/metaWeblogApi

#

#Usage:sendmetaWeblogAPIrequestsformethods:

#

#



getUsersBlogs

#





ReturnsthenameandURLofaY













#

#

#

#









usestrict;

useXMLRPC::Transport::HTTP;













XMLRPC::Transport::HTTP::CGI



->dispatch_to('metaWeblog')



->handle

;











packagemetaWeblog;

useWWW::Yahoo::Loginqw(loginlogout);

useWWW::Mechanize;















blogforthegivenuser.

newPost



Addsapostwiththeincomingt

aYahoo!360blogforthegivenuser.









useImage::Size'html_imgsize';

useLWP::Simple;







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









subgetUsersBlogs{



my($app,$msg,$user,$pass)=@_;



















#Setsomedefaults

my$blog_url="http://blog.360.yahoo.com/blog/

my$blog_name="MyYahoo!360Blog";









































































#LogintoYahoo!360

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

my$login=login(



mech=>$mech,



uri=>'http://blog.360.yahoo.com/blog



user=>$user,



pass=>$pass,

);

























#GetweblogURLandtitle

if($login){



my$html=$mech->response()->content(



if($html=~m!

(.*?)

!mgis){





$blog_name=$1;



}



if($html=~m!
  • View





    $blog_url=$1;



    }

    }















    #Sendtheresponse

    my@res;

    push@res,{url=>SOAP::Data->type(string=>



    blogid=>SOAP::Data->type(string=>"1



    blogName=>SOAP::Data->type(string=>$











    }



    \@res;











    subnewPost{



    shiftifUNIVERSAL::isa($_[0]=>__PACKAGE__);



    my($blog_id,$user,$pass,$item,$publish)=@







































    #LogintoYahoo!360

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

    my$login=login(



    mech=>$mech,



    uri=>'http://blog.360.yahoo.com/blog



    user=>$user,



    pass=>$pass,

    );































    #Addwidth/heighttoimagetags(forFlickrpo

    if($item->{description}=~m!(.*?


    my$file=get($2);



    my$size=html_imgsize(\$file);



    $item->{description}="$1$size$4";

    }



















    #Writepostdescriptiontoafile

    open(FILE,">>post.txt")ordie("Couldn'topen

    printFILE$item->{description}."\n\n";















    #Removeinitialdivtagfromdel.icio.usposts

    $item->{description}=~s!






    !$1!gis;















    #Striplinebreaks

    $item->{description}=~s!\n!!gis;











    #Ifloginsucceeded,addpostandsendasucce















































    }



    #responsewiththegeneric1000asthepostID

    if($login&&$item->{title}){



    my$form=$mech->form_name("blog_compo



    $mech->field("title",$item->{title});



    $mech->field("contents",$item->{descri



    $mech->click("post");



    SOAP::Data->type(string=>"1000");

    }else{



    returnerror$WWW::Yahoo::Login::ERROR;

    }



    ThisscriptisbuiltspecificallytoworkwiththeAPI-related

    servicesatFlickr[Hack#67]anddel.icio.usabookmark

    manageravailableathttp://del.icio.us.ThenewPostsubroutine

    reformatsincomingtextbasedonthetextthattheseservices

    provide.Forexample,Flickrdoesn'tprovideheightandwidth

    attributesforimagetagsintheHTMLitproduces,andYahoo!

    360requiresthoseattributesinweblogposts.Sothisscript

    usestheImage::Sizemoduletoaddthoseattributestothetext.



    3.14.3.RunningtheHack

    Torunthehack,uploadY360_api.pltoapubliclyavailableweb

    serverandnotetheURL.Itshouldlooksomethinglikethis:





    http://example.com/Y360_api.pl



    BeawarethatthisURLissometimesreferredtoasanAPI

    Endpoint,sodon'tletthejargonthrowyouoff.Whenevera

    serviceasksforaURL,thisistheoneyoushoulduse.



    ThefinalstepistotestoutyournewAPIatathird-party

    service.SincethescriptwasbuiltspecificallyforFlickr,that'sa

    goodplacetostart.FollowthestepstosetupaweblogatFlickr

    [Hack#98]andchooseMetaWeblogAPIEnabledBlogasyour

    weblogtype,enteryourscript'sURLastheEndpoint,and

    includeyourYahoo!IDandpassword.

    Flickrwillcontactyourscriptbehindthesceneswitha

    getUsersBlogsrequest,andyourscriptwillsendbackthetitleand

    URLofyourYahoo!360weblog.Onceyourweblogissetupin

    Flickr,youcanclicktheBlogThisbuttonaboveanyphotoand

    chooseyourYahoo!360weblog(seeFigure3-29).



    Figure3-29.TheBlogThisbuttonatFlickr



    FromthereyoucancomposeyourpostatFlickrandseethe

    finishedproductasapostonyourYahoo!360bloglikethat

    showninFigure3-30.



    Figure3-30.AFlickrimagepostedtoaYahoo!

    360blog



    Thecodetakesabitoftimetosetup,butonceyouwirethis

    shortcutforyourself,youmayfindthatyouusebothYahoo!

    360andFlickrmoreoften.







    Hack64.CreateaYahoo!Avatar



    Makeacustomcaricatureofyourselfandwatchitsmile

    orcryalongwithyouasyouchatthroughYahoo!Instant

    Messenger.

    Havinganinstantmessagingconversationonthecomputerisa

    bitlikehavingaconversationwithablindfoldon.Youcan

    "hear"whattheotherpersonissayingclearlybyreadingwhat

    hetypes,butothercues,suchasfacialexpressions,are

    completelyabsent.Withoutseeingandhearingtheperson

    you'rechattingwith,it'shardtopickuponemotionalovertones

    suchashumor,sadness,orsarcasm.Thisisexactlywhywe've

    developedtextshortcutstoconveyemotionsoverthe

    Internetforexample,usinganemoticontoindicatesmiling,or

    usinganabbreviationsuchasLOLtoindicatethatyou're

    laughingoutloud.

    Yahoo!hastakenthisideaofconveyingemotionastepbeyond

    suchtextshortcutsbycreatingcartoon-likecharacterscalled

    avatarstostandinforsomeoftheseemotionalcuesduring

    Yahoo!InstantMessengerconversations.Withalittlework,you

    cancreateyourownavatarthat'suniquetoyourpersonality

    andletitstandinforyouasyouchatwithpeopleonline.



    3.15.1.YourDigitalDouble

    TocreatetheInternetrepresentationofyou,browsetothe

    Yahoo!Avatarssite(http://avatars.yahoo.com).(You'llneedto

    beataWindowscomputerandhavetheFlashplug-ininstalled

    foryourbrowser.)Onceyou'rethere,loginandchoosethe

    genderofyouravatar(maleorfemale);allotheroptionswill



    flowfromthischoice.Youcanthengoonavirtualshopping

    spreetochangethelookofyouravatarinanumberofways:



    Appearance

    Matchyourskincolor,faceshape,eyecolor,orhairstyle.



    Apparel

    Dressyouravatarupindifferenttopsandbottoms.



    Extras

    Giveyouravatarbags,jewelry,scarves,sportsitems,pets,

    hats,orholiday-specificitems.



    Backgrounds

    Putyouravatarinaspecificlocation,suchasaschoolor

    vacationspot.



    Branded

    Giveyouravatarthelatestbrand-nameapparel.

    Asyougofromsectiontosectionchangingyouravatar,youcan

    keeptrackofyourchangesonthePreviewingChangespanelon

    theleftsideofthepage,asshowninFigure3-31.

    Asyouclickdifferentapparelitemsorextras,you'llfindthem



  • Tài liệu bạn tìm kiếm đã sẵn sàng tải về

    Hack 63. Add an API to Your Yahoo! 360 Blog

    Tải bản đầy đủ ngay(0 tr)

    ×