-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Initial implementation of tumblr importer
Fixes: #47
- Loading branch information
Showing
2 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const { promisify } = require("util"); | ||
const { parseString } = require("xml2js"); | ||
const _ = require("lodash"); | ||
|
||
const parse = promisify(parseString); | ||
|
||
module.exports = async function tumblrImport(fileContents) { | ||
const data = await parse(fileContents); | ||
|
||
let channel = data.tumblr.posts[0]; | ||
|
||
let authors = []; | ||
let tags = []; | ||
|
||
let content = _.chain(channel.post) | ||
.map(post => { | ||
let postType = post.$.type; | ||
let id = post.$.id; | ||
let date = new Date(post.$.date); | ||
let author = post.$.tumblelog; | ||
let title, content; | ||
|
||
switch (postType) { | ||
case "regular": | ||
title = post["regular-title"][0]; | ||
content = post["regular-body"][0]; | ||
break; | ||
case "quote": | ||
title = post["quote-text"][0]; | ||
content = post["quote-text"][0]; | ||
break; | ||
} | ||
|
||
if (!authors.find(existingAuthor => existingAuthor.id === author)) { | ||
authors.push({ | ||
id: author, | ||
name: author | ||
}); | ||
} | ||
|
||
let result = { | ||
id, | ||
title, | ||
authors, | ||
date, | ||
tags, | ||
content | ||
}; | ||
|
||
return result; | ||
}) | ||
.compact() | ||
.value(); | ||
|
||
return { | ||
tags, | ||
authors, | ||
content | ||
}; | ||
}; |