-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_wordpress_posts.js
127 lines (103 loc) · 3.23 KB
/
convert_wordpress_posts.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
'use strict';
const _ = require('lodash');
const fs = require('fs');
const parseString = require('xml2js').parseString;
const getMovieInfo = require('./tmdb_cache').getMovieInfo;
const getMoviePoster = require('./tmdb_cache').getMoviePoster;
const voca = require('voca');
const postsFileArg = process.argv[2];
const frequencies = {
'first-view': 'first',
'functional-first-time': 'functional-first',
'repeat': 'repeat',
'regular': 'regular'
};
const getMetaValue = function(metaKey, postmeta) {
let meta = _.find(postmeta, function(el) {
return el['wp:meta_key'][0] === metaKey;
});
if (_.isUndefined(meta) || meta.length === 0) {
return false;
}
return meta['wp:meta_value'][0];
};
const processWPCategories = function(categories) {
let movie = {};
movie.features = [];
_.each(categories, function(el, index, list) {
let category = el.$.nicename;
switch (category) {
case 'home':
case 'theater':
case 'plane':
movie.location = category;
break;
case 'first-view':
case 'functional-first-time':
case 'repeat':
case 'regular':
movie.frequency = frequencies[category];
break;
default:
movie.features.push(category);
}
});
return movie;
};
const getRelevantMovieInfo = function(movieInfo) {
let relevantInfo = _.pick(movieInfo, ['title', 'imdb_id', 'poster_path', 'release_date']);
relevantInfo.genres = [];
_.forEach(movieInfo.genres, genre => {
relevantInfo.genres.push(genre.name);
});
return relevantInfo;
};
var postsFilePath;
var xmlContent;
if (postsFileArg.charAt(0) === '/') {
postsFilePath = postsFileArg;
} else {
postsFilePath = './' + postsFileArg;
}
xmlContent = fs.readFileSync(postsFilePath, 'utf8');
parseString(xmlContent, (err, data) => {
let rawMovie;
let movieCount = data.rss.channel[0].item.length;
// movieCount = 1;
let movies = [];
for (let i = 0; i < movieCount; i++) {
let tmdbPromise;
let tmdbInfo;
let movie;
rawMovie = data.rss.channel[0].item[i];
// Simple properties from the WordPress export
movie = {
date: new Date(rawMovie.pubDate[0]),
content: rawMovie['content:encoded'][0],
tmdb_id: getMetaValue('tmdb_id', rawMovie['wp:postmeta'])
};
// Categories from the WP export
let categoryProperties = processWPCategories(rawMovie.category);
_.extend(movie, categoryProperties);
// if post has tmdb_id, fetch movie info & extend post object with it
if (movie.tmdb_id !== false) {
getMovieInfo(movie.tmdb_id)
.then(results => {
let relevantInfo = getRelevantMovieInfo(results);
_.extend(movie, relevantInfo);
// Save movie poster to disc
getMoviePoster(movie.poster_path);
// Save blog post (use voca.slugify on the title for the file name)
// movies.push(movie);
// console.log(JSON.stringify(movie));
// console.log(JSON.stringify(movies));
})
.catch(err => {
console.log('getMovieInfo:err => ', err);
});
} else {
console.log('Missing tmdb_id: ' + movie.date.toDateString() + ' ' + movie.title);
movie.imdb_id = getMetaValue('imdb_id', rawMovie['wp:postmeta']);
}
}
});