|
2 | 2 |
|
3 | 3 | import React from 'react'; |
4 | 4 | import Problems from './problems'; |
| 5 | +import config from '../config' |
| 6 | +import request from 'request' |
| 7 | +import cheerio from 'cheerio' |
| 8 | + |
| 9 | +function exec(cmd, handler = function(error, stdout, stderr){ |
| 10 | + console.log(stdout); |
| 11 | + if(error !== null){ |
| 12 | + console.log(stderr)} |
| 13 | + }){ |
| 14 | + const childfork = require('child_process'); |
| 15 | + return childfork.exec(cmd, handler); |
| 16 | +} |
| 17 | + |
| 18 | +function couple(x) { |
| 19 | + if(x<10){ |
| 20 | + return "0"+x |
| 21 | + }else{ |
| 22 | + return x; |
| 23 | + } |
| 24 | +} |
5 | 25 |
|
6 | 26 | export default class Root extends React.PureComponent { |
7 | 27 | constructor(props){ |
8 | 28 | super(props); |
9 | | - this.state = { num: 663, div: 2, id: 0 }; |
| 29 | + // this.state = { desc: "", id: "", changed: 0, timeremaining: 0}; |
| 30 | + this.state = { desc: "Codeforces Round #663 (Div. 2)", id: 1176, changed: 2, timeremaining: 0} |
10 | 31 | } |
11 | 32 |
|
12 | | - display(){ |
13 | | - var editor = atom.workspace.getActiveTextEditor(); |
14 | | - if(editor){ |
15 | | - console.log(editor); |
| 33 | + fetch(url){ |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + request(url, (error, response, body) => { |
| 36 | + if (!error && response.statusCode == 200) { |
| 37 | + resolve(body) |
| 38 | + } else { |
| 39 | + reject({ |
| 40 | + reason: 'Unable to download page' |
| 41 | + }) |
| 42 | + } |
| 43 | + }) |
| 44 | + }) |
| 45 | + } |
| 46 | + |
| 47 | + timesolver(s){ |
| 48 | + var t = 0 |
| 49 | + s = s.split(":") |
| 50 | + t+=3600*parseInt(s[0]) |
| 51 | + t+=60*parseInt(s[1]) |
| 52 | + t+=parseInt(s[2]) |
| 53 | + return t |
| 54 | + } |
| 55 | + |
| 56 | + scrape(html){ |
| 57 | + $ = cheerio.load(html) |
| 58 | + var tmp = $('#pageContent > div:nth-child(2) > span').text().trim() |
| 59 | + |
| 60 | + if(this.state.desc.length==0){ |
| 61 | + var desc = $("#pageContent > div:nth-child(1) > div").text().trim() |
| 62 | + this.setState({desc: desc}) |
| 63 | + } |
| 64 | + |
| 65 | + if(tmp.length){ |
| 66 | + this.setState({timeremaining: this.timesolver(tmp)}) |
| 67 | + }else{ |
| 68 | + this.setState({changed: 2}) |
| 69 | + this.setState({timeremaining: 0}) |
16 | 70 | } |
17 | 71 | } |
18 | 72 |
|
19 | | - fetch(){ |
| 73 | + fetchtimeremaining(){ |
| 74 | + var url = "https://codeforces.com/contest/"+this.state.id+"/countdown" |
| 75 | + this.fetch(url).then((html) => { |
| 76 | + this.scrape(html) |
| 77 | + }).catch((error) => { |
| 78 | + atom.notifications.addWarning(error.reason) |
| 79 | + }) |
| 80 | + } |
20 | 81 |
|
| 82 | + display(){ |
| 83 | + this.setState({changed: 1}) |
| 84 | + this.fetchtimeremaining() |
| 85 | + var repeat = setInterval(()=>{ |
| 86 | + this.setState({timeremaining: this.state.timeremaining-1}) |
| 87 | + if(this.state.timeremaining==0){ |
| 88 | + this.setState({changed: 2}) |
| 89 | + clearInterval(repeat) |
| 90 | + } |
| 91 | + |
| 92 | + this.setState({timeremaining: Math.max(this.state.timeremaining, 0)}) |
| 93 | + },1000) |
| 94 | + } |
| 95 | + |
| 96 | + pathfinder(){ |
| 97 | + |
| 98 | + // var editor = atom.workspace.getActiveTextEditor(); |
| 99 | + // if(editor){ |
| 100 | + // var file = editor.buffer.file |
| 101 | + // if(file){ |
| 102 | + // var filepath = file.path |
| 103 | + // exec("python "+filepath); |
| 104 | + // } |
| 105 | + // } |
| 106 | + } |
| 107 | + |
| 108 | + Timer({sec}){ |
| 109 | + return( |
| 110 | + <div className="timer"> |
| 111 | + <span>Before contest</span> |
| 112 | + <span>{couple(Math.floor(sec/3600))} : {couple(Math.floor((sec%3600)/60))} : {couple(Math.floor((sec%3600)%60))}</span> |
| 113 | + </div> |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + fetchinput(event){ |
| 118 | + if(event.target){ |
| 119 | + this.setState({id: event.target.value}); |
| 120 | + } |
| 121 | + // console.log(this.state); |
21 | 122 | } |
22 | 123 |
|
23 | 124 | render() { |
24 | 125 | return ( |
25 | 126 | <div> |
26 | | - <h2>Codeforces Round #{this.state.num} (Div. {this.state.div})</h2> |
27 | | - {this.state.id ? <input type="text"/> : <Problems contest={this.state}/> } |
28 | | - <button onClick={()=> this.fetch()}>Submit</button> |
| 127 | + {this.state.changed ? <h2>{this.state.desc}</h2> : null} |
| 128 | + |
| 129 | + {this.state.changed==0 ? ( |
| 130 | + <div className="fillup"> |
| 131 | + <span>ContestId</span> |
| 132 | + <input type="text" value={this.state.id} onChange={this.fetchinput.bind(this)}/> |
| 133 | + <br/><button onClick={()=> this.display()}>Submit</button> |
| 134 | + </div> |
| 135 | + ) : null} |
| 136 | + |
| 137 | + {this.state.changed==1 ? ( |
| 138 | + <div className="waiting"> |
| 139 | + <this.Timer sec={this.state.timeremaining} /> |
| 140 | + <button onClick={()=>this.fetchtimeremaining()} >Refresh</button> |
| 141 | + </div> |
| 142 | + ):null} |
| 143 | + |
| 144 | + {this.state.changed==2 ? ( |
| 145 | + <Problems contest={this.state}/> |
| 146 | + ):null} |
| 147 | + |
29 | 148 | </div> |
30 | 149 | ); |
31 | 150 | } |
32 | 151 | } |
| 152 | +// <button onClick={()=> this.pathfinder()}>Check</button> |
33 | 153 |
|
34 | 154 | // <button onClick={()=> this.toggler()}>Click me</button> |
| 155 | + |
| 156 | +// <Problems contest={this.state} /> |
0 commit comments