1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Twitter Archivist: Expand t.co URLs in tweets

This commit is contained in:
jeremy@jermolene.com 2022-11-13 19:05:09 +00:00
parent 34353f4065
commit af6c017086
3 changed files with 52 additions and 15 deletions

View File

@ -74,14 +74,14 @@ TwitterArchivist.prototype.loadArchive = async function(options) {
$tw.utils.each(tweetData,function(tweet) { $tw.utils.each(tweetData,function(tweet) {
// Compile the tags for the tweet // Compile the tags for the tweet
var tags = ["$:/tags/Tweet"]; var tags = ["$:/tags/Tweet"];
// Create tiddlers for each mentioned tweeter, and hyperlink the mention in the test // Accumulate the replacements/insertions to the text as an array of {startPos:,endPos:,fnTransform:}
var rawText = tweet.tweet.full_text, var modifications = [];
posText = 0, // Mentions
text = "";
var mentions = []; var mentions = [];
$tw.utils.each(tweet.tweet.entities.user_mentions,function(mention) { $tw.utils.each(tweet.tweet.entities.user_mentions,function(mention) {
var title = "Tweeter - " + mention.id_str; var title = "Tweeter - " + mention.id_str;
tags.push(title); tags.push(title);
mentions.push(mention.id_str);
wiki.addTiddler({ wiki.addTiddler({
title: title, title: title,
screenname: "@" + mention.screen_name, screenname: "@" + mention.screen_name,
@ -89,18 +89,53 @@ TwitterArchivist.prototype.loadArchive = async function(options) {
user_id: mention.id_str, user_id: mention.id_str,
name: mention.name name: mention.name
}); });
text = text + modifications.push({
$tw.utils.htmlEncode(rawText.substring(posText,mention.indices[0])) + startPos: mention.indices[0],
"<$link to=\"" + title + "\">" + endPos: mention.indices[1],
$tw.utils.htmlEncode(rawText.substring(mention.indices[0],mention.indices[1])) + fnTransform: function(text) {
return "<$link to=\"" + title + "\">" +
$tw.utils.htmlEncode(text.substring(mention.indices[0],mention.indices[1])) +
"</$link>"; "</$link>";
posText = mention.indices[1];
mentions.push(mention.id_str);
});
if(posText < rawText.length) {
text = text + $tw.utils.htmlEncode(rawText.substring(posText));
} }
text = text.replace("\n","<br>"); });
});
// URLs
$tw.utils.each(tweet.tweet.entities.urls,function(urlInfo) {
modifications.push({
startPos: urlInfo.indices[0],
endPos: urlInfo.indices[1],
fnTransform: function(text) {
return "<a href=\"" + $tw.utils.htmlEncode(urlInfo.expanded_url) + "\" rel=\"noopener noreferrer\" target=\"_blank\">" +
$tw.utils.htmlEncode(urlInfo.display_url) +
"</a>";
}
});
});
// Sort the modifications by start position
modifications.sort(function(a,b) {
return a.startPos - b.startPos;
});
// Apply the modifications in reverse order
var rawText = tweet.tweet.full_text,
posText = rawText.length - 1,
chunks = [];
for(var modificationIndex=modifications.length-1; modificationIndex>=0; modificationIndex--) {
var modification = modifications[modificationIndex];
// Process any text after the modification
if(posText > modification.endPos) {
chunks.push($tw.utils.htmlEncode(rawText.substring(modification.endPos,posText)));
}
// Process the modification
chunks.push(modification.fnTransform(rawText));
// Adjust the position
posText = modification.startPos;
}
// Process any remaining text
if(posText > 0) {
chunks.push($tw.utils.htmlEncode(rawText.substring(0,posText)));
}
// Concatenate the chunks and replace newlines with <br>
var text = chunks.reverse().join("").replace("\n","<br>");
// Create the tweet tiddler // Create the tweet tiddler
var tiddler = { var tiddler = {
title: "Tweet - " + tweet.tweet.id_str, title: "Tweet - " + tweet.tweet.id_str,

View File

@ -28,6 +28,7 @@ The Twitter Archivist imports the following tiddlers:
|!Field |!Description | |!Field |!Description |
|''created'' |Tweet creation date (in TiddlyWiki format) | |''created'' |Tweet creation date (in TiddlyWiki format) |
|''favorite_count'' |Number of favourites received by this tweet | |''favorite_count'' |Number of favourites received by this tweet |
|''mention_user_ids'' |Optional list of user IDs mentioned in the tweet |
|''modified'' |Tweet creation date (in TiddlyWiki format) | |''modified'' |Tweet creation date (in TiddlyWiki format) |
|''retweet_count'' |Number of retweets received by this tweet | |''retweet_count'' |Number of retweets received by this tweet |
|''status_id'' |Unique numeric identifier for tweet | |''status_id'' |Unique numeric identifier for tweet |

View File

@ -4,10 +4,11 @@ title: $:/plugins/tiddlywiki/twitter-archivist/todo
* Fixed display of tweet account information * Fixed display of tweet account information
* Data model documentation * Data model documentation
* Expand t.co URLs
!! To Do !! To Do
* Expand t.co URLs * Wikify hashtags
* Import direct messages * Import direct messages
* Control over which media types are imported * Control over which media types are imported
* `_canonical_uri` support for media * `_canonical_uri` support for media