Skip to content

Commit

Permalink
Create serve.js
Browse files Browse the repository at this point in the history
  • Loading branch information
berlintay authored Aug 10, 2024
1 parent bd273a3 commit d085526
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions serve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const express = require('express');
const axios = require('axios');
const cheerio = require('cheerio');
const cron = require('node-cron');
const app = express();

let cachedRepoDetails = []; // Store the scraped data here

// Function to scrape GitHub trending repositories
const scrapeGitHubTrending = async () => {
try {
const { data } = await axios.get('https://github.com/trending');
const $ = cheerio.load(data);
const repoDetails = [];

$('article.Box-row').each((index, element) => {
const anchor = $(element).find('h2 a');
const orgNameElement = anchor.find('.text-normal');
let orgName = orgNameElement.text().trim().replace('/', '').trim();
const repoName = anchor.text().replace(orgName, '').trim().replace('/', '').trim();
const descriptionElement = $(element).find('p');
const description = descriptionElement.text().trim();

repoDetails.push({
organization: orgName,
repository: repoName,
description: description
});
});

cachedRepoDetails = repoDetails; // Update the cache with new data
} catch (error) {
console.error('Error scraping GitHub trending data:', error);
}
};

// Schedule the scraping to run every 7 hours
cron.schedule('0 */7 * * *', () => {
console.log('Fetching new data from GitHub Trending...');
scrapeGitHubTrending();
});

// Initial fetch to have data ready when the server starts
scrapeGitHubTrending();

// Endpoint to serve the cached GitHub trending repositories feed
app.get('/api/github-trending', (req, res) => {
res.json(cachedRepoDetails);
});

// Serve static files (like your main website)
app.use(express.static('public'));

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

0 comments on commit d085526

Please sign in to comment.