File tree 4 files changed +172
-0
lines changed
JavaScript/Hashnode_Scripts
4 files changed +172
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Hashnode Script
2
+
3
+ - This Scripts Collect The desired number of Best Posts from Hashnode.
4
+ - Each Post will include Title , Brief Summary About Post and Author of Post.
5
+ - All Data will be Stored in output.csv.
6
+ - You can Customize Script According in Local Environment.
7
+
8
+ ## Setup instructions
9
+
10
+ - Clone the Folder ` git clone https://github.com/HarshCasper/Rotten-Scripts `
11
+ - Change the directory ``` cd Rotten-Scripts/JavaScript/Hashnode_Scripts ``` .
12
+ - Run command ` npm install ` .
13
+ - Run the script using the command ` node script.js required_no_of_posts ` .
14
+ - ` required_no_of_posts ` defines the number of Posts needed.
15
+ - So run script as ` node script.js 14 ` .
16
+
17
+ ## Output
18
+
19
+ ![ output-img] ( https://i.imgur.com/p4Nf1Oi.png )
20
+
21
+ ## Author
22
+
23
+ [ Sukriti Sood] ( https://github.com/Sukriti-sood )
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " hashnode_script" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " script.js" ,
6
+ "scripts" : {
7
+ "test" : " echo \" Error: no test specified\" && exit 1"
8
+ },
9
+ "author" : " " ,
10
+ "license" : " ISC" ,
11
+ "dependencies" : {
12
+ "node-fetch" : " ^2.6.1" ,
13
+ "objects-to-csv" : " ^1.3.6"
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ // all requires
2
+ const fetch = require ( "node-fetch" ) ;
3
+ const ObjectsToCsv = require ( "objects-to-csv" ) ;
4
+
5
+ let page = 0 ;
6
+ let postCount = process . argv [ 2 ] ;
7
+
8
+ // Working Function
9
+ fetchPosts = async ( postCount ) => {
10
+ let postsData = [ ] ; // stores posts
11
+ let count = 0 ;
12
+ let currentPostCount = 0 ;
13
+
14
+ while ( currentPostCount < postCount ) {
15
+ const query = `{
16
+ storiesFeed(type:BEST
17
+ page:${ page } ){
18
+ title
19
+ brief
20
+ author{
21
+ name
22
+ }
23
+ }
24
+ }` ;
25
+
26
+ const response = await fetch ( "https://api.hashnode.com" , {
27
+ method : "POST" ,
28
+ headers : {
29
+ "Content-type" : "application/json" ,
30
+ } ,
31
+ body : JSON . stringify ( {
32
+ query
33
+ } ) ,
34
+ } ) ;
35
+
36
+ const apiResponse = await response . json ( ) ;
37
+
38
+ let posts = apiResponse . data . storiesFeed ;
39
+
40
+ currentPostCount += posts . length ;
41
+
42
+ page ++ ;
43
+
44
+ for ( let post of posts ) {
45
+ count ++ ;
46
+
47
+ let postObject = { } ;
48
+
49
+ postObject [ "title" ] = post [ "title" ] ;
50
+
51
+ postObject [ "brief" ] = post [ "brief" ] . split ( "\n" ) . join ( " " ) ;
52
+
53
+ postObject [ "author" ] = post . author [ "name" ] ;
54
+
55
+ postsData . push ( postObject ) ;
56
+
57
+ if ( count >= postCount ) break ;
58
+ }
59
+ }
60
+
61
+ const csv = new ObjectsToCsv ( postsData ) ;
62
+
63
+ await csv . toDisk ( "./output.csv" ) ;
64
+ } ;
65
+
66
+ fetchPosts ( postCount ) ;
You can’t perform that action at this time.
0 commit comments