Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 Add newsinlevels support. #23

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ const { articleModel } = rt;
const ut = require("../utils/utils");
const { generateResponse } = ut;
const axios = require("axios").default;
const cheerio = require("cheerio");
const {
newsInLevelsModel,
} = require("../schemas/supportedWebsite/newsInLevelsSchema");

// Get article detail
router.get("/:id", async function (req, res, next) {
try {
Expand Down Expand Up @@ -151,4 +156,71 @@ router.get("/china/daily/:articleID", async function (req, res, next) {
answerList: atts,
});
});

async function parseNewsInLevelsItemAndCreate(html) {
const $ = cheerio.load(html);
const cover = $(".img-wrap a img").attr("src");
const title = $(".news-block-right .title a").text().trim();
const excerpt = $(".news-block-right .news-excerpt")
.contents()
.eq(2)
.text()
.trim();
const level1Url = $(".fancy-buttons ul")
.children("li")
.eq(0)
.find("a")
.attr("href");
const level2Url = $(".fancy-buttons ul")
.children("li")
.eq(1)
.find("a")
.attr("href");
const level3Url = $(".fancy-buttons ul")
.children("li")
.eq(2)
.find("a")
.attr("href");
const originalNewsInLevel = await newsInLevelsModel.findOne({ level1Url });
console.log(`Original news in level: ${originalNewsInLevel}`);
console.log({ cover, title, excerpt, level1Url, level2Url, level3Url });
if (!originalNewsInLevel) {
const newsInLevel = await newsInLevelsModel.create({
cover,
title,
excerpt,
level1Url,
level2Url,
level3Url,
});
}
}

router.post("/grab/newsinlevels", async function (req, res, next) {
try {
const { url } = req.body;
const requestUrl = url || `https://www.newsinlevels.com/#/`;
console.log(url, requestUrl);
const d = await axios({ url: requestUrl });
const $ = cheerio.load(d.data);
const newsItemList = $(
".home-in .container .row .main-content .recent-news .news-block"
);
newsItemList.each(async (index, element) => {
await parseNewsInLevelsItemAndCreate(newsItemList.eq(index).html());
});
res.json(generateResponse());
} catch (error) {
res.json(ut.generateBadResponse());
}
});

router.post("/parse/newsinlevels", async function (req, res, next) {
const url = `https://www.newsinlevels.com/products/same-sex-marriage-in-india-level-1/#/`;
const a = await axios.get(url);
const $ = cheerio.load(a.data);
console.log(a.data);
res.json(generateResponse());
});

module.exports = router;
1 change: 1 addition & 0 deletions schemas/articleSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const articleSchema = new Schema(
title: String,
author: String,
content: String,
cover: String,
tags: [String],
/** 文章链接 */
link: { type: String, default: "" },
Expand Down
22 changes: 22 additions & 0 deletions schemas/supportedWebsite/newsInLevelsSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { ObjectId } = require("mongodb");
const mongoose = require("mongoose");

const { Schema } = mongoose;
const newsInLevelsSchema = new Schema(
{
title: String,
cover: String,
excerpt: String,
level1Url: String,
level2Url: String,
level3Url: String,
},

{
timestamps: true,
}
);

const newsInLevelsModel = mongoose.model("NewsInLevels", newsInLevelsSchema);

module.exports = { newsInLevelsModel };
Loading