kbravh / obsidian-tweet-to-markdown

An Obsidian.md plugin to save tweets as Markdown files.
MIT License
195 stars 12 forks source link

🐛 Bug - twitter handle prepended to profile image location #33

Closed kbravh closed 2 years ago

kbravh commented 2 years ago

Referencing the comment on the tweet-to-markdown repository: https://github.com/kbravh/tweet-to-markdown/issues/9#issuecomment-1213612159


I've updated both ttm and the Obsidian plugin. In Obsidian, the downloaded/saved profile image path is incorrect.

Here are the results. The username is prepending the image path (Twitter/media/).

Tweet URL

---
author: "Vlad Mihalcea"
handle: "@vlad_mihalcea"
source: "https://twitter.com/vlad_mihalcea/status/1549446910527918080"
date: "July 19, 2022 1:31 PM"
fetched: "August 12, 2022 8:19 PM"
likes: 21
retweets: 5
replies: 1
---
![[vlad_mihalceaTwitter/media/2255336726-z_LLR8AX_normal.jpg]]
Vlad Mihalcea ([@vlad_mihalcea](https://twitter.com/vlad_mihalcea)) - July 19, 2022 1:31 PM

How to tunnel localhost to the public Internet

[vladmihalcea.com/tunnel-localho…](https://vladmihalcea.com/tunnel-localhost-public-internet/) [pic.twitter.com/YGAOK6Jifr](https://twitter.com/vlad_mihalcea/status/1549446910527918080/photo/1)

![3_1549446907621154830](Twitter/media/3_1549446907621154830.jpg)

[Tweet link](https://twitter.com/vlad_mihalcea/status/1549446910527918080)

Here are the plugin settings:

{
  "bearerToken": "<TWITTER_BEARER_TOKEN>",
  "noteLocation": "Twitter",
  "downloadAssets": true,
  "imageEmbedStyle": "markdown",
  "assetLocation": "Twitter/media/",
  "filename": "@[[handle]]-[[id]]",
  "tweetCompleteAction": "newTab",
  "tweetLinkFetch": true,
  "embedMethod": "obsidian",
  "frontmatter": true,
  "tags": [],
  "cssclass": "",
  "freeformFrontmatter": [],
  "avatars": true,
  "avatarSize": "",
  "condensedThread": false,
  "textOnly": false,
  "includeImages": true,
  "imageSize": "",
  "includeLinks": true,
  "includeDate": true,
  "dateFormat": "LLL",
  "dateLocale": "en"
}
gapmiss commented 2 years ago

After restarting Obsidian, I am unable to re-create this "bug". I do backup older versions before I upgrade and may have inadvertently merged some custom code. Please mark this as solved and I will comment again if needed. Apologies for the false alarm. While I'm here, let me share some mods I've made to your plugin to facilitate GIFs & other small changes. No expectations, do with it as you see fit.

The animated GIF makes it easy to save demos, memes and such as MP4s. The #round is a CSS class from Minimal Theme to spice up the profile avatar. The vhn-api-3.netlify.app call is for an Open Graph Image API deployed on Netlify (@gapmiss/api-opengraph-image). The "nested" level of tagging could be added as a new setting. I couldn't figure out how to discover the video URLs via Twitter API, so I opted to use the info provided to give a little context.

diff:


2425a2426,2431
> function millisToMinutesAndSeconds(millis) {
>   var minutes = Math.floor(millis / 60000);
>   var seconds = ((millis % 60000) / 1000).toFixed(0);
>   return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
> }
>
3802c3808
<         'media.fields': 'url,alt_text',
---
>         'media.fields': 'duration_ms,height,media_key,preview_image_url,public_metrics,type,width,url,alt_text',
3986a3994,4003
>                 case 'animated_gif':
>                     // var fp = obsidian.normalizePath(`${assetLocation}/${medium.media_key}.jpg`);
>                     const tmp = medium.preview_image_url.replace('https://pbs','https://video').replace('_thumb','').replace('jpg','mp4');
>                     const new_title = tmp.replace('https://video.twimg.com/tweet_video/','');
>                     const fp = obsidian.normalizePath(`${assetLocation}/${new_title}`);
>                     // console.log(tmp);
>                     // console.log(new_title);
>                     return `\n![${(_a = null) !== null && _a !== void 0 ? _a : medium.media_key}](${fp})`;
>                 case 'video':
>                     return `\n![[${(_a = null) !== null && _a !== void 0 ? _a : ''}${filepath}|${medium.width/2}x${medium.height/2}]]\nVideo: ${medium.width}x${medium.height} - ${millisToMinutesAndSeconds(medium.duration_ms)} mins\n`;
4002c4019
<     var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
---
>     var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
4052c4070
<             text = text.replace(new RegExp(`#${tag}`, 'g'), `[#${tag}](https://twitter.com/hashtag/${tag}) `);
---
>             text = text.replace(new RegExp(`#${tag}`, 'g'), `[#TWTTR/${tag}](https://twitter.com/hashtag/${tag}) `);
4055a4074,4076
>             if (!url.expanded_url.match("twitter.com")) {
>               text += `\n\n![](https://vhn-api-3.netlify.app/${encodeURIComponent(url.expanded_url)}/small/)`;
>             }
4109c4129
<                 markdown.push(`![[${filename}${plugin.settings.avatarSize ? `|${plugin.settings.avatarSize}` : ''}]]`);
---
>                 markdown.push(`![[${filename}${plugin.settings.avatarSize ? `|${plugin.settings.avatarSize}` : ''}#round]]`);
4112c4132
<                 markdown.push(`![${user.username}${plugin.settings.avatarSize ? `|${plugin.settings.avatarSize}` : ''}](${plugin.settings.downloadAssets ? filename : user.profile_image_url})` // profile image
---
>                 markdown.push(`![${user.username}${plugin.settings.avatarSize ? `|${plugin.settings.avatarSize}` : ''}](${plugin.settings.downloadAssets ? filename : user.profile_image_url}#round)` // profile image
4205a4227,4247
>                 case 'animated_gif':
>                     var tmp = medium.preview_image_url.replace('https://pbs','https://video').replace('_thumb','').replace('jpg','mp4');
>                     var new_title = tmp.replace('https://video.twimg.com/tweet_video/','');
>                     // console.log(tmp);
>                     // console.log(new_title);
>                     filesToDownload.push({
>                         // https://video.twimg.com/tweet_video/FNK2z3UWQAYJjdj.mp4
>                         // https://pbs.twimg.com/tweet_video_thumb/FNK2z3UWQAYJjdj.jpg
>                         url: tmp,
>                         // title: `${medium.media_key}.mp4`,
>                         title: new_title,
>                     });
>                     break;
>                 case 'video' :
>                     // console.log(medium);
>
>                     filesToDownload.push({
>                         url: medium.preview_image_url,
>                         title: `${medium.media_key}.jpg`,
>                     });
>                     break;

CleanShot – Obsidian - @Reuters-1512471582236585984

CleanShot – Obsidian - @LocalXpose-1539341399908397056-b

CleanShot – Obsidian - @LocalXpose-1539341399908397056-a

kbravh commented 2 years ago

@gapmiss Thanks for reporting back! I was doing some testing, and the only way I could replicate the issue was by setting the image location to [[handle]]Twitter/media, so I'm glad to hear you haven't seen it again.

And thanks so much for sharing your modifications! It all seems to be very clever. I'm going to read through and see what I can implement.