diff --git a/src/pages/Github.js b/src/pages/Github.js index 084fc71b..473024e1 100644 --- a/src/pages/Github.js +++ b/src/pages/Github.js @@ -9,13 +9,19 @@ import optionLang from '../source/trending.json'; const githublist = localStorage.getItem('github-list'); +const starSVG = ( + +); + export default class Github extends Component { - static typeName = 'trending' + static typeName = 'trending'; constructor(props) { super(props); this.state = { loading: false, - content: githublist, + content: githublist ? JSON.parse(githublist) : null, option: [ { label: '今天', @@ -49,8 +55,11 @@ export default class Github extends Component { return url; } getTrending(type) { - const localContent = localStorage.getItem('github-list'); + let localContent = localStorage.getItem('github-list'); if (!localContent) type = 'select'; // 判断是否直接选择 + else { + localContent = JSON.parse(localContent); + } this.setState({ loading: true }); const getDate = type === 'select' ? fetchTimely(this.getURL()) : fetchInterval(this.getURL(), 3, 'github-trending'); getDate.then((response) => { @@ -64,23 +73,36 @@ export default class Github extends Component { } return node; }); + const resultData = []; const $ = cheerio.load(response); - // 清除头像,避免被和谐 - $('.f6 .mr-3').not('.mr-3:first-child').empty(); - $('.starring-container').empty(); - const _html = $('div.explore-content').html(); - if (!_html) return; - localStorage.setItem('github-list', _html); + $('.Box-row').each(function (idx, item) { + // 不需要头像,避免被和谐 + const full_name = $(item).find('h1 a').text().replace(/(\n|\s)/g, ''); + const href = $(item).find('h1 a').attr('href').replace(/(\n|\s)/g, ''); + const language = $(item).find('span[itemprop=programmingLanguage]').text().replace(/(\n|\s)/g, ''); + const languageColor = $(item).find('span.repo-language-color'); + const stargazers_count = $(item).find('svg[aria-label="star"].octicon.octicon-star').parent().text().replace(/(\n|\s|,)/g, ''); + const forked = $(item).find('svg[aria-label="fork"].octicon.octicon-repo-forked').parent().text().replace(/(\n|\s|,)/g, ''); + const todayStar = $(item).find('.float-sm-right svg.octicon.octicon-star').parent().text().replace(/(\n|,)/g, '').trim(); + const description = $(item).find('p.text-gray').text().replace(/(\n)/g, '').trim(); + let color = ''; + if (language && languageColor && languageColor.css) { + color = languageColor.css('background-color'); + } + resultData.push({ full_name, language, color, description, forked, stargazers_count: Number(stargazers_count), todayStar, html_url: href, rank: idx + 1 }); + }); + if (!resultData) return; + localStorage.setItem('github-list', JSON.stringify(resultData)); if (!this.mounted) return; this.setState({ loading: false, - content: _html, + content: resultData, }); - }).catch(() => { + }).catch((err) => { this.setState({ loading: false }); if (!this.mounted) return; this.setState({ - content: githublist || '请求错误,请检查网路,或者重新刷新请求数据!', + content: this.state.content || '请求错误,请检查网路,或者重新刷新请求数据!', }); }); } @@ -98,6 +120,7 @@ export default class Github extends Component { }); } render() { + const { content } = this.state; return (