1
+ import axios from "axios" ;
2
+ import cheerio from "cheerio"
3
+
4
+ import { links } from "../links/links"
5
+
6
+ interface teams {
7
+ name : string
8
+ drivers : string [ ]
9
+ points : number
10
+ rank : number
11
+ carLogo : string | undefined
12
+ carImage : string | undefined
13
+ }
14
+
15
+ export function getTeamsData ( ) {
16
+
17
+ let teamsArray : teams [ ] = [ ]
18
+
19
+ axios ( links . teams ) . then ( response => {
20
+ const html = response . data
21
+ const $ = cheerio . load ( html )
22
+
23
+ $ ( 'fieldset' ) . each (
24
+ function ( ) {
25
+ const name : string = $ ( this ) . find ( ' div:nth-child(1) > .listing-info > .name.f1-bold--m > .f1-color--black' ) . text ( )
26
+ const points : number = parseInt ( $ ( this ) . find ( '.points > div:nth-child(1)' ) . text ( ) )
27
+ const rank : number = parseInt ( $ ( this ) . find ( '.rank' ) . text ( ) )
28
+
29
+ const driver1_0 : string = $ ( this ) . find ( ' div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)' ) . text ( )
30
+ const driver1_1 : string = $ ( this ) . find ( ' div:nth-child(1) > div:nth-child(3) > div:nth-child(1) > div:nth-child(1) > span:nth-child(2)' ) . text ( )
31
+ const driver2_0 : string = $ ( this ) . find ( 'div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(1)' ) . text ( )
32
+ const driver2_1 : string = $ ( this ) . find ( 'div:nth-child(1) > div:nth-child(3) > div:nth-child(2) > div:nth-child(1) > span:nth-child(2)' ) . text ( )
33
+ const driver1 = driver1_0 . concat ( " " , driver1_1 )
34
+ const driver2 = driver2_0 . concat ( " " , driver2_1 )
35
+ const drivers : string [ ] = [ driver1 , driver2 ]
36
+
37
+ const carLogo : string | undefined = $ ( this ) . find ( 'div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > picture:nth-child(1) > img:nth-child(2)' ) . attr ( 'data-src' )
38
+ const carImage : string | undefined = $ ( this ) . find ( 'div:nth-child(1) > div:nth-child(4) > picture:nth-child(1) > img:nth-child(5)' ) . attr ( 'data-src' )
39
+
40
+ if ( name . length !== 0 && points !== NaN && rank !== NaN ) {
41
+ const teamObj : teams = {
42
+ name,
43
+ drivers,
44
+ points,
45
+ rank,
46
+ carLogo,
47
+ carImage,
48
+ }
49
+ teamsArray . push ( teamObj )
50
+ }
51
+ } )
52
+ console . log ( teamsArray )
53
+ } )
54
+ }
0 commit comments