-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
159 lines (146 loc) · 4.56 KB
/
github.js
File metadata and controls
159 lines (146 loc) · 4.56 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const request = require("request");
const cheerio = require("cheerio");
let { jsPDF } = require("jspdf");
const fs = require("fs");
let $;
//1
//This object will contain all issues and their links
let data = {};
//2-A
//fetch the topics page of github
request("https://github.com/topics", function (err, res, body) {
$ = cheerio.load(body);
//2-B
//get top 3 topics anchor tag
let topThreeTopics = $(
".no-underline.d-flex.flex-column.flex-justify-center"
);
//2-C
//get the href attribute of those 3 anchor tags
//since they won't have complete url pass them to topicLinkGenerator function
//to get complete links and pass getTopicPage function as callback
for (let i = 0; i < 3; i++) {
topicLinkGenerator($(topThreeTopics[i]).attr("href"), getTopicPage);
}
});
//3
//this function adds the domain name to complete the url and pass that url to getTopicPage function
function topicLinkGenerator(subLink, callback) {
callback("https://github.com" + subLink);
}
//4
//this function requests the topic page by the url passed to it
//also extracts by spliting the topic name
//calls the findProjects function by passing them topicName body of the topic page which was requested and findIssues function.
function getTopicPage(url) {
request(url, function (err, res, body) {
let urlArray = url.split("/");
let topicName = urlArray[urlArray.length - 1];
findProjects(topicName, body, findIssues);
});
}
//5-A
//callback received is findIssues
function findProjects(folderName, body, callback) {
//5-B
// creates folder using the topic name passed to it during the function call
fs.mkdirSync(folderName);
//5-C
//if body of requested page is empty return
if (!body) {
return;
} else {
$ = cheerio.load(body);
}
//5-D
//get h1 all projects of in the page
let allProjects = $(".d-flex.flex-justify-between.my-3 h1 ");
//5-E
//if projects are more than 8 make them 8
if (allProjects.length > 8) {
allProjects = allProjects.slice(0, 8);
}
//5-F
//h1 of a project containes 2 anchor tags
// the second anchor tag contains project url
//use that url and make it issue url by simply adding /issues to it
//pass that url to the callback along with folderName and projectName and issueProcessor as callback
for (let i = 0; i < allProjects.length; i++) {
callback(
"https://github.com" +
$($(allProjects[i]).find("a")[1]).attr("href") +
"/issues",
folderName,
issueProcessor,
$($(allProjects[i]).find("a")[1]).text()
);
}
}
//6
//callback received here is issueProcessor
// this function get the issue page of project and passes each issue with its url to the callback
function findIssues(url, folderName, callback, projectName) {
request(url, (err, res, body) => {
if (!body) {
return;
} else {
$ = cheerio.load(body);
}
let allIssues = $(
".Link--primary.v-align-middle.no-underline.h4.js-navigation-open"
);
if (allIssues.length <= 0) {
callback(folderName, null, null);
} else {
for (let i = 0; i < allIssues.length; i++) {
callback(
folderName,
$(allIssues[i]).text(),
"https://github.com" + $(allIssues[i]).attr("href"),
projectName
);
}
}
});
}
//7
//this function adds the issue along with project and topic to data object
// and using that data object creates pdf
function issueProcessor(topic, issue, issueUrl, projectName) {
if (projectName) projectName = projectName.trim();
if (issue) {
let pdfPath = `./${topic}/${projectName}.pdf`;
if (!data[topic]) {
data[topic] = [
{ projectName: projectName, issues: [{ issue, url: issueUrl }] },
];
pdfGenerator(pdfPath, projectName, topic);
} else {
let requiredProjectIndex = data[topic].findIndex(
(p) => p.projectName === projectName
);
if (requiredProjectIndex !== -1) {
data[topic][requiredProjectIndex].issues.push({ issue, url: issueUrl });
pdfGenerator(pdfPath, projectName, topic);
} else {
data[topic].push({
projectName: projectName,
issues: [{ issue, url: issueUrl }],
});
pdfGenerator(pdfPath, projectName, topic);
}
}
}
}
//8
//function used to create pdf
function pdfGenerator(path, projectName, topic) {
let doc = new jsPDF();
let requiredIssues = data[topic].find(
(p) => p.projectName === projectName
).issues;
requiredIssues.forEach(function (issue, i) {
doc.text(5, 15 + i * 15, "issue: " + issue.issue + "\nURL: " + issue.url);
});
doc.save(path);
}