How to get Twitch watchtime (like streamelements) - Stack ...

文章推薦指數: 80 %
投票人數:10人

The file is named 'watchtime.php'. On an update request, it fetches the chatters and adds the time difference between the updates to the ... Home Public Questions Tags Users Collectives ExploreCollectives FindaJob Jobs Companies Teams StackOverflowforTeams –Collaborateandshareknowledgewithaprivategroup. CreateafreeTeam WhatisTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore HowtogetTwitchwatchtime(likestreamelements) AskQuestion Asked 9monthsago Active 4monthsago Viewed 30ktimes 0 Iamcurrentlyondevelopingalittletwitch.tvchatbot.Iknow,therearelikeathousandofthembutIwouldliketocreatemyown,justtohaveaproject-sothisisnotthepoint. Iwaswondering,howthewatchtimeofausercouldbecalculated.Justasstreamelements.comdoesitwiththeir!watchtimechatcommand.IdidnotfindanyAPIendpointtogettheusers,whicharecurrentlyviewingthestream.Theonlythingfoundisthecurrentviewcountinhttps://dev.twitch.tv/docs/api/reference#get-streamsbutthereisnowaytolinktoaspecificuser. Anyonehasanyidea? twitchtwitch-api Share Follow askedApr11'21at2:02 VestalisVestalis 26022goldbadges44silverbadges99bronzebadges 1 1 I'mnotsurehowstreamelementsworks.butiassumewhattheydoiswatchthejoinandpartevents:joinpartaddingausernametoa"inchannel"varandforallthoseuserstheycounthowlongtheyarethereuntilltheypart.andthewatchtimeissavedintheirdatabase. – RamondeVries Apr15'21at8:46 Addacomment  |  1Answer 1 Active Oldest Votes 1 There'snotanAPIendpointtodoitdirectly. However,I'verecentlydoneasimilarthingwithPHP,withouttheneedforTwitchAPIcredentials.ThechannelIworkforusesNightBot,soI'lltrytoexplainbrieflyhowitworkswithit,soyoucanimplementithoweveryouwant. Disclaimer:I'mnewtoPHP,soitprobablycanbeimproved;also,usingcommandsfromtheTwitchAPIcanprobablypolishthecode. Thefileisnamed'watchtime.php'.Onanupdaterequest,itfetchesthechattersandaddsthetimedifferencebetweentheupdatestothewatchtimeofeachuser.Onagetrequest,ittakesthewatchtimesavedandparsesitindays,hours,minutesandseconds. Toupdatethedatabaseforachannel,youhavetouseascheduleroraloopthatrequestshttps://path/to/watchtime.php?action=update&channel=target,where'target'isthechannelname.Therequestshavetobelessthan10minsapart.However,makesuretodotherequestifonlythechannelisstreaming.Everyloopcycle,thescriptrequeststheTwitchTMItofetchthechatters(ex.forLudwighttps://tmi.twitch.tv/group/user/ludwig/chatters)andthenitaddstoeachchatterthetimedifferencebetweentheupdates.IstorethevaluesinaJSON;foryourchatbot,Isuggestimplementingarealdatabase.Iftherearenoerrorsintheupdate,theresponsetextwillbe"OK". TogetthewatchtimefromNightBot,Iusedthecommand $(urlfetchhttps://path/to/watchtime.php?action=get&channel=$(channel)&user=$(user)&time=$(time)),where$(urlfetchurl)returnstheoutputofanurl,$(channel)isthechannelname,$(user)istheusername,and$(time)isthecurrenttime(it'suselessforthePHP,butit'susedtobypassthequerycachinginNightBot). However,IcalculatethewatchtimesonlyforviewersandVIPs;foramod,itwillreturnanerror(itcanbechanged). Here'sthePHPcode: 600) $passed=0; //Both$data[username]and$passedareintegers $s=$data[username]+$passed; //Parsingsecondstodays,hours,mins,secs $m=intdiv($s,60); $s-=$m*60; $h=intdiv($m,60); $m-=$h*60; $d=intdiv($h,24); $h-=$d*24; //Showtimesifonlytheyaregreaterthanzeros $args=[]; if($d>0)array_push($args,"{$d}days"); if($h>0)array_push($args,"{$h}hours"); if($m>0)array_push($args,"{$m}minutes"); if($s>0)array_push($args,"{$s}seconds"); echousername.'watchedthestreamfor'.implode(',',$args).'!'; }elseecho'Invalidusername"'.username.'":moderator,toonewornonexistent'; } Share Follow answeredApr28'21at10:37 Remmar00Remmar00 11611silverbadge55bronzebadges Addacomment  |  Highlyactivequestion.Earn10reputation(notcountingtheassociationbonus)inordertoanswerthisquestion.Thereputationrequirementhelpsprotectthisquestionfromspamandnon-answeractivity. Nottheansweryou'relookingfor?Browseotherquestionstaggedtwitchtwitch-apioraskyourownquestion. TheOverflowBlog TheGreatResignationishere.Whatdoesthatmeanfordevelopers? Podcast405:HelpingcommunitiesbuildtheirownLTEnetworks FeaturedonMeta ProvidingaJavaScriptAPIforuserscripts Congratulationstothe59sitesthatjustleftBeta Related 0 NewTwitchAPIgettingjsondataPython3 1 TwitchAPIreturning403withAuthorizationandClientID? HotNetworkQuestions Everythingisnormallylight.Itisdarkwavesthattravelandcausedarkness-how? Whyisthereafile"wireless-info"inmyhomedirectory? Similar(andstillongoing)serviceslikeTeleSpekIRsimulatorontheweb? Specificsofwhycomputermemorywassoexpensiveandscarce Whydowemakesuchbigdealaboutthe'unsolvability'ofthequintic? ForceofphotonsfromtheSunhittingafootballfield=weightof1dime? Howcouldtemporarysurrenderofself-governmenthelpNewfoundlandwithitsdebts? IsitappropriatetoaddresscharactersbytheirraceinD&D? CanIremovetheolderrevisionpackageofduplicatedsnappackages? Singlewire(nogroundwire)longdistancelowfrequencycommunication Is"Iwilldoyogaforgood"ambiguousbecauseofthephrase"forgood"? Whatdoesitmeanforaproto-neutronstartobeopaquetoneutrinos? Ifmagiccanmanipulatememoriesforinmatesondeathrow,whycapitalpunishmenthasn'tbeenabolished? Compareshapelesslists Whatistheproperwordtodesignatethissmallbag? Tryingtounderstandanoldcustomlicense IfIbuyastolenitemfrompartyA,andthenpartyAspendstheproceeds,whoisentitledtotheitem? WhereelsedidIseeaskeletonswordfight? WhymightRobertFrostspecifya'longtwo-pointedladder'inthepoem'AfterApplePicking'? Whyismyhouseenergyefficientonlyinwinter? Whomakestherulesofchess? Ischangingsomewordsplagiarism? FloorplanforJ.T.Kirk'sapartmentin"StarTrekII:TheWrathofKhan"and"StarTrekIII:TheSearchforSpock" WhenisthesupersynchronousorbitmoreefficientthanatypicalHohmanntransferwhereinclinationchangeandcircularizationaresimultaneous? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?