Skip to content

Commit cdf4571

Browse files
committed
Add 글들을 가져오는 기능 추가
1 parent 1217d95 commit cdf4571

File tree

5 files changed

+98
-1
lines changed

5 files changed

+98
-1
lines changed

app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ program.parse(process.argv);
1010

1111
const crawler = new Crawler(program.username);
1212

13+
console.log(crawler.getPosts());

crawler/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1+
const axios = require('axios');
2+
const { PostQuery } = require('./query');
3+
14
class Crawler {
25
constructor(username) {
36
this.username = username;
7+
this.__grahpqlURL = 'https://v2.velog.io/graphql';
48
}
59

610
async parse() {
711
const posts = await this.getPosts();
812
}
913

1014
async getPosts() {
11-
15+
const url = `https://velog.io/@${this.username}`;
16+
let response;
17+
18+
try {
19+
await axios.get(url);
20+
} catch (e) {
21+
if (e.response.status === 404) {
22+
console.error(`⚠️ 해당 유저를 찾을 수 없어요 \n username = ${this.username}`);
23+
process.exit(1);
24+
}
25+
26+
console.error(e);
27+
}
28+
29+
try {
30+
response = await axios.post(this.__grahpqlURL, PostQuery(this.username));
31+
} catch(e) {
32+
console.error(`⚠️ 벨로그에서 글을 가져오는데 실패했습니다. \n error = ${e}`);
33+
process.exit(1);
34+
}
35+
36+
const posts = response.data.data.posts;
37+
38+
console.log(`✅ ${this.username}님의 모든 글(${posts.length} 개) 을 가져옴`);
39+
40+
return response.data;
1241
}
1342
};
1443

crawler/query.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const PostQuery = (name) => ({
2+
operationName:'Posts',
3+
variables: {
4+
username: name,
5+
tag: null
6+
},
7+
8+
query: `query Posts($cursor: ID, $username: String, $temp_only: Boolean, $tag: String, $limit: Int) {
9+
posts(cursor: $cursor, username: $username, temp_only: $temp_only, tag: $tag, limit: $limit) {
10+
id
11+
title
12+
short_description
13+
thumbnail
14+
user {
15+
id
16+
username
17+
profile {
18+
id
19+
thumbnail
20+
__typename
21+
}
22+
__typename
23+
}
24+
url_slug
25+
released_at
26+
updated_at
27+
comments_count
28+
tags
29+
is_private
30+
likes
31+
__typename
32+
}
33+
}`
34+
});
35+
36+
37+
module.exports.PostQuery = PostQuery;

package-lock.json

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"homepage": "https://github.com/cjaewon/velog-backup#readme",
1919
"dependencies": {
20+
"axios": "^0.19.2",
2021
"commander": "^5.1.0"
2122
}
2223
}

0 commit comments

Comments
 (0)