11import * as util from '../../util.js' ;
22
33
4- let speedrundotcomKey = localStorage . getItem ( 'speedrundotcomKey' ) ;
4+ let cachedKey = localStorage . getItem ( 'speedrundotcomKey' ) ;
5+ const getSpeedrundotcomKey = async ( ) => {
6+ if ( cachedKey ) {
7+ return cachedKey ;
8+ } else {
9+ const settings = await util . ajax ( 'https://www.speedrun.com/settings' ) ;
10+ const $username = settings . querySelector (
11+ '.container.navbar-bottom .dropdown.user .username' ) ;
12+ if ( ! $username ) {
13+ throw Error ( 'Unable to get API token from speedrun.com, not logged in?' ) ;
14+ }
15+ const username = $username . textContent ;
16+ const apiPage = await util . ajax (
17+ `https://www.speedrun.com/${ username } /settings/api` ) ;
18+ const $code = apiPage . querySelector ( 'code' ) ;
19+ if ( ! $code ) {
20+ throw Error ( 'Unable to get API token from speedrun.com' ) ;
21+ }
22+ cachedKey = $code . textContent ;
23+ localStorage . setItem ( 'speedrundotcomKey' , cachedKey ) ;
24+ return cachedKey ;
25+ }
26+ } ;
27+
528export default async ( ) => {
629 const getMetaForRun = ( url ) => {
730 return util . ajax ( url , {
@@ -17,6 +40,7 @@ export default async () => {
1740 users : response . data . players ,
1841 } ;
1942 } ,
43+ ttl : 1000 * 60 * 30 // 30 min
2044 } ,
2145 } ) ;
2246 } ;
@@ -27,13 +51,25 @@ export default async () => {
2751 if ( user . rel === 'guest' ) {
2852 return { name : user . name } ;
2953 } else {
30- return util . ajax ( 'http ://www.speedrun.com/api/v1/users/' + user . id , {
54+ return util . ajax ( 'https ://www.speedrun.com/api/v1/users/' + user . id , {
3155 cache : {
32- transform : ( response ) => ( {
33- url : response . data . weblink ,
34- name : response . data . names . international ,
35- } )
36- }
56+ transform : async ( response ) => {
57+ const url = response . data . weblink ;
58+ const user = url . slice ( url . lastIndexOf ( '/' ) + 1 ) ;
59+ let thumbnail =
60+ `https://www.speedrun.com/themes/user/${ user } /image.png` ;
61+ const imageRes = await fetch ( thumbnail , {
62+ method : 'HEAD'
63+ } ) ;
64+ if ( ! imageRes . ok ) { thumbnail = null ; }
65+ return {
66+ url,
67+ name : response . data . names . international ,
68+ thumbnail,
69+ } ;
70+ } ,
71+ ttl : 1000 * 60 * 60 * 24 // 1day
72+ } ,
3773 } ) ;
3874 }
3975 } ) ) ;
@@ -44,11 +80,12 @@ export default async () => {
4480 const addMetaToVideo = async ( run , meta ) => {
4581 if ( ! run . game && meta . gameID ) {
4682 const game = await util . ajax (
47- 'http ://www.speedrun.com/api/v1/games/' + meta . gameID , {
83+ 'https ://www.speedrun.com/api/v1/games/' + meta . gameID , {
4884 cache : {
4985 transform : ( response ) => ( {
5086 name : response . data . names . international
5187 } ) ,
88+ ttl : 1000 * 60 * 60 * 24 // 1day
5289 } ,
5390 } ) ;
5491 run . game = game . name ;
@@ -63,23 +100,15 @@ export default async () => {
63100 run . url = meta . url ;
64101 run . desc = meta . desc ;
65102 const results = await Promise . all ( [
66- addUsersToRun . bind ( null , run , meta ) ,
67- addMetaToVideo . bind ( null , run , meta )
103+ addUsersToRun ( run , meta ) ,
104+ addMetaToVideo ( run , meta )
68105 ] ) ;
69106 return results [ 0 ] && results [ 1 ] ;
70107 } ;
71108
72- if ( ! speedrundotcomKey ) {
73- const body = await util . ajax ( 'http://www.speedrun.com/settings' ) ;
74- const $code = body . getElementsByTagName ( 'code' ) [ 0 ] ;
75- if ( ! $code ) {
76- throw Error ( 'Unable to retrieve API token from speedrun.com' ) ;
77- }
78- speedrundotcomKey = $code . textContent ;
79- localStorage . setItem ( 'speedrundotcomKey' , speedrundotcomKey ) ;
80- }
81- const results = await util . ajax ( 'http://www.speedrun.com/api/v1/notifications' , {
82- headers : { 'X-API-Key' : speedrundotcomKey } ,
109+ const key = await getSpeedrundotcomKey ( ) ;
110+ const results = await util . ajax ( 'https://www.speedrun.com/api/v1/notifications' , {
111+ headers : { 'X-API-Key' : key } ,
83112 } ) ;
84113 const runs = results . data
85114 . filter ( noti => noti . item . rel === 'run' )
@@ -89,7 +118,7 @@ export default async () => {
89118 url : noti . item . uri ,
90119 } ,
91120 url : noti . links [ 0 ] . uri ,
92- title : noti . text ,
121+ title : null ,
93122 timestamp : new Date ( noti . created ) . getTime ( ) ,
94123 } ;
95124 } ) ;
0 commit comments