diff --git a/components/bitport/actions/add-item/add-item.mjs b/components/bitport/actions/add-item/add-item.mjs new file mode 100644 index 0000000000000..842a1031b0d6f --- /dev/null +++ b/components/bitport/actions/add-item/add-item.mjs @@ -0,0 +1,41 @@ +import app from "../../bitport.app.mjs"; + +export default { + key: "bitport-add-item", + name: "Add Item", + description: "Add new torrent. [See the documentation](https://bitport.io/api/index.html?url=/v2/transfers&method=POST)", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: false, + }, + props: { + app, + torrent: { + type: "string", + label: "Torrent", + description: "A URL linking to a .torrent file or a magnet link", + }, + folderCode: { + propDefinition: [ + app, + "folderCode", + ], + optional: true, + }, + }, + async run({ $ }) { + const response = await this.app.addItem({ + $, + data: { + torrent: this.torrent, + folder_code: this.folderCode, + }, + }); + + $.export("$summary", "Successfully added torrent!"); + return response; + }, +}; diff --git a/components/bitport/actions/search/search.mjs b/components/bitport/actions/search/search.mjs new file mode 100644 index 0000000000000..81a408ca63aae --- /dev/null +++ b/components/bitport/actions/search/search.mjs @@ -0,0 +1,35 @@ +import app from "../../bitport.app.mjs"; + +export default { + key: "bitport-search", + name: "Search", + 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)", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: false, + }, + props: { + app, + query: { + type: "string", + label: "Query", + description: "The query to search for", + }, + }, + async run({ $ }) { + const { data } = await this.app.search({ + $, + query: this.query, + }); + + $.export("$summary", `Successfully found for ${data.folders.length} folder${data.folders.length > 1 + ? "s" + : ""} and ${data.files.length} file${data.files.length > 1 + ? "s" + : ""}`); + return data; + }, +}; diff --git a/components/bitport/bitport.app.mjs b/components/bitport/bitport.app.mjs index d67f960bdc012..24370fb2493df 100644 --- a/components/bitport/bitport.app.mjs +++ b/components/bitport/bitport.app.mjs @@ -1,11 +1,66 @@ +import { axios } from "@pipedream/platform"; +import { prepareList } from "./common/utils.mjs"; + export default { type: "app", app: "bitport", - propDefinitions: {}, + propDefinitions: { + folderCode: { + type: "string", + label: "Folder Code", + description: "The code of the folder to add the item to", + async options() { + const { data } = await this.listFolders(); + + return prepareList({ + items: data, + }).map((item) => ({ + label: item.fullName, + value: item.code, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _apiUrl() { + return "https://api.bitport.io/v2"; + }, + _getHeaders() { + return { + "Authorization": `Bearer ${this.$auth.oauth_access_token}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._apiUrl()}${path}`, + headers: this._getHeaders(), + ...opts, + }); + }, + search({ + query, ...opts + }) { + return this._makeRequest({ + path: `/search/${query}`, + ...opts, + }); + }, + listFolders() { + return this._makeRequest({ + path: "/cloud/byPath", + params: { + scope: "recursive", + }, + }); + }, + addItem(opts = {}) { + return this._makeRequest({ + method: "POST", + path: "/transfers", + ...opts, + }); }, }, -}; \ No newline at end of file +}; diff --git a/components/bitport/common/utils.mjs b/components/bitport/common/utils.mjs new file mode 100644 index 0000000000000..0529e3ba9a0aa --- /dev/null +++ b/components/bitport/common/utils.mjs @@ -0,0 +1,27 @@ +export const prepareList = ({ + items, parentName = "", filesOnly = false, +}) => { + const itemsArray = []; + for (const item of items) { + const fullName = `${parentName}/${item.name}`; + + if (filesOnly) { + itemsArray.push( + ...item.files, + ); + } else { + itemsArray.push({ + fullName, + ...item, + }); + } + + itemsArray.push(...prepareList({ + items: item.folders, + parentName: fullName, + filesOnly, + })); + } + + return itemsArray; +}; diff --git a/components/bitport/package.json b/components/bitport/package.json index ff54ba4d24b36..6adaa04a1cf01 100644 --- a/components/bitport/package.json +++ b/components/bitport/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/bitport", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Bitport Components", "main": "bitport.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^3.1.0" } } diff --git a/components/bitport/sources/common/base.mjs b/components/bitport/sources/common/base.mjs new file mode 100644 index 0000000000000..d0f2bddb223fa --- /dev/null +++ b/components/bitport/sources/common/base.mjs @@ -0,0 +1,86 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import bitport from "../../bitport.app.mjs"; +import { prepareList } from "../../common/utils.mjs"; + +export default { + props: { + bitport, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + folderCode: { + propDefinition: [ + bitport, + "folderCode", + ], + withLabel: true, + }, + }, + methods: { + _getLastDate() { + return this.db.get("lastDate") || 0; + }, + _setLastDate(lastDate) { + this.db.set("lastDate", lastDate); + }, + getFilter() { + return false; + }, + async emitEvent(maxResults = false) { + const lastDate = this._getLastDate(); + + const bufferObj = Buffer.from(this.folderCode.label, "utf8"); + const base64String = bufferObj.toString("base64"); + + const { data: response } = await this.bitport.listFolders({ + maxResults, + params: { + folderPath: base64String, + }, + }); + + let items = prepareList({ + items: response, + filesOnly: true, + }); + + if (items.length) { + items = items.filter((item) => { + return Date.parse(item.created_at.date) > lastDate; + }) + .sort((a, b) => Date.parse(b.created_at.date) - Date.parse(a.created_at.date)); + + const filteredItems = this.getFilter(items); + if (filteredItems) { + items = filteredItems; + } + if (items.length) { + if (maxResults && (items.length > maxResults)) { + items.length = maxResults; + } + this._setLastDate(Date.parse(items[0].created_at.date)); + } + } + + for (const item of items.reverse()) { + this.$emit(item, { + id: item.code, + summary: this.getSummary(item), + ts: Date.parse(item.created_at.date), + }); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, +}; diff --git a/components/bitport/sources/new-file/new-file.mjs b/components/bitport/sources/new-file/new-file.mjs new file mode 100644 index 0000000000000..8bb15b4693c9f --- /dev/null +++ b/components/bitport/sources/new-file/new-file.mjs @@ -0,0 +1,19 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "bitport-new-file", + name: "New File", + 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)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getSummary(item) { + return `New File: ${item.name}`; + }, + }, + sampleEmit, +}; diff --git a/components/bitport/sources/new-file/test-event.mjs b/components/bitport/sources/new-file/test-event.mjs new file mode 100644 index 0000000000000..d752d04849628 --- /dev/null +++ b/components/bitport/sources/new-file/test-event.mjs @@ -0,0 +1,22 @@ +export default { + "name": "Big.Buck.Bunny.4K.UHD.HFR.60fps.FLAC.mkv", + "crc32": null, + "created_at": { + "date": "2016-02-09 15:36:47.000000", + "timezone_type": 3, + "timezone": "UTC" + }, + "code": "0phkm9kpro", + "parent_folder_code": null, + "size": 892862006, + "video": true, + "conversion_status": "unconverted", + "screenshots": { + "small": "https://static.bitport.io/1a338b413f21cbe0_s01.jpg", + "medium": "https://static.bitport.io/1a338b413f21cbe0_m01.jpg", + "big": "https://static.bitport.io/1a338b413f21cbe0_l01.jpg" + }, + "extension": "mkv", + "type": "video", + "virus": false +} \ No newline at end of file diff --git a/components/bitport/sources/new-media/new-media.mjs b/components/bitport/sources/new-media/new-media.mjs new file mode 100644 index 0000000000000..657f5f503653e --- /dev/null +++ b/components/bitport/sources/new-media/new-media.mjs @@ -0,0 +1,27 @@ +import common from "../common/base.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + ...common, + key: "bitport-new-media", + name: "New Media", + 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)", + version: "0.0.1", + type: "source", + dedupe: "unique", + methods: { + ...common.methods, + getFilter(items) { + return items.filter((item) => { + return [ + "video", + "audio", + ].includes(item.type); + }); + }, + getSummary(item) { + return `New Media: ${item.name}`; + }, + }, + sampleEmit, +}; diff --git a/components/bitport/sources/new-media/test-event.mjs b/components/bitport/sources/new-media/test-event.mjs new file mode 100644 index 0000000000000..d752d04849628 --- /dev/null +++ b/components/bitport/sources/new-media/test-event.mjs @@ -0,0 +1,22 @@ +export default { + "name": "Big.Buck.Bunny.4K.UHD.HFR.60fps.FLAC.mkv", + "crc32": null, + "created_at": { + "date": "2016-02-09 15:36:47.000000", + "timezone_type": 3, + "timezone": "UTC" + }, + "code": "0phkm9kpro", + "parent_folder_code": null, + "size": 892862006, + "video": true, + "conversion_status": "unconverted", + "screenshots": { + "small": "https://static.bitport.io/1a338b413f21cbe0_s01.jpg", + "medium": "https://static.bitport.io/1a338b413f21cbe0_m01.jpg", + "big": "https://static.bitport.io/1a338b413f21cbe0_l01.jpg" + }, + "extension": "mkv", + "type": "video", + "virus": false +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2af094a37cb1e..637210f762426 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1718,7 +1718,11 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/bitport: {} + components/bitport: + dependencies: + '@pipedream/platform': + specifier: ^3.1.0 + version: 3.1.0 components/bitquery: {} @@ -1924,8 +1928,7 @@ importers: components/braintree: {} - components/brandblast: - specifiers: {} + components/brandblast: {} components/brandfetch: {}