tweet.sh Twitter client written in simple Bash scr开源项目

我要开发同款
匿名用户2021年11月09日
49阅读
开发技术SHELL
所属分类终端/远程登录、应用工具
授权协议MIT License

作品详情

tweet.sh,aTwitterclientwritteninsimpleBashscriptSetup

YouneedtoprepareAPIkeysatfirst.Gotothefrontpage,createanewapp,andgenerateanewaccesstoken.

Thenputthemasakeyfileat~/.tweet.client.key,withtheformat:

MY_SCREEN_NAME=xxxxxxxxxxxxxxxxxxxMY_LANGUAGE=xxCONSUMER_KEY=xxxxxxxxxxxxxxxxxxxCONSUMER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxACCESS_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxACCESS_TOKEN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Ifthereisakeyfilenamedtweet.client.keyinthecurrentdirectory,tweet.shwillloadit.Otherwise,thefile~/.tweet.client.keywillbeusedasthedefaultkeyfile.

Moreover,youcangivethoseinformationviaenvironmentvariableswithoutakeyfile.

$exportMY_SCREEN_NAME=xxxxxxxxxxxxxxxxxxx$exportMY_LANGUAGE=xx$exportCONSUMER_KEY=xxxxxxxxxxxxxxxxxxx$exportCONSUMER_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$exportACCESS_TOKEN=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$exportACCESS_TOKEN_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx$./tweet.shpost"Hello!"

Thisformwillbeusefultoimplementabotprogram.

And,thisscriptusessomeexternalcommands.Youneedtoinstallthemviapackagesystemonyourenvironment:apt,yumorsomething.Requiredcommandsare:

curljqnkfopensslUsage$./tweet.sh[command][...arguments]

Availablecommandsare:

help:showsusageofthetweet.shitself.Readingexistingtweets(require"Read"permission)fetch(get,show):fetchesaJSONstringofatweet.search:searchestweetswithqueries.fetch-favorites(fetch-fav):fetchesfavoritetweets.fetch-tweets(fetch-posts):fetchestweetsofauser.watch-mentions(watch):watchesmentions,retweets,DMs,etc.,andexecuteshandlersforeachevent.type:detectsthetypeofthegiveninput.body:extractsthebodyofatweet.owner:extractstheownerofatweet.showme:reportstherawinformationofyourself.whoami:reportsthescreennameofyourself.language(lang):reportstheselectedlanguageofyourself.Makingsomechanges(require"Write"permission)post(tweet,tw):postsanewtweet.reply:repliestoanexistingtweet.upload:uploadsamediafile.delete(del,remove,rm):deletesatweet.favorite(fav):marksatweetasafavorite.unfavorite(unfav):removesfavoritedflagofatweet.retweet(rt):retweetsatweet.unretweet(unrt):deletestheretweetofatweet.follow:followsauser.unfollow:unfollowsauser.Operatedirectmessages(require"Accessdirectmessages"permission)fetch-direct-messages(fetch-dm,get-direct-messages,get-dm):fetchesrecentDMs.direct-message(dm):sendsaDM.Misc.resolve:resolvesashortenedURL.resolve-all:resolveallshortenedURLsinthegiveninput.

IfyouwanttohandleDMsbythewatch-mentionscommand,youhavetopermittheapptoaccessdirectmessages.

DetailedlogscanbeshownwiththeDEBUGflag,like:

$envDEBUG=1./tweet.shsearch-q"Bash"

ThisscriptismainlydesignedtobeaclientlibrarytoimplementTwitterbotprogram,insteadfordailyhumanuse.FormostcasesthisscriptreportsresponseJSONsofTwitter'sAPIsviathestandardoutput.SeedescriptionsofeachJSON:atweet,anevent,andotherresponsesalso.

SomecommandsrequireURLofatweet,andtheyacceptshortenedURLslikehttps://t.co/***.SuchURLsareautomaticallyresolvedasactualURLslikehttps://twitter.com/***/status/***.ThedetectipnpatternforsuchshortenedURLsisdefinedasURL_REDIRECTORSinthescript,anditmustbeupdatedfornewservices.

Readingexistingtweetsfetch(get,show):fetchesaJSONstringofatweet

Parameters

1stargument:theIDortheURLofthetweet.

Standardoutput

AJSONstringofthefetchedtweet.

Example

$./tweet.shfetch0123456789$./tweet.shfetchhttps://twitter.com/username/status/0123456789$./tweet.shget0123456789$./tweet.shshow0123456789search:searchestweetswithqueries.

Parameters

-q:queries.Ifyouspecifynoquery,thenyou'llseesampletweetsasresults.-c:maximumnumberoftweetstoberesponded.10bydefault.(optional)-s:theidofthelasttweetalreadyknown.(optional)Ifyouspecifythisoption,onlytweetsnewerthanthegiventweetwillbereturned.-m:theidofthetweetyouaresearchingtweetsolderthanit.(optional)Ifyouspecifythisoption,onlytweetsolderthanthegiventweetwillbereturned.-t:typeofresults.(optional)Possiblevalues:recent(default),popular,ormixed.-h:commandlinetorunforeachsearchresult.(optional)(Itwillreceivetweetsviathestandardinput.)-w:startwatchingwithouthandler.(optional)

Standardoutput

AJSONstringofthesearchresult.

Example

$./tweet.shsearch-q"queries"-c10$./tweet.shsearch-q"BashORShellScript"$./tweet.shsearch-q"BashORShellScript"-h'echo"found!";cat'$./tweet.shsearch-q"BashORShellScript"-w|whileread-rtweet;doecho"found!:${tweet}";donefetch-favorites(fetch-fav):fetchesfavoritetweets.

Parameters

-u:thescreennameoftheownerfavoritestobefetchedfrom.Yourselfbydefault.-c:maximumnumberoftweetstobefetched.10bydefault.-s:theidofthelasttweetalreadyknown.(optional)Ifyouspecifythisoption,onlytweetsnewerthanthegiventweetwillbereturned.-m:theidofthetweetyouaresearchingtweetsolderthanit.(optional)Ifyouspecifythisoption,onlytweetsolderthanthegiventweetwillbereturned.

Standardoutput

AJSONstringoffetchedfavorites.

Example

$./tweet.shfetch-favorites-c20$./tweet.shfetch-fav-c10-s0123456789fetch-tweets(fetch-posts):fetchestweetsofauser.

Parameters

-u:thescreennameoftheowneroftweetstobefetchedfrom.Yourselfbydefault.-c:maximumnumberoftweetstobefetched.10bydefault.-s:theidofthelasttweetalreadyknown.(optional)Ifyouspecifythisoption,onlytweetsnewerthanthegiventweetwillbereturned.-m:theidofthetweetyouaresearchingtweetsolderthanit.(optional)Ifyouspecifythisoption,onlytweetsolderthanthegiventweetwillbereturned.-a:includereplies.-r:includeretweets.-f:returnsfulltextofthetweet(nottruncated)sendstweet_mode=extendedThejsonresponsechangestheusualreturnedfieldfromtexttofull_text.

Standardoutput

AJSONstringofausertimeline.

Example

$./tweet.shfetch-tweets-uscreen_name-c20$./tweet.shfetch-posts-uscreen_name-c10-s0123456789Streaming

Basicallythiscommandprovidesabilitytogetsearchresultbasedonthegivenquery.

Ifyouwanttoobservenewtweetsmatchedtothequerycontinuously,specifyacallbackcommandlineasthehandlerviathe-hoption.

$./tweet.shsearch-q"queries"-h"echo'FOUND';cat"

Inthiscase,only-qand-hoptionsareavailable.Thescriptdoesn'texitautomaticallyifyouspecifythe-hoption.Tostoptheprocess,youneedtosendtheSIGINTsignalviaCtrl-Corsomething.

Importantnote:youcannotusethisfeaturetogetherwithwatch-mentionscommand.OnlyonestreamingAPIisallowedforyouatonce.Ifyouwanttowatchsearchresultswithmentions,usethe-kand-soptionsofthewatch-mentionscommand.

watch-mentions(watch):watchesmentions,retweets,DMs,etc.,andexecuteshandlersforeachevent.

Parameters

-k:comma-separatedlistoftrackingkeywords.-m:commandlinetorunforeachreplyormention.(optional)(Itwillreceivementiontweetsviathestandardinput.)-r:commandlinetorunforeachretweet.(optional)(Itwillreceiveretweettweetsviathestandardinput.)-q:commandlinetorunforeachquotation.(optional)(Itwillreceivequotationtweetsviathestandardinput.)-f:commandlinetorunwhenauserfollowsyou.(optional)(Itwillreceivefolloweventviathestandardinput.)-d:commandlinetorunwhenaDMisreceived.(optional)(Itwillreceivequotationtweetsviathestandardinput.)-s:commandlinetorunforeachsearchresult,matchedtothekeywordsgivenviathe-koption.(optional)

Standardoutput

Nothing.

Example1:withouthandlers

$./tweet.shwatch-mentions-k"keyword1,keyword2,..."|whileread-revent;doecho"event:${event}";done

Example2:withhandlers

$./tweet.shwatch-mentions-k"keyword1,keyword2,..."\-r"echo'REPLY';cat"\-t"echo'RT';cat"\-q"echo'QT';cat"\-f"echo'FOLLOWED';cat"\-d"echo'DM';cat"\-s"echo'SEARCH-RESULT';cat"

Thiscommandprovidesabilitytoobservevariouseventsaroundyouoranykeyword.

Inthiscasethisscriptstaysrunning.Tostoptheprocess,youneedtosendtheSIGINTsignalviaCtrl-Corsomething.

Importantnote:youcannotusethisfeaturetogetherwithsearchcommandwithahandler.OnlyonestreamingAPIisallowedforyouatonce.Ifyouwanttowatchsearchresultswithmentions,usethe-kand-soptionsinsteadofthesearchcommand.

type:detectsthetypeofthegiveninput.

Parameters

Thestandardinput:aJSONstringofatweet.-k:comma-separatedlistofkeywordswhichareusedfor"search".

Standardoutput

Thedatatypedetectedfromtheinput.Possiblevalues:event-follow:Aneventwhenyouarefollowed.direct-message:Adirectmessage.Itcanbewrappedwithakeydirect_message.quotation:AcommentedRT.retweet:AnRT.mention:Amentionorreply.search-result:Atweetwhichismatchedtothegivenkeywords.

Example

$echo"$tweet_json"|./tweet.shtype-kkeyword1,keyword2

Thiscommandprovidesabilitytodetectthetypeofeachobjectreturnedfromtheuserstream.Forunknowntypeinput,thisreturnsanexitstatus1andreportsnothing.

body:extractsthebodyofatweet.

Parameters

Thestandardinput:aJSONstringofatweet.(optional)1stargument:theIDortheURLofatweet.(optional)

Standardoutput

Thebodystringofthetweet.

Example

$./tweet.shbody0123456789$./tweet.shbodyhttps://twitter.com/username/status/0123456789$echo"$tweet_json"|./tweet.shbodyowner:extractstheownerofatweet.

Parameters

Thestandardinput:aJSONstringofatweet.(optional)1stargument:theIDortheURLofatweet.(optional)

Standardoutput

Thescreennameoftheowner.

Example

$./tweet.showner0123456789$./tweet.shownerhttps://twitter.com/username/status/0123456789$echo"$tweet_json"|./tweet.shownershowme:reportstherawinformationofyourself.

Parameters

Nothing.

Standardoutput

AJSONstringofthecredentialsAPI.

Example

$./tweet.shshowme

Thiswillbeusefulifyouwanttogetbothinformationswhoamiandlanguageatonce.

whoami:reportsthescreennameofyourself.

Parameters

Nothing.

Standardoutput

Thescreennameofyourself.

Example

$./tweet.shwhoamiusername

Importantnote:theratelimitoftheAPIusedbythiscommandisverylow.Ifyouwanttocallanotherlanguagecommandtogether,thenyoushoulduseshowmecommandinstead.

language(lang):reportstheselectedlanguageofyourself.

Parameters

Nothing.

Standardoutput

Thelanguagecodeselectedbyyourself.

Example

$./tweet.shlanguageen$./tweet.shlangen

Importantnote:theratelimitoftheAPIusedbythiscommandisverylow.Ifyouwanttocallanotherwhoamicommandtogether,thenyoushoulduseshowmecommandinstead.

Makingsomechangespost(tweet,tw):postsanewtweet.

Parameters

-m:comma-separatedlistofuploadedmediaIDs.Seealsotheuploadcommand.-l:addlocationtotweet.(optional)Allrestarguments:thebodyofanewtweettobeposted.Ifyoudon'tspecifynoextraparameters,thiscommandreadspostingbodyfromthestandardinput.

Standardoutput

AJSONstringofthepostedtweet.

Example

$./tweet.shpostAtweetfromcommandline$./tweet.shpost何らかのつぶやき$./tweet.shtweet@friendGoodmorning.$./tweet.shtw-m123,456,789MyPhotos!$./tweet.shpost-lAtweetwithlocation$catbody.txt|./tweet.shpost

Allrestargumentsfollowingtothecommandnamearepostedasatweet.Ifyouincludeauser'sscreennamemanuallyinthebody,itwillbecomeamention(notareply).

reply:repliestoanexistingtweet.

Parameters

-m:comma-separatedlistofuploadedmediaIDs.Seealsotheuploadcommand.1strestargument:theIDortheURLofatweettobereplied.Allotherrestarguments:thebodyofanewreplytobeposted.Ifyoudon'tspecifynoextraparameters,thiscommandreadspostingbodyfromthestandardinput.

Standardoutput

AJSONstringofthepostedreplytweet.

Example

$./tweet.shreply0123456789@friendAregularreply$./tweet.shreply0123456789Asilentreply$./tweet.shreplyhttps://twitter.com/username/status/0123456789@friendAregularreply$./tweet.shreplyhttps://twitter.com/username/status/0123456789Asilentreply$./tweet.shreply0123456789-m123,456,789Photoreply$catbody.txt|./tweet.shreply0123456789

Notethatyouhavetoincludetheuser'sscreennamemanuallyifitisneeded.Thiscommanddoesnotappenditautomatically.

upload:uploadsafile.

Parameters

1stargument:absolutepathtoalocalfile.

Standardoutput

AJSONstringoftheuplaodresult.

Example

$./tweet.shupload/path/to/file.pngdelete(del,remove,rm):deletesatweet.

Parameters

1stargument:theIDortheURLofatweettobedeleted.

Standardoutput

AJSONstringofthedeletedtweet.

Example

$./tweet.shdelete0123456789$./tweet.shdelhttps://twitter.com/username/status/0123456789$./tweet.shremove0123456789$./tweet.shrmhttps://twitter.com/username/status/0123456789favorite(fav):marksatweetasafavorite.

Parameters

1stargument:theIDortheURLofatweettobefavorited.

Standardoutput

AJSONstringofthefavoritedtweet.

Example

$./tweet.shfavorite0123456789$./tweet.shfavoritehttps://twitter.com/username/status/0123456789$./tweet.shfav0123456789$./tweet.shfavhttps://twitter.com/username/status/0123456789unfavorite(unfav):removesfavoritedflagofatweet.

Parameters

1stargument:theIDortheURLofatweettobeunfavorited.

Standardoutput

AJSONstringoftheunfavoritedtweet.

Example

$./tweet.shunfavorite0123456789$./tweet.shunfavoritehttps://twitter.com/username/status/0123456789$./tweet.shunfav0123456789$./tweet.shunfavhttps://twitter.com/username/status/0123456789retweet(rt):retweetsatweet.

Parameters

1stargument:theIDortheURLofatweettoberetweeted.

Standardoutput

AJSONstringofthenewtweetforaretweet.

Example

$./tweet.shretweet0123456789$./tweet.shretweethttps://twitter.com/username/status/0123456789$./tweet.shrt0123456789$./tweet.shrthttps://twitter.com/username/status/0123456789

Note,youcannotaddextracommentfortheretweet.Instead,ifyouwantto"quote"thetweet,thenyoujusthavetopostwiththeURLoftheoriginaltweet.

$./tweet.shpostGoodnews!https://twitter.com/username/status/0123456789unretweet(unrt):deletestheretweetofatweet.

Parameters

1stargument:theIDortheURLofatweettobeunretweeted.

Standardoutput

AJSONstringofthedeletedtweetforaretweet.

Example

$./tweet.shunretweet0123456789$./tweet.shunretweethttps://twitter.com/username/status/0123456789$./tweet.shunrt0123456789$./tweet.shunrthttps://twitter.com/username/status/0123456789follow:followsauser.

Parameters

1stargument:thescreennameofausertobefollowed,oraURLofatweet.

Standardoutput

AJSONstringofthefolloweduser.

Example

$./tweet.shfollow@username$./tweet.shfollowusername$./tweet.shfollowhttps://twitter.com/username/status/012345unfollow:unfollowsauser.

Parameters

1stargument:thescreennameofausertobeunfollowed,oraURLofatweet.

Standardoutput

AJSONstringoftheunfolloweduser.

Example

$./tweet.shunfollow@username$./tweet.shunfollowusername$./tweet.shunfollowhttps://twitter.com/username/status/012345

#Operatedirectmessages

fetch-direct-messages(fetch-dm,get-direct-messages,get-dm):fetchesrecentDMs.

Parameters

-c:maximumnumberofmessagestobefetched.10bydefault.

Standardoutput

AJSONstringofeventsfordirectmessages.

Example

$./tweet.shfetch-direct-messages-c20$./tweet.shget-direct-messages-c20direct-message(dm):sendsaDM.

Parameters

Allarguments:thebodyofanewdirectmessagetobesent.Ifyoudon'tspecifynoparameter,thiscommandreadsmessagebodyfromthestandardinput.

Standardoutput

AJSONstringoftheeventforthesentdirectmessage.

Example

$./tweet.shdirect-message@friendGoodmorning.$./tweet.shdirect-messagefriendGoodmorning.$./tweet.shdm@friendGoodmorning.$./tweet.shdmfriendGoodmorning.$catbody.txt|./tweet.shdirect-message@friend

#Misc.

resolve:resolvesashortenedURL.

Parameters

1stargument:ashortenedURL.

Standardoutput

TheresolvedoriginalURL.

Example

$./tweet.shresolvehttps://t.co/xxxxresolve-all:resolveallshortenedURLsinthegiveninput.

Parameters

Nothing.

Standardoutput

ThegiveninputwithresolvedURLs.

Example

$cat./tweet-body.txt|./tweet.shresolve-all
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论