33import React from 'react' ;
44import Problems from './problems' ;
55import config from '../config'
6- import request from 'request'
76import cheerio from 'cheerio'
87
98function exec ( cmd , handler = function ( error , stdout , stderr ) {
@@ -26,28 +25,21 @@ function couple(x) {
2625export default class Root extends React . PureComponent {
2726 constructor ( props ) {
2827 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+
3036 this . state = {
31- desc : "Codeforces Round #663 (Div. 2)" ,
32- id : 1391 ,
37+ desc : "Codeforces Round #664 (Div. 2)" ,
38+ id : 1395 ,
3339 changed : 2 ,
3440 timeremaining : 0 ,
3541 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+ } ;
5143 }
5244
5345 timesolver ( s ) {
@@ -59,30 +51,26 @@ export default class Root extends React.PureComponent {
5951 return t
6052 }
6153
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+ }
7665 }
7766 }
7867
7968 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 ) )
8674 }
8775
8876 display ( ) {
@@ -131,7 +119,7 @@ export default class Root extends React.PureComponent {
131119 fetch ( url )
132120 . then ( res => res . json ( ) )
133121 . then ( res => this . setState ( { profimg : res . result [ 0 ] . titlePhoto } ) )
134- . catch ( err => console . log ( "Error" ) ; )
122+ . catch ( err => console . log ( err ) )
135123 }
136124
137125 Loadprofile ( { profimg} ) {
@@ -146,8 +134,11 @@ export default class Root extends React.PureComponent {
146134 )
147135 }
148136
149- render ( ) {
137+ componentWillMount ( ) {
150138 this . fetchprofileimage ( )
139+ }
140+
141+ render ( ) {
151142 return (
152143 < div >
153144 < this . Loadprofile profimg = { this . state . profimg } />
0 commit comments