3
3
import React from 'react' ;
4
4
import Problems from './problems' ;
5
5
import config from '../config'
6
- import request from 'request'
7
6
import cheerio from 'cheerio'
8
7
9
8
function exec ( cmd , handler = function ( error , stdout , stderr ) {
@@ -26,28 +25,21 @@ function couple(x) {
26
25
export default class Root extends React . PureComponent {
27
26
constructor ( props ) {
28
27
super ( props ) ;
29
- // this.state = { desc: "", id: "", changed: 0, timeremaining: 0"};
28
+ // this.state = {
29
+ // desc: "",
30
+ // id: "",
31
+ // changed: 0,
32
+ // timeremaining: 0,
33
+ // profimg: "//templates.joomla-monster.com/joomla30/jm-news-portal/components/com_djclassifieds/assets/images/default_profile.png"
34
+ // };
35
+
30
36
this . state = {
31
- desc : "Codeforces Round #663 (Div. 2)" ,
32
- id : 1391 ,
37
+ desc : "Codeforces Round #664 (Div. 2)" ,
38
+ id : 1395 ,
33
39
changed : 2 ,
34
40
timeremaining : 0 ,
35
41
profimg : "//templates.joomla-monster.com/joomla30/jm-news-portal/components/com_djclassifieds/assets/images/default_profile.png"
36
- }
37
- }
38
-
39
- fetch ( url ) {
40
- return new Promise ( ( resolve , reject ) => {
41
- request ( url , ( error , response , body ) => {
42
- if ( ! error && response . statusCode == 200 ) {
43
- resolve ( body )
44
- } else {
45
- reject ( {
46
- reason : 'Unable to download page'
47
- } )
48
- }
49
- } )
50
- } )
42
+ } ;
51
43
}
52
44
53
45
timesolver ( s ) {
@@ -59,30 +51,26 @@ export default class Root extends React.PureComponent {
59
51
return t
60
52
}
61
53
62
- scrape ( html ) {
63
- $ = cheerio . load ( html )
64
- var tmp = $ ( '#pageContent > div:nth-child(2) > span' ) . text ( ) . trim ( )
65
-
66
- if ( this . state . desc . length == 0 ) {
67
- var desc = $ ( "#pageContent > div:nth-child(1) > div" ) . text ( ) . trim ( )
68
- this . setState ( { desc : desc } )
69
- }
70
-
71
- if ( tmp . length ) {
72
- this . setState ( { timeremaining : this . timesolver ( tmp ) } )
73
- } else {
74
- this . setState ( { changed : 2 } )
75
- this . setState ( { timeremaining : 0 } )
54
+ setcontest ( contests ) {
55
+ for ( var contest of contests ) {
56
+ if ( contest . id == this . state . id ) {
57
+ this . setState ( { desc : contest . name } )
58
+ if ( contest . phase == "BEFORE" ) {
59
+ this . setState ( { timeremaining : - 1 * contest . relativeTimeSeconds } )
60
+ } else {
61
+ this . setState ( { timeremaining : 1 } )
62
+ }
63
+ break ;
64
+ }
76
65
}
77
66
}
78
67
79
68
fetchtimeremaining ( ) {
80
- var url = "https://codeforces.com/contest/" + this . state . id + "/countdown"
81
- this . fetch ( url ) . then ( ( html ) => {
82
- this . scrape ( html )
83
- } ) . catch ( ( error ) => {
84
- atom . notifications . addWarning ( error . reason )
85
- } )
69
+ var url = "https://codeforces.com/api/contest.list"
70
+ fetch ( url )
71
+ . then ( res => res . json ( ) )
72
+ . then ( res => this . setcontest ( res . result ) )
73
+ . catch ( err => console . log ( err ) )
86
74
}
87
75
88
76
display ( ) {
@@ -131,7 +119,7 @@ export default class Root extends React.PureComponent {
131
119
fetch ( url )
132
120
. then ( res => res . json ( ) )
133
121
. then ( res => this . setState ( { profimg : res . result [ 0 ] . titlePhoto } ) )
134
- . catch ( err => console . log ( "Error" ) ; )
122
+ . catch ( err => console . log ( err ) )
135
123
}
136
124
137
125
Loadprofile ( { profimg} ) {
@@ -146,8 +134,11 @@ export default class Root extends React.PureComponent {
146
134
)
147
135
}
148
136
149
- render ( ) {
137
+ componentWillMount ( ) {
150
138
this . fetchprofileimage ( )
139
+ }
140
+
141
+ render ( ) {
151
142
return (
152
143
< div >
153
144
< this . Loadprofile profimg = { this . state . profimg } />
0 commit comments