Skip to content

Commit 4490a8d

Browse files
committed
[Components] typefully #15184
Sources - New Draft Published - New Draft Scheduled Actions - Create Draft - Schedule Draft Next Slot - Schedule Draft
1 parent 02d727c commit 4490a8d

File tree

10 files changed

+189
-343
lines changed

10 files changed

+189
-343
lines changed

components/typefully/actions/create-draft/create-draft.mjs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import typefully from "../../typefully.app.mjs";
2-
import { axios } from "@pipedream/platform";
32

43
export default {
54
key: "typefully-create-draft",
65
name: "Create Draft",
7-
description: "Creates a new draft in Typefully. [See the documentation]()",
8-
version: "0.0.{{ts}}",
6+
description: "Creates a new draft in Typefully. [See the documentation](https://support.typefully.com/en/articles/8718287-typefully-api#h_df59629cbf)",
7+
version: "0.0.1",
98
type: "action",
109
props: {
1110
typefully,
@@ -45,8 +44,18 @@ export default {
4544
},
4645
},
4746
async run({ $ }) {
48-
const draft = await this.typefully.createDraft();
49-
$.export("$summary", `Created draft with ID: ${draft.id} and content: "${this.content}"`);
50-
return draft;
47+
const response = await this.typefully.createDraft({
48+
$,
49+
data: {
50+
content: this.content,
51+
threadify: this.threadify,
52+
share: this.share,
53+
auto_retweet_enabled: this.autoRetweetEnabled,
54+
auto_plug_enabled: this.autoPlugEnabled,
55+
},
56+
});
57+
58+
$.export("$summary", `Created draft with ID: ${response.id}.`);
59+
return response;
5160
},
5261
};

components/typefully/actions/schedule-draft-next-slot/schedule-draft-next-slot.mjs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import typefully from "../../typefully.app.mjs";
2-
import { axios } from "@pipedream/platform";
32

43
export default {
54
key: "typefully-schedule-draft-next-slot",
65
name: "Schedule Draft Next Slot",
7-
description: "Schedules an existing draft for publication in the next available time slot. [See the documentation]()",
8-
version: "0.0.{{ts}}",
6+
description: "Schedules an existing draft for publication in the next available time slot. [See the documentation](https://support.typefully.com/en/articles/8718287-typefully-api#h_df59629cbf)",
7+
version: "0.0.1",
98
type: "action",
109
props: {
1110
typefully,
@@ -45,8 +44,19 @@ export default {
4544
},
4645
},
4746
async run({ $ }) {
48-
const response = await this.typefully.scheduleDraftNextAvailableSlot();
49-
$.export("$summary", "Draft scheduled successfully");
47+
const response = await this.typefully.createDraft({
48+
$,
49+
data: {
50+
"content": this.content,
51+
"threadify": this.threadify,
52+
"share": this.share,
53+
"schedule-date": "next-free-slot",
54+
"auto_retweet_enabled": this.autoRetweetEnabled,
55+
"auto_plug_enabled": this.autoPlugEnabled,
56+
},
57+
});
58+
59+
$.export("$summary", `Draft scheduled successfully with ID: ${response.id}.`);
5060
return response;
5161
},
5262
};
Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,68 @@
11
import typefully from "../../typefully.app.mjs";
2-
import { axios } from "@pipedream/platform";
32

43
export default {
54
key: "typefully-schedule-draft",
65
name: "Schedule Draft",
7-
description: "Schedules a draft for publication at a specific date and time. [See the documentation](https://support.typefully.com/en/articles/8718287-typefully-api)",
8-
version: "0.0.{{ts}}",
6+
description: "Schedules a draft for publication at a specific date and time. [See the documentation](https://support.typefully.com/en/articles/8718287-typefully-api#h_df59629cbf)",
7+
version: "0.0.1",
98
type: "action",
109
props: {
1110
typefully,
1211
content: {
1312
propDefinition: [
14-
"typefully",
13+
typefully,
1514
"content",
1615
],
1716
},
18-
scheduleDate: {
19-
propDefinition: [
20-
"typefully",
21-
"scheduleDate",
22-
],
23-
optional: false,
24-
},
2517
threadify: {
2618
propDefinition: [
27-
"typefully",
19+
typefully,
2820
"threadify",
2921
],
3022
optional: true,
3123
},
3224
share: {
3325
propDefinition: [
34-
"typefully",
26+
typefully,
3527
"share",
3628
],
3729
optional: true,
3830
},
31+
scheduleDate: {
32+
propDefinition: [
33+
typefully,
34+
"scheduleDate",
35+
],
36+
},
3937
autoRetweetEnabled: {
4038
propDefinition: [
41-
"typefully",
39+
typefully,
4240
"autoRetweetEnabled",
4341
],
4442
optional: true,
4543
},
4644
autoPlugEnabled: {
4745
propDefinition: [
48-
"typefully",
46+
typefully,
4947
"autoPlugEnabled",
5048
],
5149
optional: true,
5250
},
5351
},
5452
async run({ $ }) {
55-
const response = await this.typefully.scheduleDraftAtSpecificDate();
56-
$.export("$summary", `Draft scheduled for ${this.scheduleDate}`);
53+
const response = await this.typefully.createDraft({
54+
$,
55+
data: {
56+
"content": this.content,
57+
"threadify": this.threadify,
58+
"share": this.share,
59+
"schedule-date": this.scheduleDate,
60+
"auto_retweet_enabled": this.autoRetweetEnabled,
61+
"auto_plug_enabled": this.autoPlugEnabled,
62+
},
63+
});
64+
65+
$.export("$summary", `Draft scheduled successfully with ID: ${response.id}.`);
5766
return response;
5867
},
5968
};

components/typefully/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/typefully",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Typefully Components",
55
"main": "typefully.app.mjs",
66
"keywords": [
@@ -11,5 +11,8 @@
1111
"author": "Pipedream <[email protected]> (https://pipedream.com/)",
1212
"publishConfig": {
1313
"access": "public"
14+
},
15+
"dependencies": {
16+
"@pipedream/platform": "^3.0.3"
1417
}
1518
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import typefully from "../../typefully.app.mjs";
3+
4+
export default {
5+
props: {
6+
typefully,
7+
db: "$.service.db",
8+
timer: {
9+
type: "$.interface.timer",
10+
default: {
11+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
12+
},
13+
},
14+
},
15+
methods: {
16+
_getLastId() {
17+
return this.db.get("lastId") || 0;
18+
},
19+
_setLastId(lastId) {
20+
this.db.set("lastId", lastId);
21+
},
22+
async emitEvent(maxResults = false) {
23+
const lastId = this._getLastId();
24+
const fn = await this.getFunction();
25+
const response = await fn();
26+
27+
let responseArray = [];
28+
for await (const item of response) {
29+
if (item.id <= lastId) break;
30+
responseArray.push(item);
31+
}
32+
33+
if (responseArray.length) {
34+
if (maxResults && (responseArray.length > maxResults)) {
35+
responseArray.length = maxResults;
36+
}
37+
this._setLastId(responseArray[0].id);
38+
}
39+
40+
for (const item of responseArray.reverse()) {
41+
this.$emit(item, {
42+
id: item.id,
43+
summary: this.getSummary(item),
44+
ts: Date.parse(new Date()),
45+
});
46+
}
47+
},
48+
},
49+
hooks: {
50+
async deploy() {
51+
await this.emitEvent(25);
52+
},
53+
},
54+
async run() {
55+
await this.emitEvent();
56+
},
57+
};
Lines changed: 12 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,22 @@
1-
import {
2-
axios, DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
3-
} from "@pipedream/platform";
4-
import typefully from "../../typefully.app.mjs";
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
53

64
export default {
5+
...common,
76
key: "typefully-new-draft-published",
87
name: "New Draft Published",
9-
description: "Emit new event when a draft is published to Twitter via Typefully. [See the documentation](#)",
10-
version: "0.0.{{ts}}",
8+
description: "Emit new event when a draft is published to Twitter via Typefully.",
9+
version: "0.0.1",
1110
type: "source",
1211
dedupe: "unique",
13-
props: {
14-
typefully,
15-
db: "$.service.db",
16-
timer: {
17-
type: "$.interface.timer",
18-
default: {
19-
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
20-
},
12+
methods: {
13+
...common.methods,
14+
getFunction() {
15+
return this.typefully.getRecentlyPublishedDrafts;
2116
},
22-
kind: {
23-
propDefinition: [
24-
"typefully",
25-
"kind",
26-
],
27-
optional: true,
17+
getSummary(draft) {
18+
return `Draft Published: ${draft.id}`;
2819
},
2920
},
30-
hooks: {
31-
async deploy() {
32-
try {
33-
const drafts = await this.typefully.getRecentlyPublishedDrafts({
34-
params: {
35-
...(this.kind
36-
? {
37-
kind: this.kind,
38-
}
39-
: {}),
40-
},
41-
});
42-
const sortedDrafts = drafts.sort((a, b) => new Date(b.published_at) - new Date(a.published_at));
43-
const draftsToEmit = sortedDrafts.slice(0, 50);
44-
45-
for (const draft of draftsToEmit) {
46-
this.$emit(draft, {
47-
id: draft.id || new Date(draft.published_at).getTime(),
48-
summary: `Draft Published: ${draft.content.substring(0, 50)}...`,
49-
ts: new Date(draft.published_at).getTime(),
50-
});
51-
}
52-
53-
if (draftsToEmit.length > 0) {
54-
const latestTimestamp = new Date(draftsToEmit[0].published_at).getTime();
55-
await this.db.set("last_published_ts", latestTimestamp);
56-
}
57-
} catch (error) {
58-
this.$emit(error, {
59-
summary: "Error during deploy",
60-
ts: Date.now(),
61-
});
62-
}
63-
},
64-
async activate() {
65-
// No webhook subscription needed for polling source
66-
},
67-
async deactivate() {
68-
// No webhook unsubscription needed for polling source
69-
},
70-
},
71-
async run() {
72-
try {
73-
const lastTs = (await this.db.get("last_published_ts")) || 0;
74-
const drafts = await this.typefully.getRecentlyPublishedDrafts({
75-
params: {
76-
...(this.kind
77-
? {
78-
kind: this.kind,
79-
}
80-
: {}),
81-
},
82-
});
83-
84-
const newDrafts = drafts.filter((draft) => new Date(draft.published_at).getTime() > lastTs);
85-
const sortedNewDrafts = newDrafts.sort((a, b) => new Date(a.published_at) - new Date(b.published_at));
86-
87-
for (const draft of sortedNewDrafts) {
88-
this.$emit(draft, {
89-
id: draft.id || new Date(draft.published_at).getTime(),
90-
summary: `Draft Published: ${draft.content.substring(0, 50)}...`,
91-
ts: new Date(draft.published_at).getTime(),
92-
});
93-
}
94-
95-
if (drafts.length > 0) {
96-
const latestDraft = drafts[0];
97-
const latestTs = new Date(latestDraft.published_at).getTime();
98-
await this.db.set("last_published_ts", latestTs);
99-
}
100-
} catch (error) {
101-
this.$emit(error, {
102-
summary: "Error during run",
103-
ts: Date.now(),
104-
});
105-
}
106-
},
21+
sampleEmit,
10722
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default {
2+
"id": 123,
3+
"status": "published",
4+
"html": "content test",
5+
"num_tweets": 1,
6+
"last_edited": null,
7+
"scheduled_date": null,
8+
"published_on": "2025-01-09T10:22:11Z",
9+
"share_url": "https://typefully.com/t/nb9Lxx13df",
10+
"twitter_url": null,
11+
"linkedin_url": null,
12+
"text_first_tweet": "content",
13+
"html_first_tweet": "content",
14+
"text_preview_linkedin": null
15+
}

0 commit comments

Comments
 (0)