Skip to content

Commit

Permalink
Images now supported (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored May 28, 2021
1 parent a7605bb commit 8108044
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ This plugin allows you to post tweets directly from Obsidian.
- Automatically appends a tag to your tweet (to keep track of what you've posted)
- **Secure mode** - encrypts your API keys such that they can only be accessed with a password.
- Delete tweet/thread that was just posted (undo)
- Post Tweet Mode
- Post Tweet Mode - a modal dedicated to writing tweets and threads.
- Images are supported - just include a `[[link]]` to your images!


Feel free to recommend features!
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "notetweet",
"name": "NoteTweet🐦",
"version": "0.3.5",
"version": "0.4.0",
"minAppVersion": "0.9.12",
"description": "This plugin allows you to post tweets directly from Obsidian.",
"author": "Christian B. B. Houmann",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "notetweet",
"version": "0.3.5",
"version": "0.4.0",
"description": "Post tweets from Obsidian",
"main": "src/main.js",
"scripts": {
Expand Down
41 changes: 38 additions & 3 deletions src/TwitterHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { StatusesUpdate, TwitterClient } from "twitter-api-client";
import NoteTweet from "./main";

export class TwitterHandler {
private twitterClient: TwitterClient;
public isConnectedToTwitter = false;

constructor(private plugin: NoteTweet) {
}

public connectToTwitter(
apiKey: string,
apiSecret: string,
Expand All @@ -27,11 +31,13 @@ export class TwitterHandler {
let postedTweets: StatusesUpdate[] = [];
let previousPost: StatusesUpdate;

for (const tweet of threadContent) {
let isFirstTweet = threadContent.indexOf(tweet) == 0;
for (const threadTweet of threadContent) {
let isFirstTweet = threadContent.indexOf(threadTweet) == 0;
const {tweet, media_ids} = await this.getImagesInTweet(threadTweet);

previousPost = await this.twitterClient.tweets.statusesUpdate({
status: tweet.trim(),
media_ids,
...(!isFirstTweet && { in_reply_to_status_id: previousPost.id_str }),
});

Expand All @@ -41,12 +47,41 @@ export class TwitterHandler {
return postedTweets;
}

IMAGE_REGEX: RegExp = new RegExp(/!?\[\[([a-zA-Z 0-9-\.]*\.(gif|jpe?g|tiff?|png|webp|bmp))\]\]/);
public async postTweet(tweet: string) {
const {tweet: processedTweet, media_ids} = await this.getImagesInTweet(tweet);

return await this.twitterClient.tweets.statusesUpdate({
status: tweet.trim(),
status: processedTweet.trim(),
media_ids
});
}

private async getImagesInTweet(tweet: string): Promise<{ tweet: string, media_ids: string }> {
let media_ids: string[] = [];
let processedTweet = tweet;

while (this.IMAGE_REGEX.test(processedTweet)) {
const match = this.IMAGE_REGEX.exec(processedTweet);
const fileName: string = match[1];

// Link in [[...]] might not be the actual path because of the attachment folder.
const file = this.plugin.app.vault.getFiles().find(f => f.name === fileName);
const fullPath = (await this.plugin.app.vault.readBinary(file));

const media_data = Buffer.from(fullPath).toString('base64');

const media_id = (await this.twitterClient.media.mediaUpload({media_data, media_category: "tweet_image"}));

if (media_id) {
media_ids.push(media_id.media_id_string);
processedTweet = processedTweet.replace(this.IMAGE_REGEX, "");
}
}

return {tweet: processedTweet, media_ids: media_ids.join(",")}
}

public async deleteTweets(tweets: StatusesUpdate[]) {
try {
for (const tweet of tweets)
Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class NoteTweet extends Plugin {
console.log(WELCOME_MESSAGE);

await this.loadSettings();
this.twitterHandler = new TwitterHandler();
this.twitterHandler = new TwitterHandler(this);
this.connectToTwitterWithPlainSettings();

this.addCommand({
Expand Down Expand Up @@ -87,6 +87,10 @@ export default class NoteTweet extends Plugin {
});
/*END.DEVCMD*/

this.registerObsidianProtocolHandler("notetweet", params => {
console.log(params);
})

this.addSettingTab(new NoteTweetSettingsTab(this.app, this));
}

Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"0.3.3": "0.9.12",
"0.3.4": "0.12.0",
"0.3.5": "0.12.3"
"0.4.0": "0.12.3"
}

0 comments on commit 8108044

Please sign in to comment.