-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenRss.js
178 lines (144 loc) · 8.05 KB
/
genRss.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const fs = require('fs').promises;
var os = require('os');
const feed_fe = require('feed');
const Feed = feed_fe.Feed;
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// Connection URL
let mongoUrl = 'mongodb://rss-webtoons:27017';
let dbName = 'rss-webtoons';
let feedLocation = "/feed/"
if(os.platform() == "win32"){
mongoUrl = 'mongodb://localhost:27017';
dbName = 'testProject';
feedLocation = "c:\\tmp\\"
}
/*https://www.webtoons.com/en/challenge/sunny-side-skies/list?title_no=70280
{_id: "70280", generateOldDaily: true, typePrefix: "c_", title: "Sunny Side Skies", seriesNum: "70280", link: "https://www.webtoons.com/en/challenge/sunny-side-skies/list?title_no=70280"}
{_id: "117474", typePrefix: "c_", title: "I wish I were you", seriesNum: "117474", link: "https://www.webtoons.com/en/challenge/i-wish-i-were-you/list?title_no=117474"}
{_id: "303991", typePrefix: "c_", title: "Draigon", seriesNum: "303991", link: "https://www.webtoons.com/en/challenge/draigon/list?title_no=303991"}
{_id: "212675", typePrefix: "c_", title: "Lamp-chan", seriesNum: "212675", link: "https://www.webtoons.com/en/challenge/lamp-chan/list?title_no=212675"}
*/
async function GenerateFeed(){
let client, db;
try{
client = await MongoClient.connect(mongoUrl, {useNewUrlParser: true});
db = client.db(dbName);
const comics = db.collection("comics");
var allComics = await comics.find().toArray()
for(let obj of allComics){
let old_skip_count = 3
if(obj.oldCount){
old_skip_count = obj.oldCount
}
const seriesTitleNo = obj.seriesNum
const collection = db.collection(seriesTitleNo);
const episodes = db.collection(seriesTitleNo + '_episodes');
getEpisodeRow = async function (post){
s = post.objectId.split('_')
const episodeNo=s[2]
const titleNo=s[1]
return (await episodes.find({_id: titleNo + "_"+episodeNo}).toArray())[0]
}
generateContent = async function(post){
let episodeRow = await getEpisodeRow(post)
const picUrl = episodeRow.pic
const title = episodeRow.title
if('contentsTranslated' in post){
post.contents = post.contentsTranslated
}
let contents = /* "<img src=\"" + picUrl + "\"/>" + "<p>" +*/ "Chapter: " + title +"</p><p>User: " + post.userName + "</p><p>Post: " + post.contents + "</p>"
if(post.commentNo != post.parentCommentNo){
let parent = (await collection.find({_id: post.parentCommentNo}).toArray())[0]
if('contentsTranslated' in parent){
parent.contents = parent.contentsTranslated
}
contents += "<br>Parent User: " + parent.userName + "<br>Parent Post: " + parent.contents
}
return contents
}
getPostUrl = async function (post){
let episodeRow = await getEpisodeRow(post)
return episodeRow.url
}
async function MakeFeed(posts, title){
const feed = new Feed({
title: title,
description: "Feed for " + obj.title,
id: obj.link,
link: obj.link,
language: "en", // optional, used only in RSS 2.0, possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
// image: "https://upload.wikimedia.org/wikipedia/commons/0/09/Naver_Line_Webtoon_logo.png",
// image: "https://webtoon-phinf.pstatic.net/20190719_270/15635185426713GnDk_JPEG/40c1bb66-b65d-4dc0-88c6-046b389e679b.jpg",
// favicon: "https://webtoons-static.pstatic.net/image/favicon/favicon.ico?dt=2017082301",
// copyright: "All rights reserved 2013, John Doe",
// updated: new Date(2013, 6, 14), // optional, default = today
generator: "croppy", // optional, default = 'Feed for Node.js'
// feedLinks: {
// json: "https://example.com/json",
// atom: "https://example.com/atom"
// },
// author: {
// name: "John Doe",
// email: "[email protected]",
// link: "https://example.com/johndoe"
// }
});
for( post of posts){
if('contentsTranslated' in post){
post.contents = post.contentsTranslated
}
feed.addItem({
title: post.contents,
guid: post._id.toString(),
id: post._id.toString(),
link: await getPostUrl(post),
// description: post.contents,
description: await generateContent(post), //post.userName,
// content: await generateContent(post),
author: [
{
name: post.userName
}
],
date: new Date(post.modTimeGmt)
// image: post.image
// image: 'https://webtoon-phinf.pstatic.net/20190731_237/1564534544145ItKfB_JPEG/eeac74b4-62fb-423f-a8ed-a8e66f26d319.jpg?type=q90'
});
};
// feed.addCategory("Technologie");
// feed.addContributor({
// name: "Johan Cruyff",
// email: "[email protected]",
// link: "https://example.com/johancruyff"
// }
// );
return feed
}
var posts = await collection.find().sort( [["insertTime", -1], ["commentNo", -1]]).limit(100).toArray()
let feed = await MakeFeed(posts, obj.title)
await fs.writeFile(feedLocation + obj.seriesNum + "-atom.xml",feed.atom1())
await fs.writeFile(feedLocation + obj.seriesNum + "-rss.xml",feed.rss2())
// get posts, but not newest ones
function EpisodeGetEpisodeNumber(episode){return Number(episode._id.split("_")[1])}
var allEpisodes = await episodes.find().toArray()
function idSorter(a,b){
let aEp = EpisodeGetEpisodeNumber(a)
let bEp = EpisodeGetEpisodeNumber(b)
return bEp - aEp
}
allEpisodes.sort(idSorter)
toFilter = []
for(let i = 0; i< allEpisodes.length && i< old_skip_count; ++i){
toFilter.push(obj.typePrefix + allEpisodes[i]._id)
}
posts = await collection.find().sort( [["insertTime", -1], ["commentNo", -1]]).filter({objectId: {$nin: toFilter}}).limit(100).toArray()
feed = await MakeFeed(posts, "Old: "+ obj.title)
await fs.writeFile(feedLocation + obj.seriesNum + "-old-atom.xml",feed.atom1())
await fs.writeFile(feedLocation + obj.seriesNum + "-old-rss.xml",feed.rss2())
}
}
catch(err){ console.error(err); } // catch any mongo error here
finally{ client.close(); } // make sure to close your connection after
}
GenerateFeed()