Skip to content

Commit 6b059d8

Browse files
committed
alttextify init
1 parent ceea1c8 commit 6b059d8

File tree

8 files changed

+494
-4
lines changed

8 files changed

+494
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import alttextify from "../../alttextify.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "alttextify-delete-image",
6+
name: "Delete Image Alt Text",
7+
description: "Delete the generated alt text for a specific image using the asset ID. [See the documentation](https://apidoc.alttextify.net/)",
8+
version: "0.0.{{ts}}",
9+
type: "action",
10+
props: {
11+
alttextify,
12+
assetId: {
13+
propDefinition: [
14+
alttextify,
15+
"assetId",
16+
],
17+
},
18+
},
19+
async run({ $ }) {
20+
const response = await this.alttextify.deleteAltTextByAssetId({
21+
assetId: this.assetId,
22+
});
23+
24+
$.export("$summary", `Successfully deleted alt text for asset ID: ${this.assetId}`);
25+
return response;
26+
},
27+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import alttextify from "../../alttextify.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "alttextify-get-alttext",
6+
name: "Retrieve Alt Text",
7+
description: "Retrieve alt text for a previously submitted image using the asset ID or job ID. [See the documentation](https://apidoc.alttextify.net/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
alttextify,
12+
jobId: {
13+
propDefinition: [
14+
alttextify,
15+
"jobId",
16+
],
17+
optional: true,
18+
},
19+
assetId: {
20+
propDefinition: [
21+
alttextify,
22+
"assetId",
23+
],
24+
optional: true,
25+
},
26+
},
27+
async run({ $ }) {
28+
const {
29+
jobId, assetId,
30+
} = this;
31+
let response;
32+
33+
if (jobId) {
34+
response = await this.alttextify.retrieveAltTextByJobId({
35+
jobId,
36+
});
37+
$.export("$summary", `Successfully retrieved alt text by Job ID: ${jobId}`);
38+
} else if (assetId) {
39+
response = await this.alttextify.retrieveAltTextByAssetId({
40+
assetId,
41+
});
42+
$.export("$summary", `Successfully retrieved alt text by Asset ID: ${assetId}`);
43+
} else {
44+
throw new Error("Either Job ID or Asset ID must be provided.");
45+
}
46+
47+
return response;
48+
},
49+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import alttextify from "../../alttextify.app.mjs";
2+
import { axios } from "@pipedream/platform";
3+
4+
export default {
5+
key: "alttextify-submit-image",
6+
name: "Submit Image to Alttextify",
7+
description: "Upload or submit an image URL to Alttextify for alt text generation. [See the documentation](https://apidoc.alttextify.net/)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
alttextify,
12+
imageUrl: {
13+
propDefinition: [
14+
alttextify,
15+
"imageUrl",
16+
],
17+
},
18+
imageType: {
19+
propDefinition: [
20+
alttextify,
21+
"imageType",
22+
],
23+
},
24+
lang: {
25+
propDefinition: [
26+
alttextify,
27+
"lang",
28+
],
29+
optional: true,
30+
},
31+
maxChars: {
32+
propDefinition: [
33+
alttextify,
34+
"maxChars",
35+
],
36+
optional: true,
37+
},
38+
asyncOption: {
39+
propDefinition: [
40+
alttextify,
41+
"asyncOption",
42+
],
43+
optional: true,
44+
},
45+
},
46+
async run({ $ }) {
47+
const response = await this.alttextify.uploadImageUrl({
48+
imageUrl: this.imageUrl,
49+
imageType: this.imageType,
50+
lang: this.lang,
51+
maxChars: this.maxChars,
52+
asyncOption: this.asyncOption,
53+
});
54+
55+
$.export("$summary", `Successfully submitted image to Alttextify for alt text generation with Asset ID: ${response.asset_id}`);
56+
return response;
57+
},
58+
};
Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,146 @@
1+
import { axios } from "@pipedream/platform";
2+
13
export default {
24
type: "app",
35
app: "alttextify",
4-
propDefinitions: {},
6+
propDefinitions: {
7+
imageUrl: {
8+
type: "string",
9+
label: "Image URL",
10+
description: "The URL of the image to process for alt text generation",
11+
},
12+
imageType: {
13+
type: "string",
14+
label: "Image Type",
15+
description: "The type of the image (e.g., 'raw', 'url')",
16+
options: [
17+
"raw",
18+
"url",
19+
],
20+
},
21+
jobId: {
22+
type: "string",
23+
label: "Job ID",
24+
description: "The ID of the job for generating or retrieving alt text",
25+
},
26+
assetId: {
27+
type: "string",
28+
label: "Asset ID",
29+
description: "The ID of the asset for retrieving or deleting alt text.",
30+
},
31+
lang: {
32+
type: "string",
33+
label: "Language",
34+
description: "The language code for the generated alt text.",
35+
},
36+
maxChars: {
37+
type: "integer",
38+
label: "Max Characters",
39+
description: "The maximum length of the generated alt text.",
40+
},
41+
asyncOption: {
42+
type: "boolean",
43+
label: "Async",
44+
description: "Determine whether to process the image asynchronously.",
45+
default: false,
46+
},
47+
imageSubmissionId: {
48+
type: "string",
49+
label: "Image Submission ID",
50+
description: "The ID of the image submission for monitoring events.",
51+
},
52+
},
553
methods: {
6-
// this.$auth contains connected account data
754
authKeys() {
855
console.log(Object.keys(this.$auth));
956
},
57+
_baseUrl() {
58+
return "https://api.alttextify.net/api/v1";
59+
},
60+
async _makeRequest(opts = {}) {
61+
const {
62+
$ = this,
63+
method = "GET",
64+
path = "/",
65+
headers,
66+
...otherOpts
67+
} = opts;
68+
return axios($, {
69+
...otherOpts,
70+
method,
71+
url: this._baseUrl() + path,
72+
headers: {
73+
...headers,
74+
"Authorization": `Bearer ${this.$auth.api_key}`,
75+
"Content-Type": "application/json",
76+
},
77+
});
78+
},
79+
async uploadImageUrl({
80+
imageUrl, imageType, lang, maxChars, asyncOption, ...opts
81+
}) {
82+
return this._makeRequest({
83+
method: "POST",
84+
path: `/image/${imageType}`,
85+
data: {
86+
image: imageUrl,
87+
lang,
88+
max_chars: maxChars,
89+
async: asyncOption,
90+
},
91+
...opts,
92+
});
93+
},
94+
async retrieveAltTextByJobId({
95+
jobId, ...opts
96+
}) {
97+
return this._makeRequest({
98+
path: `/image/job/${jobId}`,
99+
...opts,
100+
});
101+
},
102+
async retrieveAltTextByAssetId({
103+
assetId, ...opts
104+
}) {
105+
return this._makeRequest({
106+
path: `/image/${assetId}`,
107+
...opts,
108+
});
109+
},
110+
async deleteAltTextByAssetId({
111+
assetId, ...opts
112+
}) {
113+
return this._makeRequest({
114+
method: "DELETE",
115+
path: `/image/${assetId}`,
116+
...opts,
117+
});
118+
},
119+
async paginate(fn, ...opts) {
120+
let results = [];
121+
let hasMore = true;
122+
let page = 1;
123+
124+
while (hasMore) {
125+
const response = await fn({
126+
params: {
127+
page,
128+
...opts,
129+
},
130+
});
131+
results = results.concat(response);
132+
hasMore = response.length > 0; // Assume if response has 0 items, it's the end
133+
page++;
134+
}
135+
return results;
136+
},
137+
async emitNewAltTextGeneratedEvent(imageSubmissionId) {
138+
// Example method to handle emitting events when new alt text is generated
139+
// This method would be used in an event source component
140+
},
141+
async emitImageProcessingFailedEvent(imageSubmissionId) {
142+
// Example method to handle emitting events when image processing fails
143+
// This method would be used in an event source component
144+
},
10145
},
11-
};
146+
};

components/alttextify/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"publishConfig": {
1313
"access": "public"
1414
}
15-
}
15+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { axios } from "@pipedream/platform";
2+
import alttextify from "../../alttextify.app.mjs";
3+
4+
export default {
5+
key: "alttextify-new-alttext-generated",
6+
name: "New Alt Text Generated",
7+
description: "Emits an event when new alt text is generated for an image. [See the documentation](https://apidoc.alttextify.net/)",
8+
version: "0.0.1",
9+
type: "source",
10+
dedupe: "unique",
11+
props: {
12+
alttextify,
13+
db: "$.service.db",
14+
timer: {
15+
type: "$.interface.timer",
16+
default: {
17+
intervalSeconds: 3600, // Poll every hour
18+
},
19+
},
20+
imageSubmissionId: {
21+
propDefinition: [
22+
alttextify,
23+
"imageSubmissionId",
24+
],
25+
optional: true,
26+
},
27+
imageType: {
28+
propDefinition: [
29+
alttextify,
30+
"imageType",
31+
],
32+
optional: true,
33+
},
34+
},
35+
methods: {
36+
_getLastProcessedDate() {
37+
return this.db.get("lastProcessedDate") || new Date(0).toISOString();
38+
},
39+
_setLastProcessedDate(date) {
40+
this.db.set("lastProcessedDate", date);
41+
},
42+
async _getNewAltTexts() {
43+
const lastProcessedDate = this._getLastProcessedDate();
44+
const opts = {
45+
params: {
46+
after: lastProcessedDate,
47+
},
48+
};
49+
if (this.imageSubmissionId) {
50+
opts.params.imageSubmissionId = this.imageSubmissionId;
51+
}
52+
const altTexts = await this.alttextify.paginate(
53+
(opts) => this.alttextify.retrieveAltTextByJobId(opts),
54+
opts,
55+
);
56+
return altTexts;
57+
},
58+
},
59+
hooks: {
60+
async deploy() {
61+
const altTexts = await this._getNewAltTexts();
62+
altTexts.slice(0, 50).forEach((altText) => {
63+
this.$emit(altText, {
64+
id: altText.asset_id,
65+
summary: `New alt text generated for asset ${altText.asset_id}`,
66+
ts: new Date(altText.created_at).getTime(),
67+
});
68+
});
69+
if (altTexts.length) {
70+
const lastProcessedAltText = altTexts[0];
71+
this._setLastProcessedDate(lastProcessedAltText.created_at);
72+
}
73+
},
74+
},
75+
async run() {
76+
const altTexts = await this._getNewAltTexts();
77+
altTexts.forEach((altText) => {
78+
this.$emit(altText, {
79+
id: altText.asset_id,
80+
summary: `New alt text generated for asset ${altText.asset_id}`,
81+
ts: new Date(altText.created_at).getTime(),
82+
});
83+
});
84+
if (altTexts.length) {
85+
const lastProcessedAltText = altTexts[0];
86+
this._setLastProcessedDate(lastProcessedAltText.created_at);
87+
}
88+
},
89+
};

0 commit comments

Comments
 (0)