mirror of
https://github.com/GenderDysphoria/GenderDysphoria.fyi.git
synced 2025-11-25 12:32:42 +00:00
Added tweet translation mechanism (#96)
* Added tweet translation mechanism. * Removed test translation. It was done via Google Translate which I don't trust for this application.
This commit is contained in:
@@ -5,7 +5,41 @@ const log = require('fancy-log');
|
||||
const tweetparse = require('./lib/tweetparse');
|
||||
const Twitter = require('twitter-lite');
|
||||
const { hasOwn } = require('./lib/util');
|
||||
var twemoji = require('twemoji' );
|
||||
|
||||
function tweetText2Html(tweet_text) {
|
||||
let answer = tweet_text.split(/(\r\n|\n\r|\r|\n)+/)
|
||||
.map((s) => s.trim() && '<p>' + s + '</p>')
|
||||
.filter(Boolean)
|
||||
.join('');
|
||||
answer = twemoji.parse(answer);
|
||||
return answer;
|
||||
}
|
||||
|
||||
function applyI18N(original_tweet, twitter_i18n) {
|
||||
const id = original_tweet.id_str;
|
||||
// Make a shallow copy
|
||||
let tweet = Object.assign({}, original_tweet);
|
||||
|
||||
// Do we have a trnslation for this tweet?
|
||||
if (twitter_i18n[id] === undefined) {
|
||||
// If not, delete any translation fields just in case
|
||||
delete tweet.html_i18n;
|
||||
delete tweet.full_text_i18n;
|
||||
} else {
|
||||
// If yes, add the translations
|
||||
const originalLang = tweet["lang"] || "x-original";
|
||||
tweet.full_text_i18n = twitter_i18n[id].full_text_i18n;
|
||||
if (originalLang in tweet.full_text_i18n && tweet.full_text_i18n[originalLang] != tweet.full_text) {
|
||||
log.warn("Original text not matching for tweet "+id, { expected: tweet.full_text, got: tweet.full_text_i18n[originalLang]});
|
||||
} else {
|
||||
tweet.full_text_i18n[originalLang] = tweet.full_text;
|
||||
}
|
||||
}
|
||||
|
||||
// Return the tweet with the translations
|
||||
return tweet;
|
||||
}
|
||||
|
||||
module.exports = exports = async function tweets (pages) {
|
||||
const [ twitter, twitterBackup, twitterCache ] = await Promise.all([
|
||||
@@ -40,7 +74,6 @@ module.exports = exports = async function tweets (pages) {
|
||||
if (tweet.quoted_status_id_str && !twitterCache[tweet.quoted_status_id_str]) {
|
||||
tweetsNeeded.push(tweet.quoted_status_id_str);
|
||||
}
|
||||
// if (!twitterBackup[tweet.id_str]) twitterBackup[tweet.id_str] = tweet;
|
||||
twitterBackup[tweet.id_str] = tweet;
|
||||
twitterCache[tweet.id_str] = tweetparse(tweet);
|
||||
loaded.push(tweet.id_str);
|
||||
@@ -145,3 +178,26 @@ exports.attachTweets = function (tweetids, tweets) {
|
||||
return dict;
|
||||
}, {});
|
||||
};
|
||||
|
||||
exports.i18n = async function() {
|
||||
const [ twitterBackup, twitterCache, twitterI18N ] = await Promise.all([
|
||||
fs.readJson(resolve('twitter-backup.json')),
|
||||
fs.readJson(resolve('twitter-cache.json')).catch(() => ({})),
|
||||
fs.readJson(resolve('twitter-i18n.json')),
|
||||
]);
|
||||
|
||||
const twitterCacheBkp = JSON.stringify(twitterCache, null, 2);
|
||||
|
||||
// Make sure no translation is forgotten
|
||||
for (const id in twitterI18N) {
|
||||
if (id in twitterBackup) {
|
||||
twitterCache[id] = applyI18N(twitterBackup[id], twitterI18N);
|
||||
twitterCache[id] = tweetparse(twitterCache[id]);
|
||||
}
|
||||
}
|
||||
|
||||
const twitterCacheJson = JSON.stringify(twitterCache, null, 2);
|
||||
if (twitterCacheBkp != twitterCacheJson) {
|
||||
await fs.writeFile(resolve('twitter-cache.json'), twitterCacheJson);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user