Skip to content

Commit

Permalink
feat✨: add rss xml parse
Browse files Browse the repository at this point in the history
  • Loading branch information
isboyjc committed Aug 1, 2024
1 parent ff6bed2 commit 5ecd868
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"axios": "^1.7.2",
"cheerio": "1.0.0-rc.12",
"moment": "^2.30.1"
"moment": "^2.30.1",
"xml2js": "^0.6.2"
}
}
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions script/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const axios = require('axios');
const { parse } = require('./parse')
const { baseURL } = require('./base')
const { saveToJSON } = require('./save')
const { save } = require('./save')

const instance = axios.create({
baseURL: baseURL,
Expand All @@ -21,10 +21,9 @@ async function run (since = 'daily', language = 'all') {

console.log('get', path)
const { data } = await instance.get(path);

console.log('done', path)

await saveToJSON(parse(data), since, language)
await save(parse(data), since, language)
};

(async () => {
Expand Down
67 changes: 56 additions & 11 deletions script/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { fileBaseURL, templateBaseURL } = require('./base')

const dateTime = moment().format('MMMM Do YYYY, h:mm:ss a');

async function saveToJSON (data, since = 'daily', language = 'all') {
async function save (data, since = 'daily', language = 'all') {
const filePath = fileBaseURL + since
await mkdirSync(
path.resolve(__dirname, filePath),
Expand All @@ -15,27 +15,72 @@ async function saveToJSON (data, since = 'daily', language = 'all') {
}
);

const json = generationJson(data, since, language)
const rss = JsonToRss(json)

await writeFileSync(
path.resolve(__dirname, filePath, `${language}.json`),
JSON.stringify(json, null, 2),
'utf-8'
);
await writeFileSync(
path.resolve(__dirname, filePath, `${language}.xml`),
rss,
'utf-8'
);
};

function generationJson(data, since = 'daily', language = 'all'){
let templateContent = readFileSync(path.resolve(__dirname, templateBaseURL, 'template.json'), 'utf8');
const replacements = {
'{{language}}': language.replace(/^./, match => match.toUpperCase()),
'{{since}}': since.replace(/^./, match => match.toUpperCase()),
'{{updateTime}}': dateTime
'{{pubDate}}': dateTime
};
for (const [key, value] of Object.entries(replacements)) {
templateContent = templateContent.replace(new RegExp(key, 'g'), value);
}
let templateJson = JSON.parse(templateContent);
// console.log(templateJson)

templateJson.data = data
templateJson.items = data
}

await writeFileSync(
path.resolve(__dirname, filePath, `${language}.json`),
JSON.stringify(templateJson, null, 2),
'utf-8'
);
};
function JsonToRss(json){
const rssFeed = {
rss: {
$: { version: "2.0" },
channel: {
title: json.title,
link: json.link,
description: json.description,
pubDate: json.pubDate,
item: json.items.map(item => ({
title: item.title,
link: item.url,
description: item.description,
// pubDate: json.pubDate,
guid: item.url,
language: item.language,
languageColor: item.languageColor,
stars: item.stars,
forks: item.forks,
addStars: item.addStars,
contributors: {
contributor: item.contributors.map(contributor => ({
avatar: contributor.avatar,
name: contributor.name,
url: contributor.url
}))
}
}))
}
}
};

const builder = new Builder({ headless: true });
return builder.buildObject(rssFeed);
}

module.exports = {
saveToJSON
save
}
6 changes: 3 additions & 3 deletions template/template.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "GitHub {{language}} Languages {{since}} Trending",
"description": "{{since}} Trending of {{language}} Languages in GitHub",
"source": "https://github.com/isboyjc/github-trending-api",
"updateTime": "{{updateTime}}",
"data": []
"link": "https://github.com/isboyjc/github-trending-api",
"pubDate": "{{pubDate}}",
"items": []
}

0 comments on commit 5ecd868

Please sign in to comment.