Skip to content

Commit d6385d7

Browse files
authored
Merging pull request #18700
* Enhance Bitport component with new actions and utilities - Introduced `Add Item` action for adding torrents with optional folder selection. - Added `Search` action to find files and folders in the cloud. - Implemented utility function `prepareList` for organizing folder and file data. - Updated `bitport.app.mjs` to include new prop definitions and methods for folder management. - Created new sources for detecting new files and media, enhancing event-driven capabilities. - Bumped package version to 0.1.0 and added necessary dependencies. * pnpm update * Refactor Bitport component and enhance functionality - Removed the `foldersOnly` option from the `prepareList` function. - Deleted the `listFiles` method to streamline file management. - Updated the `listFolders` method call for improved folder handling. - Added `annotations` to `Add Item` and `Search` actions for better metadata. - Adjusted timestamp parsing to ensure consistency across data handling.
1 parent 4bb9102 commit d6385d7

File tree

11 files changed

+348
-7
lines changed

11 files changed

+348
-7
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import app from "../../bitport.app.mjs";
2+
3+
export default {
4+
key: "bitport-add-item",
5+
name: "Add Item",
6+
description: "Add new torrent. [See the documentation](https://bitport.io/api/index.html?url=/v2/transfers&method=POST)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: false,
13+
},
14+
props: {
15+
app,
16+
torrent: {
17+
type: "string",
18+
label: "Torrent",
19+
description: "A URL linking to a .torrent file or a magnet link",
20+
},
21+
folderCode: {
22+
propDefinition: [
23+
app,
24+
"folderCode",
25+
],
26+
optional: true,
27+
},
28+
},
29+
async run({ $ }) {
30+
const response = await this.app.addItem({
31+
$,
32+
data: {
33+
torrent: this.torrent,
34+
folder_code: this.folderCode,
35+
},
36+
});
37+
38+
$.export("$summary", "Successfully added torrent!");
39+
return response;
40+
},
41+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import app from "../../bitport.app.mjs";
2+
3+
export default {
4+
key: "bitport-search",
5+
name: "Search",
6+
description: "Searches folders and files in the cloud and sorts them by name. [See the documentation](https://bitport.io/api/index.html?url=/v2/search/%3Cterm%3E)",
7+
version: "0.0.1",
8+
type: "action",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: false,
13+
},
14+
props: {
15+
app,
16+
query: {
17+
type: "string",
18+
label: "Query",
19+
description: "The query to search for",
20+
},
21+
},
22+
async run({ $ }) {
23+
const { data } = await this.app.search({
24+
$,
25+
query: this.query,
26+
});
27+
28+
$.export("$summary", `Successfully found for ${data.folders.length} folder${data.folders.length > 1
29+
? "s"
30+
: ""} and ${data.files.length} file${data.files.length > 1
31+
? "s"
32+
: ""}`);
33+
return data;
34+
},
35+
};

components/bitport/bitport.app.mjs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1+
import { axios } from "@pipedream/platform";
2+
import { prepareList } from "./common/utils.mjs";
3+
14
export default {
25
type: "app",
36
app: "bitport",
4-
propDefinitions: {},
7+
propDefinitions: {
8+
folderCode: {
9+
type: "string",
10+
label: "Folder Code",
11+
description: "The code of the folder to add the item to",
12+
async options() {
13+
const { data } = await this.listFolders();
14+
15+
return prepareList({
16+
items: data,
17+
}).map((item) => ({
18+
label: item.fullName,
19+
value: item.code,
20+
}));
21+
},
22+
},
23+
},
524
methods: {
6-
// this.$auth contains connected account data
7-
authKeys() {
8-
console.log(Object.keys(this.$auth));
25+
_apiUrl() {
26+
return "https://api.bitport.io/v2";
27+
},
28+
_getHeaders() {
29+
return {
30+
"Authorization": `Bearer ${this.$auth.oauth_access_token}`,
31+
};
32+
},
33+
_makeRequest({
34+
$ = this, path, ...opts
35+
}) {
36+
return axios($, {
37+
url: `${this._apiUrl()}${path}`,
38+
headers: this._getHeaders(),
39+
...opts,
40+
});
41+
},
42+
search({
43+
query, ...opts
44+
}) {
45+
return this._makeRequest({
46+
path: `/search/${query}`,
47+
...opts,
48+
});
49+
},
50+
listFolders() {
51+
return this._makeRequest({
52+
path: "/cloud/byPath",
53+
params: {
54+
scope: "recursive",
55+
},
56+
});
57+
},
58+
addItem(opts = {}) {
59+
return this._makeRequest({
60+
method: "POST",
61+
path: "/transfers",
62+
...opts,
63+
});
964
},
1065
},
11-
};
66+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export const prepareList = ({
2+
items, parentName = "", filesOnly = false,
3+
}) => {
4+
const itemsArray = [];
5+
for (const item of items) {
6+
const fullName = `${parentName}/${item.name}`;
7+
8+
if (filesOnly) {
9+
itemsArray.push(
10+
...item.files,
11+
);
12+
} else {
13+
itemsArray.push({
14+
fullName,
15+
...item,
16+
});
17+
}
18+
19+
itemsArray.push(...prepareList({
20+
items: item.folders,
21+
parentName: fullName,
22+
filesOnly,
23+
}));
24+
}
25+
26+
return itemsArray;
27+
};

components/bitport/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/bitport",
3-
"version": "0.0.1",
3+
"version": "0.1.0",
44
"description": "Pipedream Bitport Components",
55
"main": "bitport.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.1.0"
1417
}
1518
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform";
2+
import bitport from "../../bitport.app.mjs";
3+
import { prepareList } from "../../common/utils.mjs";
4+
5+
export default {
6+
props: {
7+
bitport,
8+
db: "$.service.db",
9+
timer: {
10+
type: "$.interface.timer",
11+
default: {
12+
intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL,
13+
},
14+
},
15+
folderCode: {
16+
propDefinition: [
17+
bitport,
18+
"folderCode",
19+
],
20+
withLabel: true,
21+
},
22+
},
23+
methods: {
24+
_getLastDate() {
25+
return this.db.get("lastDate") || 0;
26+
},
27+
_setLastDate(lastDate) {
28+
this.db.set("lastDate", lastDate);
29+
},
30+
getFilter() {
31+
return false;
32+
},
33+
async emitEvent(maxResults = false) {
34+
const lastDate = this._getLastDate();
35+
36+
const bufferObj = Buffer.from(this.folderCode.label, "utf8");
37+
const base64String = bufferObj.toString("base64");
38+
39+
const { data: response } = await this.bitport.listFolders({
40+
maxResults,
41+
params: {
42+
folderPath: base64String,
43+
},
44+
});
45+
46+
let items = prepareList({
47+
items: response,
48+
filesOnly: true,
49+
});
50+
51+
if (items.length) {
52+
items = items.filter((item) => {
53+
return Date.parse(item.created_at.date) > lastDate;
54+
})
55+
.sort((a, b) => Date.parse(b.created_at.date) - Date.parse(a.created_at.date));
56+
57+
const filteredItems = this.getFilter(items);
58+
if (filteredItems) {
59+
items = filteredItems;
60+
}
61+
if (items.length) {
62+
if (maxResults && (items.length > maxResults)) {
63+
items.length = maxResults;
64+
}
65+
this._setLastDate(Date.parse(items[0].created_at.date));
66+
}
67+
}
68+
69+
for (const item of items.reverse()) {
70+
this.$emit(item, {
71+
id: item.code,
72+
summary: this.getSummary(item),
73+
ts: Date.parse(item.created_at.date),
74+
});
75+
}
76+
},
77+
},
78+
hooks: {
79+
async deploy() {
80+
await this.emitEvent(25);
81+
},
82+
},
83+
async run() {
84+
await this.emitEvent();
85+
},
86+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "bitport-new-file",
7+
name: "New File",
8+
description: "Emit new event when a new file is added to a project in Bitport. [See the documentation](https://bitport.io/api/index.html?url=/v2/cloud/byPath)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getSummary(item) {
15+
return `New File: ${item.name}`;
16+
},
17+
},
18+
sampleEmit,
19+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default {
2+
"name": "Big.Buck.Bunny.4K.UHD.HFR.60fps.FLAC.mkv",
3+
"crc32": null,
4+
"created_at": {
5+
"date": "2016-02-09 15:36:47.000000",
6+
"timezone_type": 3,
7+
"timezone": "UTC"
8+
},
9+
"code": "0phkm9kpro",
10+
"parent_folder_code": null,
11+
"size": 892862006,
12+
"video": true,
13+
"conversion_status": "unconverted",
14+
"screenshots": {
15+
"small": "https://static.bitport.io/1a338b413f21cbe0_s01.jpg",
16+
"medium": "https://static.bitport.io/1a338b413f21cbe0_m01.jpg",
17+
"big": "https://static.bitport.io/1a338b413f21cbe0_l01.jpg"
18+
},
19+
"extension": "mkv",
20+
"type": "video",
21+
"virus": false
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import common from "../common/base.mjs";
2+
import sampleEmit from "./test-event.mjs";
3+
4+
export default {
5+
...common,
6+
key: "bitport-new-media",
7+
name: "New Media",
8+
description: "Emit new event when a new media is added to a project in Bitport. [See the documentation](https://bitport.io/api/index.html?url=/v2/cloud/byPath)",
9+
version: "0.0.1",
10+
type: "source",
11+
dedupe: "unique",
12+
methods: {
13+
...common.methods,
14+
getFilter(items) {
15+
return items.filter((item) => {
16+
return [
17+
"video",
18+
"audio",
19+
].includes(item.type);
20+
});
21+
},
22+
getSummary(item) {
23+
return `New Media: ${item.name}`;
24+
},
25+
},
26+
sampleEmit,
27+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default {
2+
"name": "Big.Buck.Bunny.4K.UHD.HFR.60fps.FLAC.mkv",
3+
"crc32": null,
4+
"created_at": {
5+
"date": "2016-02-09 15:36:47.000000",
6+
"timezone_type": 3,
7+
"timezone": "UTC"
8+
},
9+
"code": "0phkm9kpro",
10+
"parent_folder_code": null,
11+
"size": 892862006,
12+
"video": true,
13+
"conversion_status": "unconverted",
14+
"screenshots": {
15+
"small": "https://static.bitport.io/1a338b413f21cbe0_s01.jpg",
16+
"medium": "https://static.bitport.io/1a338b413f21cbe0_m01.jpg",
17+
"big": "https://static.bitport.io/1a338b413f21cbe0_l01.jpg"
18+
},
19+
"extension": "mkv",
20+
"type": "video",
21+
"virus": false
22+
}

0 commit comments

Comments
 (0)