|
1 | 1 | import requests |
2 | | -import urllib.parse |
3 | 2 | import json |
| 3 | +import re |
| 4 | +import traceback |
| 5 | +import urllib.parse |
4 | 6 |
|
5 | 7 | def define_env(env): |
6 | 8 |
|
7 | | - @env.macro |
| 9 | + @env.macro |
| 10 | + def templatesWidget(title, page): |
| 11 | + node_for_template = title.replace(' ', '+') |
| 12 | + getLastBitOfUrl = re.search("(\.)(.*)(\/)$", page.abs_url) |
| 13 | + node_for_integrations_slug = getLastBitOfUrl.group(2) |
| 14 | + response = requests.get(url = f'https://api.n8n.io/api/templates/search?rows=3&search=&category=&apps={node_for_template}&page=1&sort=views:desc') |
| 15 | + data = response.json() |
| 16 | + # not all nodes have three templates |
| 17 | + try: |
| 18 | + workflows = data["workflows"][:3] |
| 19 | + workflow_one, workflow_two, workflow_three = workflows |
| 20 | + except: |
| 21 | + return f'<span class="n8n-templates-widget-more"><a href="https://n8n.io/integrations/{node_for_integrations_slug}/" target="_blank">Browse {title} integration templates</a>, or <a href="https://n8n.io/workflows/" target="_blank">search all templates</a></span>' |
| 22 | + # the data is not trustworthy |
| 23 | + try: |
| 24 | + workflow_one_title = workflow_one["name"] |
| 25 | + workflow_one_user = workflow_one["user"]["name"] |
| 26 | + workflow_one_url = f'https://n8n.io/workflows/{workflow_one["id"]}-{workflow_one["name"].lower().replace(" ", "-").replace(":", "")}/' |
| 27 | + workflow_two_title = workflow_two["name"] |
| 28 | + workflow_two_user = workflow_two["user"]["name"] |
| 29 | + workflow_two_url = f'https://n8n.io/workflows/{workflow_two["id"]}-{workflow_two["name"].lower().replace(" ", "-").replace(":", "")}/' |
| 30 | + workflow_three_title = workflow_three["name"] |
| 31 | + workflow_three_url = f'https://n8n.io/workflows/{workflow_three["id"]}-{workflow_three["name"].lower().replace(" ", "-").replace(":", "")}/' |
| 32 | + workflow_three_user = workflow_three["user"]["name"] |
| 33 | + except: |
| 34 | + return f'<span class="n8n-templates-widget-more"><a href="https://n8n.io/integrations/{node_for_integrations_slug}/" target="_blank">Browse all {title} integration templates</a>, or <a href="https://n8n.io/workflows/" target="_blank">search all templates</a></span>' |
| 35 | + |
| 36 | + return f'<div class="n8n-templates-widget"><div class="n8n-templates-widget-template"><strong>{workflow_one_title}</strong><p class="n8n-templates-name">by {workflow_one_user}</p><a class="n8n-templates-link" href="{workflow_one_url}" target="_blank">View template details</a></div><div class="n8n-templates-widget-template"><strong>{workflow_two_title}</strong><p class="n8n-templates-name">by {workflow_two_user}</p><a class="n8n-templates-link" href="{workflow_two_url}" target="_blank">View template details</a></div><div class="n8n-templates-widget-template"><strong>{workflow_three_title}</strong><p class="n8n-templates-name">by {workflow_three_user}</p><a class="n8n-templates-link" href="{workflow_three_url}" target="_blank">View template details</a></div><span class="n8n-templates-widget-more"><a href="https://n8n.io/integrations/{node_for_integrations_slug}/" target="_blank">Browse {title} integration templates</a>, or <a href="https://n8n.io/workflows/" target="_blank">search all templates</a></span></div>' |
| 37 | + |
8 | 38 | def workflowDemo(workflow_endpoint): |
9 | 39 | r = requests.get(url = workflow_endpoint) |
10 | 40 | wf_data = r.json() |
|
0 commit comments