33import { command , run , string , positional } from 'cmd-ts'
44import { TwitterApi } from 'twitter-api-v2'
55import dayjs from 'dayjs'
6- import fs from 'fs'
76import z from 'zod'
87
8+ import { getTodaysLectures , siteLink } from './util'
9+
910process . env . TZ = 'Europe/London'
1011
1112const app = command ( {
@@ -26,19 +27,19 @@ const app = command({
2627 return Promise . reject ( new Error ( 'Missing twitter client keys' ) )
2728 } )
2829
29- const data = await fs . promises
30- . readFile ( lectures , 'utf-8' )
31- . then ( ( x ) => JSON . parse ( x ) )
32- . then ( ( x ) => ( x as unknown [ ] ) . filter ( ( y ) : y is LectureSchema => LectureSchema . safeParse ( y ) . success ) )
33- . then ( ( x ) =>
34- x . filter (
35- ( y ) =>
36- dayjs ( y . time_start ) . isAfter ( dayjs ( ) . startOf ( 'day' ) ) && dayjs ( y . time_start ) . isBefore ( dayjs ( ) . endOf ( 'day' ) ) ,
37- ) ,
38- )
30+ const data = await getTodaysLectures ( lectures )
31+
32+ for ( let talk of data ) {
33+ const status = `
34+ ${ talk . title }
35+ ${ dayjs ( talk . time_start ) . format ( 'HH:mm' ) } : ${ talk . host . name }
36+ ${ siteLink ( talk ) }
37+
38+ ${ talk . link }
39+
40+ ${ talk . host . twitter } #london #lectures #publiclectures #lectureslondon
41+ ` . trim ( )
3942
40- for ( let i in data ) {
41- const status = generateLectureTweet ( data [ i ] )
4243 await client . v2 . tweet ( status ) . catch ( ( e ) => {
4344 console . error ( `Failed to post` , e )
4445 console . error ( status )
@@ -48,34 +49,9 @@ const app = command({
4849} )
4950run ( app , process . argv . slice ( 2 ) )
5051
51- const LectureSchema = z . object ( {
52- id : z . string ( ) ,
53- link : z . string ( ) ,
54- title : z . string ( ) ,
55- time_start : z . string ( ) ,
56- host : z . object ( { name : z . string ( ) , twitter : z . string ( ) } ) ,
57- } )
58- type LectureSchema = z . infer < typeof LectureSchema >
59-
6052const TwitSchema = z . object ( {
6153 appKey : z . string ( ) ,
6254 appSecret : z . string ( ) ,
6355 accessToken : z . string ( ) ,
6456 accessSecret : z . string ( ) ,
6557} )
66-
67- const generateLectureTweet = ( talk : z . TypeOf < typeof LectureSchema > ) =>
68- `${ talk . title }
69- ${ dayjs ( talk . time_start ) . format ( 'HH:mm' ) } : ${ talk . host . name }
70- https://lectures.london/${ slugit ( talk . host . name ) } /${ slugit ( talk . title ) }
71-
72- ${ talk . link }
73-
74- ${ talk . host . twitter } #london #lectures #publiclectures #lectureslondon
75- `
76-
77- const slugit = ( str : string ) =>
78- str
79- . replace ( / [ ^ \w \s ] / gim, '' )
80- . replace ( / \s { 1 , } / gim, '-' )
81- . toLowerCase ( )
0 commit comments