-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New Components - docparser #16191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
New Components - docparser #16191
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
components/docparser/actions/fetch-document-url/fetch-document-url.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import FormData from "form-data"; | ||
| import docparser from "../../docparser.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "docparser-fetch-document-url", | ||
| name: "Fetch Document by URL", | ||
| description: "Fetches a document from a provided URL and imports it to Docparser for parsing. [See the documentation](https://docparser.com/api/)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| docparser, | ||
| parserId: { | ||
| propDefinition: [ | ||
| docparser, | ||
| "parserId", | ||
| ], | ||
| }, | ||
| url: { | ||
| type: "string", | ||
| label: "Document URL", | ||
| description: "The URL of the document to be fetched and imported into Docparser.", | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const data = new FormData(); | ||
| data.append("url", this.url); | ||
|
|
||
| const response = await this.docparser.fetchDocumentFromURL({ | ||
| $, | ||
| parserId: this.parserId, | ||
| data, | ||
| headers: data.getHeaders(), | ||
| }); | ||
|
|
||
| $.export("$summary", `Document is scheduled to be fetched and processed. Document ID: ${response.document_id}`); | ||
| return response; | ||
| }, | ||
| }; | ||
40 changes: 40 additions & 0 deletions
40
components/docparser/actions/upload-document/upload-document.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import FormData from "form-data"; | ||
| import fs from "fs"; | ||
| import { checkTmp } from "../../common/utils.mjs"; | ||
| import docparser from "../../docparser.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "docparser-upload-document", | ||
| name: "Upload Document", | ||
| description: "Uploads a document to docparser that initiates parsing immediately after reception. [See the documentation](https://docparser.com/api/#import-documents)", | ||
| version: "0.0.1", | ||
| type: "action", | ||
| props: { | ||
| docparser, | ||
| parserId: { | ||
| propDefinition: [ | ||
| docparser, | ||
| "parserId", | ||
| ], | ||
| }, | ||
| file: { | ||
| type: "string", | ||
| label: "File", | ||
| description: "The path to a file in the `/tmp` directory. [See the documentation on working with files](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)", | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const data = new FormData(); | ||
| data.append("file", fs.createReadStream(checkTmp(this.file))); | ||
|
|
||
| const response = await this.docparser.uploadDocument({ | ||
| $, | ||
| parserId: this.parserId, | ||
| data, | ||
| headers: data.getHeaders(), | ||
| }); | ||
|
|
||
| $.export("$summary", `Successfully uploaded document. Document ID: ${response.id}`); | ||
| return response; | ||
| }, | ||
jcortes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const checkTmp = (filename) => { | ||
| if (!filename.startsWith("/tmp")) { | ||
| return `/tmp/${filename}`; | ||
| } | ||
| return filename; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,73 @@ | ||
| import { axios } from "@pipedream/platform"; | ||
|
|
||
| export default { | ||
| type: "app", | ||
| app: "docparser", | ||
| propDefinitions: {}, | ||
| propDefinitions: { | ||
| parserId: { | ||
| type: "string", | ||
| label: "Parser ID", | ||
| description: "The ID of the parser to be used.", | ||
| async options() { | ||
| const parsers = await this.listParsers(); | ||
| return parsers.map(({ | ||
| id: value, label, | ||
| }) => ({ | ||
| label, | ||
| value, | ||
| })); | ||
| }, | ||
| }, | ||
| }, | ||
| methods: { | ||
| // this.$auth contains connected account data | ||
| authKeys() { | ||
| console.log(Object.keys(this.$auth)); | ||
| _baseUrl() { | ||
| return "https://api.docparser.com"; | ||
| }, | ||
| _auth() { | ||
| return { | ||
| username: `${this.$auth.api_key}`, | ||
| password: "", | ||
| }; | ||
| }, | ||
| _makeRequest({ | ||
| $ = this, path, ...opts | ||
| }) { | ||
| return axios($, { | ||
| url: this._baseUrl() + path, | ||
| auth: this._auth(), | ||
| ...opts, | ||
| }); | ||
| }, | ||
jcortes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| listData({ | ||
| parserId, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| path: `/v1/results/${parserId}`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| listParsers() { | ||
| return this._makeRequest({ | ||
| path: "/v1/parsers", | ||
| }); | ||
| }, | ||
| fetchDocumentFromURL({ | ||
| parserId, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| method: "POST", | ||
| path: `/v2/document/fetch/${parserId}`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| uploadDocument({ | ||
| parserId, ...opts | ||
| }) { | ||
| return this._makeRequest({ | ||
| method: "POST", | ||
| path: `/v1/document/upload/${parserId}`, | ||
| ...opts, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; | ||
| import app from "../../docparser.app.mjs"; | ||
|
|
||
| export default { | ||
| props: { | ||
| app, | ||
| db: "$.service.db", | ||
| timer: { | ||
| type: "$.interface.timer", | ||
| default: { | ||
| intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, | ||
| }, | ||
| }, | ||
| parserId: { | ||
| propDefinition: [ | ||
| app, | ||
| "parserId", | ||
| ], | ||
| }, | ||
| }, | ||
| methods: { | ||
| _getLastDate() { | ||
| return this.db.get("lastDate") || "1970-01-01T00:00:00"; | ||
| }, | ||
| _setLastDate(lastDate) { | ||
| this.db.set("lastDate", lastDate); | ||
| }, | ||
| async emitEvent(maxResults = false) { | ||
| const lastDate = this._getLastDate(); | ||
| const fn = this.getFunction(); | ||
| const params = { | ||
| sort_by: "parsed_at", | ||
| sort_order: "DESC", | ||
| list: "processed_after", | ||
| date: lastDate, | ||
| }; | ||
|
|
||
| if (maxResults) { | ||
| params.limit = maxResults; | ||
| } | ||
|
|
||
| const response = await fn({ | ||
| parserId: this.parserId, | ||
| params, | ||
| }); | ||
|
|
||
| if (response.length) { | ||
| const dateTime = response[0].processed_at_utc; | ||
| this._setLastDate(dateTime.substring(0, dateTime.length - 6)); | ||
| } | ||
|
|
||
| for (const item of response.reverse()) { | ||
| this.$emit(item, { | ||
| id: item.id, | ||
| summary: this.getSummary(item), | ||
| ts: Date.parse(item.created || new Date()), | ||
| }); | ||
| } | ||
| }, | ||
jcortes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| hooks: { | ||
| async deploy() { | ||
| await this.emitEvent(25); | ||
| }, | ||
| }, | ||
| async run() { | ||
| await this.emitEvent(); | ||
| }, | ||
| }; | ||
22 changes: 22 additions & 0 deletions
22
components/docparser/sources/new-document-data-available/new-document-data-available.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import common from "../common/base.mjs"; | ||
| import sampleEmit from "./test-event.mjs"; | ||
|
|
||
| export default { | ||
| ...common, | ||
| key: "docparser-new-document-data-available", | ||
| name: "New Document Data Available", | ||
| description: "Emit new event every time a document is processed and parsed data is available. [See the documentation](https://docparser.com/api/)", | ||
| version: "0.0.1", | ||
| type: "source", | ||
| dedupe: "unique", | ||
| methods: { | ||
| ...common.methods, | ||
| getFunction() { | ||
| return this.app.listData; | ||
| }, | ||
| getSummary(item) { | ||
| return `New Document Parsed: ${item.file_name}`; | ||
| }, | ||
| }, | ||
| sampleEmit, | ||
| }; |
16 changes: 16 additions & 0 deletions
16
components/docparser/sources/new-document-data-available/test-event.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export default { | ||
| "id": "2b11659f161dcd26694e9403fc430cfd", | ||
| "document_id": "2b11659f161dcd26694e9403fc430cfd", | ||
| "remote_id": "", | ||
| "file_name": "file.pdf", | ||
| "media_link": "https://api.docparser.com/v1/document/media/2b11659f161dcd26694e9403fc430cfd-2b11659f161dcd26694e9403fc430cfd", | ||
| "media_link_original": "https://api.docparser.com/v1/document/media/2b11659f161dcd26694e9403fc430cfd-2b11659f161dcd26694e9403fc430cfd/original", | ||
| "media_link_data": "https://api.docparser.com/v1/document/media/2b11659f161dcd26694e9403fc430cfd-2b11659f161dcd26694e9403fc430cfd/data", | ||
| "page_count": 5, | ||
| "uploaded_at": "2025-04-08T13:32:02+00:00", | ||
| "processed_at": "2025-04-08T13:32:02+00:00", | ||
| "uploaded_at_utc": "2025-04-08T13:32:02+00:00", | ||
| "uploaded_at_user": "2025-04-08T06:32:02+00:00", | ||
| "processed_at_utc": "2025-04-08T13:32:02+00:00", | ||
| "processed_at_user": "2025-04-08T06:32:02+00:00" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.