|
| 1 | +'use babel'; |
| 2 | + |
| 3 | +import React from 'react' |
| 4 | +import Problem from './problem' |
| 5 | +import request from 'request' |
| 6 | +import cheerio from 'cheerio' |
| 7 | + |
| 8 | +export default class Problems extends React.PureComponent { |
| 9 | + constructor(props){ |
| 10 | + super(props); |
| 11 | + this.state = { |
| 12 | + count: 0, |
| 13 | + probs: [] |
| 14 | + }; |
| 15 | + } |
| 16 | + |
| 17 | + fetch(url){ |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + request(url, (error, response, body) => { |
| 20 | + if (!error && response.statusCode == 200) { |
| 21 | + resolve(body) |
| 22 | + } else { |
| 23 | + reject({ |
| 24 | + reason: 'Unable to download page' |
| 25 | + }) |
| 26 | + } |
| 27 | + }) |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + scrape(html){ |
| 32 | + $ = cheerio.load(html) |
| 33 | + var i = 2 |
| 34 | + var probs = [] |
| 35 | + while(true){ |
| 36 | + var al = $(`#pageContent > div.datatable > div:nth-child(6) > table > tbody > tr:nth-child(${i}) > td.id > a`).text().trim() |
| 37 | + if(al.length==0){ |
| 38 | + break; |
| 39 | + }else{ |
| 40 | + var nm = $(`#pageContent > div.datatable > div:nth-child(6) > table > tbody > tr:nth-child(${i}) > td:nth-child(2) > div > div:nth-child(1) > a`).text().trim() |
| 41 | + var sub = $(`#pageContent > div.datatable > div:nth-child(6) > table > tbody > tr:nth-child(${i}) > td > a`).text().trim().substring(2) |
| 42 | + probs.push({alpha: al, name: nm, con: -1, test: 1, sub: sub}); |
| 43 | + } |
| 44 | + i++; |
| 45 | + } |
| 46 | + this.setState({probs: probs}) |
| 47 | + } |
| 48 | + |
| 49 | + componentWillMount(){ |
| 50 | + var url = "https://codeforces.com/contest/"+this.props.contest.id |
| 51 | + this.fetch(url).then((html) => { |
| 52 | + this.scrape(html); |
| 53 | + }).catch((error) => { |
| 54 | + atom.notifications.addWarning(error.reason) |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + render(){ |
| 59 | + return ( |
| 60 | + <div className="problems"> |
| 61 | + {this.state.probs && this.state.probs.map(prob=>{ |
| 62 | + return (<Problem prob={prob}/>) |
| 63 | + }) |
| 64 | + } |
| 65 | + </div> |
| 66 | + )} |
| 67 | +} |
| 68 | + |
| 69 | +// [ |
| 70 | +// {alpha: "A", name: "Suborrays"}, |
| 71 | +// {alpha: "B", name: "Fix you"}, |
| 72 | +// {alpha: "C", name: "Cyclic Permutations"}, |
| 73 | +// {alpha: "D", name: "505"}, |
| 74 | +// {alpha: "E", name: "Pairs of Pairs"} |
| 75 | +// ] |
0 commit comments