@pipedream/twitter 0.3.9 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,13 @@
1
1
  import twitter from "../../twitter.app.mjs";
2
2
  import fs from "fs";
3
3
  import mime from "mime";
4
+ import FormData from "form-data";
4
5
 
5
6
  export default {
6
7
  key: "twitter-upload-media",
7
8
  name: "Upload Media",
8
9
  description: "Upload images or other media types to Twitter. Returns a `media_id_string` to be used with the Create Tweet action. [See the docs here](https://developer.twitter.com/en/docs/tutorials/uploading-media)",
9
- version: "0.0.1",
10
+ version: "0.0.3",
10
11
  type: "action",
11
12
  props: {
12
13
  twitter,
@@ -23,54 +24,98 @@ export default {
23
24
  return Object.values(mime._types);
24
25
  },
25
26
  },
27
+ mediaCategory: {
28
+ type: "string",
29
+ label: "Media Category",
30
+ description: "The category that represents how the media will be used",
31
+ options: [
32
+ "amplify_video",
33
+ "tweet_gif",
34
+ "tweet_image",
35
+ "tweet_video",
36
+ ],
37
+ },
38
+ },
39
+ methods: {
40
+ async append($, fileSize, mediaId) {
41
+ const bufferLength = 1000000;
42
+ const theBuffer = new Buffer(bufferLength);
43
+ let offset = 0;
44
+ let segment = 0;
45
+ const fd = fs.openSync(this.file, "r");
46
+ while (offset < fileSize) {
47
+ const bytesRead = fs.readSync(fd, theBuffer, 0, bufferLength, null);
48
+ const mediaData = bytesRead < bufferLength
49
+ ? theBuffer.slice(0, bytesRead)
50
+ : theBuffer;
51
+ const data = new FormData();
52
+ data.append("command", "APPEND");
53
+ data.append("media_id", mediaId);
54
+ data.append("media_data", mediaData.toString("base64"));
55
+ data.append("segment_index", segment);
56
+ await this.twitter.uploadMedia({
57
+ data,
58
+ headers: {
59
+ "Content-Type": `multipart/form-data; boundary=${data._boundary}`,
60
+ },
61
+ $,
62
+ });
63
+ segment++;
64
+ offset += bufferLength;
65
+ }
66
+ fs.close(fd);
67
+ },
26
68
  },
27
69
  async run({ $ }) {
28
70
  // INIT
29
- const fileContent = fs.readFileSync(this.file, {
30
- encoding: "base64",
31
- });
32
71
  const fileSize = (fs.statSync(this.file)).size;
72
+ const params = {
73
+ command: "INIT",
74
+ total_bytes: fileSize,
75
+ media_type: this.mediaType,
76
+ additional_owners: this.twitter.$auth.oauth_uid,
77
+ };
78
+ if (this.mediaCategory) {
79
+ params.media_category = this.mediaCategory;
80
+ }
33
81
  const { media_id_string: mediaId } = await this.twitter.uploadMedia({
34
- params: {
35
- command: "INIT",
36
- total_bytes: fileSize,
37
- media_type: this.mediaType,
38
- additional_owners: this.twitter.$auth.oauth_uid,
39
- },
82
+ params,
40
83
  $,
41
84
  });
42
85
 
43
86
  // APPEND
44
- const size = 5012;
45
- const numSegments = fileContent.length / size;
46
- let segment = 0;
47
- let startIndex = 0;
48
- let endIndex = startIndex + size;
49
- while (segment <= numSegments) {
50
- const chunk = fileContent.substring(startIndex, endIndex);
51
- const params = {
52
- command: "APPEND",
53
- media_id: mediaId,
54
- media_data: chunk,
55
- segment_index: segment,
56
- };
57
- await this.twitter.uploadMedia({
58
- params,
59
- $,
60
- });
61
- segment++;
62
- startIndex += size;
63
- endIndex += size;
64
- }
87
+ await this.append($, fileSize, mediaId);
65
88
 
66
89
  // FINALIZE
67
- const response = await this.twitter.uploadMedia({
90
+ let response;
91
+ response = await this.twitter.uploadMedia({
68
92
  params: {
69
93
  command: "FINALIZE",
70
94
  media_id: mediaId,
71
95
  },
72
96
  $,
73
97
  });
98
+
99
+ // STATUS
100
+ while (this.mediaCategory?.includes("video")) {
101
+ response = await this.twitter.uploadMedia({
102
+ params: {
103
+ command: "STATUS",
104
+ media_id: mediaId,
105
+ },
106
+ method: "GET",
107
+ $,
108
+ });
109
+ const { processing_info: info } = response;
110
+ if (info?.error) {
111
+ throw new Error(JSON.stringify(info.error));
112
+ }
113
+ if (info?.state === "succeeded") {
114
+ break;
115
+ }
116
+ setTimeout(() => {}, info.check_after_secs * 1000);
117
+ }
118
+
74
119
  response && $.export("$summary", `Successfully uploaded media with Media ID ${mediaId}`);
75
120
  return response;
76
121
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pipedream/twitter",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "description": "Pipedream Twitter Components",
5
5
  "main": "twitter.app.mjs",
6
6
  "keywords": [
@@ -13,6 +13,7 @@
13
13
  "@pipedream/platform": "^1.2.0",
14
14
  "async-retry": "^1.3.3",
15
15
  "axios": "^0.21.1",
16
+ "form-data": "^4.0.0",
16
17
  "lodash": "^4.17.21",
17
18
  "luxon": "^3.2.0",
18
19
  "mime": "^3.0.0",