forked from sivagao/github-serendipity.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlogs.js
More file actions
144 lines (124 loc) · 3.74 KB
/
Copy pathgitlogs.js
File metadata and controls
144 lines (124 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// https://api.github.com/search/repositories?q=topic:react%20created:2000-01-01..*&per_page=100&sort=stars&order=desc
// http://www.gitlogs.com/most_popular?topic=development =>
//
// q:topic:development created:2000-01-01..*
// per_page:100
// page:2
// sort:stars
// order:desc
import axios from 'axios'
import _ from 'lodash'
import {store, hourExpire} from '../utils/store'
export function getAllTopics (cb){
/*
{
"topic": "android",
"tag": "mobile",
"repos": 100,
"total_stars": 399333,
"median": 2660.5
}
*/
// import TopicsData from '../data/gitlogs_topics.json';
// http://stackoverflow.com/questions/31758081/loading-json-data-from-local-file-into-react-js
const cacheKey = `topics`
if(store.get(cacheKey)) {
cb(store.get(cacheKey))
} else {
axios({
// url: 'http://www.gitlogs.com/data/github_topics.json'
url: `${process.env.PUBLIC_URL}/gitlogs_topics.json`,
}).then(({data})=>{
const CategoryTopics = _.reverse(_.sortBy(_.map(_.groupBy(data, 'tag'), (topics, cateName)=>{
return {
name: cateName,
topics: _.reverse(_.sortBy(topics, 'median')), // median or total_stars
total_stars: _.reduce(topics, (total_stars, n)=>(total_stars + n.total_stars), 0) // total_stars
}
}), 'total_stars'))
store.set(cacheKey, CategoryTopics, hourExpire(6))
cb(CategoryTopics)
})
}
}
// has cache 6 hours
export function getTrendingBatch(cb) {
// Todo support 滚动加载更多...
const cacheKey = `repos:trending`;
if(store.get(cacheKey)) {
cb(store.get(cacheKey))
} else {
axios({
url: `${process.env.PUBLIC_URL}/gitlogs_trending.json`,
}).then(({data})=>{
store.set(cacheKey, data, hourExpire(6))
return cb(data);
})
}
}
// has cache 6 hours
export function getTopicRepoList (topic, cb){
const apiURL = 'https://api.github.com/search/repositories'
// https://developer.github.com/v3/search/#search-repositories
const cacheKey = `repos.topic.${topic}`;
if(store.get(cacheKey)) {
return cb(store.get(cacheKey));
} else {
axios({
url: apiURL,
params: {
per_page: 600,
page: 1,
sort: 'stars',
order: 'desc',
q: `topic:${topic} created:2000-01-01..*`
}
}).then(({data: {items}})=>{
const menus = [{
name: topic,
key: 'topic_only',
list: _.map(items, (i)=>{
return {
key: i.full_name.replace('/', '___'),
name: `${i.name} ${i.stargazers_count}`,
description: i.description
}
})
}];
store.set(cacheKey, menus, hourExpire(6)) // expire 6 hours
return cb(menus);
});
}
}
const blackListForRank = ['amattson21/gitjob', 'egorsmkv/google-spaces']
export function getRankAsMenus(cb) {
const cacheKey = `repos:rank`;
if(store.get(cacheKey)) {
cb(store.get(cacheKey))
} else {
axios({
url: `${process.env.PUBLIC_URL}/rank_data.json`,
}).then(({data})=>{
const _RepoData = _.filter(data, (i)=> (!blackListForRank.includes(i._id)))
const RankAsMenus = [[Infinity, 1000], [1000, 500], [500, 200], [200, 0]].map((c)=>{
const list = _RepoData.filter((i)=>{
return c[0] > i.countall && i.countall >= c[1];
}).sort( (a, b) => (b.countall - a.countall))
.map((i)=>{
return {
key: i._id.replace('/', '___'),
name: i._id.split('/')[1] + ' ' + i.countall,
description: i.desc
}
});
return {
key: c.toString(),
name: 'Between ' + c.toString() + ' stars',
list: list
}
})
store.set(cacheKey, RankAsMenus, hourExpire(6))
return cb(RankAsMenus);
})
}
}