|
| 1 | +import { axios } from "@pipedream/platform"; |
| 2 | +import { |
| 3 | + PATH_PLATFORMS, PLATFORMS, URL_PLATFORMS, |
| 4 | +} from "./common/constants.mjs"; |
| 5 | + |
1 | 6 | export default { |
2 | 7 | type: "app", |
3 | 8 | app: "scrapecreators", |
4 | | - propDefinitions: {}, |
| 9 | + propDefinitions: { |
| 10 | + platform: { |
| 11 | + type: "string", |
| 12 | + label: "Platform", |
| 13 | + description: "The platform to search for creators on", |
| 14 | + options: PLATFORMS, |
| 15 | + }, |
| 16 | + profileId: { |
| 17 | + type: "string", |
| 18 | + label: "Profile", |
| 19 | + description: "The handle, URL or unique ID of the creator profile", |
| 20 | + }, |
| 21 | + }, |
5 | 22 | methods: { |
6 | | - // this.$auth contains connected account data |
7 | | - authKeys() { |
8 | | - console.log(Object.keys(this.$auth)); |
| 23 | + _baseUrl() { |
| 24 | + return "https://api.scrapecreators.com/v1"; |
| 25 | + }, |
| 26 | + _headers() { |
| 27 | + return { |
| 28 | + "x-api-key": `${this.$auth.api_key}`, |
| 29 | + }; |
| 30 | + }, |
| 31 | + _makeRequest({ |
| 32 | + $ = this, path, ...opts |
| 33 | + }) { |
| 34 | + return axios($, { |
| 35 | + url: this._baseUrl() + path, |
| 36 | + headers: this._headers(), |
| 37 | + ...opts, |
| 38 | + }); |
| 39 | + }, |
| 40 | + searchCreators({ |
| 41 | + platform, ...opts |
| 42 | + }) { |
| 43 | + return this._makeRequest({ |
| 44 | + path: `/${platform}/search/users`, |
| 45 | + ...opts, |
| 46 | + }); |
| 47 | + }, |
| 48 | + fetchCreatorProfile({ |
| 49 | + platform, profileId, ...opts |
| 50 | + }) { |
| 51 | + const params = this.parseParams(platform, profileId); |
| 52 | + const path = this.parsePath(platform); |
| 53 | + |
| 54 | + return this._makeRequest({ |
| 55 | + path: `/${platform}${path}`, |
| 56 | + params, |
| 57 | + ...opts, |
| 58 | + }); |
| 59 | + }, |
| 60 | + parseParams(platform, profileId) { |
| 61 | + const params = {}; |
| 62 | + const urlPlatforms = URL_PLATFORMS; |
| 63 | + |
| 64 | + if (urlPlatforms.includes(platform)) { |
| 65 | + params.url = profileId; |
| 66 | + } else { |
| 67 | + params.handle = profileId; |
| 68 | + } |
| 69 | + return params; |
| 70 | + }, |
| 71 | + parsePath(platform) { |
| 72 | + const path = Object.entries(PATH_PLATFORMS).find(([ |
| 73 | + key, |
| 74 | + value, |
| 75 | + ]) => value.includes(platform) |
| 76 | + ? key |
| 77 | + : null); |
| 78 | + |
| 79 | + return path |
| 80 | + ? path[0] === "empty" |
| 81 | + ? "" |
| 82 | + : `/${path[0]}` |
| 83 | + : "/profile"; |
| 84 | + }, |
| 85 | + async *paginate({ |
| 86 | + fn, params = {}, platform, maxResults = null, ...opts |
| 87 | + }) { |
| 88 | + let hasMore = false; |
| 89 | + let count = 0; |
| 90 | + let newCursor; |
| 91 | + |
| 92 | + do { |
| 93 | + params.cursor = newCursor; |
| 94 | + const { |
| 95 | + cursor, |
| 96 | + users, |
| 97 | + } = await fn({ |
| 98 | + platform, |
| 99 | + params, |
| 100 | + ...opts, |
| 101 | + }); |
| 102 | + for (const d of users) { |
| 103 | + yield d; |
| 104 | + |
| 105 | + if (maxResults && ++count === maxResults) { |
| 106 | + return count; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + newCursor = cursor; |
| 111 | + hasMore = users.length; |
| 112 | + |
| 113 | + } while (hasMore); |
9 | 114 | }, |
10 | 115 | }, |
11 | 116 | }; |
0 commit comments