Skip to content

Instantly share code, notes, and snippets.

@cheshir
Created May 25, 2020 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheshir/2bc48dcd4d9faacc710234f1e8c8c5be to your computer and use it in GitHub Desktop.
Save cheshir/2bc48dcd4d9faacc710234f1e8c8c5be to your computer and use it in GitHub Desktop.
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
150
]
},
{
"parameters": {
"conditions": {
"string": [
{
"value1": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]}}",
"operation": "notContains",
"value2": "/"
}
]
}
},
"name": "IF",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
600,
600
]
},
{
"parameters": {
"updates": [
"*"
]
},
"name": "Telegram Trigger",
"type": "n8n-nodes-base.telegramTrigger",
"typeVersion": 1,
"position": [
250,
450
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"functionCode": "items[0].json.name = encodeURI($node[\"Telegram Trigger\"].json[\"message\"][\"text\"]);\nreturn items;"
},
"name": "Encode book name",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
750,
580
],
"notesInFlow": false,
"alwaysOutputData": true,
"notes": "Encode name to use it later for parsing"
},
{
"parameters": {
"url": "=http://flibusta.site/booksearch?ask={{$node[\"Encode book name\"].json[\"name\"]}}",
"responseFormat": "string",
"options": {}
},
"name": "Fetch search",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
900,
580
]
},
{
"parameters": {
"functionCode": "function strip_html_tags(str)\n{\n if ((str===null) || (str===''))\n return false;\n else\n str = str.toString();\n return str.replace(/<[^>]*>/g, '');\n}\n\nindex = $node[\"Fetch search\"].json.data.indexOf(\"Найденные книги\");\nif (index === -1) {\n return [{json: {}}];\n}\n\nhtml = $node[\"Fetch search\"].json.data.substring(index);\nhtml = html.substring(0, html.indexOf(\"</ul>\"))\n\nlet books = []\nlet regex = /<li>\\s*<a\\shref=\"(?<link>[^\"]+)\"[^>]*>(?<name>.*?)<\\/a>.*?<a[^>]*>(?<author>[^<]+)/gm\nlet match;\n\nwhile ((match = regex.exec(html)) !== null) {\n // This is necessary to avoid infinite loops with zero-width matches\n if (match.index === regex.lastIndex) {\n regex.lastIndex++;\n }\n \n let book = {\n url: \"http://flibusta.site\" + match.groups[\"link\"],\n name: strip_html_tags(match.groups[\"name\"]),\n author: match.groups[\"author\"],\n }\n \n books.push(book)\n}\n\nreturn [{\n json: {\n books: books,\n }\n}];"
},
"name": "Extract links to book pages",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1050,
580
]
},
{
"parameters": {
"chatId": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"chat\"][\"id\"]}}",
"text": "=No books found.",
"additionalFields": {}
},
"name": "Empty search response",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [
1350,
710
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"conditions": {
"boolean": [],
"string": [],
"number": [
{
"value1": "={{$node[\"Extract links to book pages\"].json[\"books\"].length}}",
"operation": "larger"
}
]
}
},
"name": "Is Empty Search",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
1200,
580
]
},
{
"parameters": {
"chatId": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"chat\"][\"id\"]}}",
"text": "=Commands are not supported yet.",
"additionalFields": {}
},
"name": "Unsupported command response",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [
750,
750
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"chatId": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"chat\"][\"id\"]}}",
"text": "=Here is a book list I found.\nPlease reply me the index of the book you want to download.\n{{$node[\"Format book list message\"].json[\"message\"]}}",
"replyMarkup": "forceReply",
"forceReply": {
"force_reply": true
},
"additionalFields": {
"parse_mode": "Markdown"
}
},
"name": "Send book list",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [
1500,
560
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"functionCode": "let message = \"\"\n\nfor (let i = 0; i < items[0].json.books.length; i++) {\n let book = items[0].json.books[i];\n let name = book.name.replace(/\\[/g, \"<\");\n name = name.replace(/]/g, \">\")\n message += `${i}. [\"${name}\" ${book.author}](${book.url})\\n`;\n}\n\nitems[0].json.message = message;\n\nreturn items;"
},
"name": "Format book list message",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1350,
560
]
},
{
"parameters": {
"conditions": {
"number": [
{
"value1": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"message_id\"]}}",
"operation": "larger"
}
]
}
},
"name": "IF1",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
450,
450
]
},
{
"parameters": {
"conditions": {
"number": [],
"string": [
{
"value1": "={{$node[\"Fetch book\"].binary.book.fileName}}",
"operation": "notEqual"
}
]
}
},
"name": "IF2",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
750,
250
]
},
{
"parameters": {
"chatId": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"chat\"][\"id\"]}}",
"text": "=fb2 book was not found.",
"additionalFields": {}
},
"name": "FB2 book was not found",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [
900,
330
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"path": "=/{{$node[\"Generate file name\"].json[\"book_name\"]}}",
"binaryData": true,
"binaryPropertyName": "book"
},
"name": "Dropbox",
"type": "n8n-nodes-base.dropbox",
"typeVersion": 1,
"position": [
1810,
40
],
"credentials": {
"dropboxApi": "dropbox_books"
}
},
{
"parameters": {
"functionCode": "function sanitizeName(str) {\n let ru = {\n 'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', \n 'е': 'e', 'ё': 'e', 'ж': 'j', 'з': 'z', 'и': 'i', \n 'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n', 'о': 'o', \n 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u', \n 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', \n 'щ': 'shch', 'ы': 'y', 'э': 'e', 'ю': 'u', 'я': 'ya'\n }, n_str = [];\n \n str = str.replace(/[ъь]+/g, '').replace(/й/g, 'i');\n \n for ( var i = 0; i < str.length; ++i ) {\n n_str.push(\n ru[ str[i] ]\n || ru[ str[i].toLowerCase() ] == undefined && str[i]\n || ru[ str[i].toLowerCase() ].replace(/^(.)/, function ( match ) { return match.toUpperCase() })\n );\n }\n \n return n_str.join('').replace(/[^\\x20-\\x7E]/g, '');\n}\n\nlet re = /\\d+\\.\\s*\"(?<title>[^\"]+)\"\\s*(?<author>.*)/m;\nlet body = $node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"text\"];\nlet index = +$node[\"Telegram Trigger\"].json[\"message\"][\"text\"];\nlet row = body.split('\\n')[index+2];\nlet match = re.exec(row)\nlet filename = match[\"groups\"][\"author\"] + \" - \" + match[\"groups\"][\"title\"] + \".fb2\"\nfilename = filename.replace(/(?:<|>)/g, \"\")\n\nitems[0].json.book_name = sanitizeName(filename);\n\nreturn items;"
},
"name": "Generate file name",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1650,
40
]
},
{
"parameters": {
"url": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2",
"responseFormat": "file",
"dataPropertyName": "book",
"options": {},
"headerParametersUi": {
"parameter": []
}
},
"name": "Fetch book",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
600,
250
]
},
{
"parameters": {
"fileName": "={{$node[\"Archive name\"].json[\"archive_name\"]}}",
"dataPropertyName": "=book"
},
"name": "Write Binary File",
"type": "n8n-nodes-base.writeBinaryFile",
"typeVersion": 1,
"position": [
1050,
140
]
},
{
"parameters": {
"command": "=unzip {{$node[\"Write Binary File\"].json[\"archive_name\"]}} -d /tmp -oq"
},
"name": "Execute Command",
"type": "n8n-nodes-base.executeCommand",
"typeVersion": 1,
"position": [
1200,
140
]
},
{
"parameters": {
"functionCode": "let index = $node[\"Telegram Trigger\"].json[\"message\"][\"text\"]\nlet name = $node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][index][\"url\"].substring(23)\nitems[0].json.archive_name = `/tmp/${name}.zip`;\nitems[0].json.unzipped_name = `/tmp/${name}.fb2`; \nreturn items;"
},
"name": "Archive name",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
900,
140
]
},
{
"parameters": {
"filePath": "={{$node[\"Archive name\"].json[\"unzipped_name\"]}}",
"dataPropertyName": "book"
},
"name": "Read Binary File",
"type": "n8n-nodes-base.readBinaryFile",
"typeVersion": 1,
"position": [
1500,
40
]
},
{
"parameters": {
"conditions": {
"number": [
{
"value1": "={{$node[\"Execute Command\"].json[\"exitCode\"]}}",
"operation": "equal"
}
]
}
},
"name": "IF3",
"type": "n8n-nodes-base.if",
"typeVersion": 1,
"position": [
1350,
140
]
},
{
"parameters": {
"chatId": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"chat\"][\"id\"]}}",
"text": "=Failed to extract book from archive: {{$node[\"IF3\"].json[\"stderr\"]}}",
"additionalFields": {}
},
"name": "Exec error",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [
1500,
250
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"chatId": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"chat\"][\"id\"]}}",
"text": "=Successfully download",
"additionalFields": {}
},
"name": "Book uploaded notification",
"type": "n8n-nodes-base.telegram",
"typeVersion": 1,
"position": [
1960,
40
],
"credentials": {
"telegramApi": "books"
}
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "fetch-book"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
}
]
}
},
"name": "Logger",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
600,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "book-fetched"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
},
{
"name": "file-type",
"value": "={{$node[\"Fetch book\"].binary.book.fileName}}"
}
]
}
},
"name": "Logger1",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
750,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "wrote-to-disk"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
},
{
"name": "archive",
"value": "={{$node[\"Write Binary File\"].json[\"archive_name\"]}}"
}
]
}
},
"name": "Logger2",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
1200,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "unzipped"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
},
{
"name": "os-code",
"value": "={{$node[\"Execute Command\"].json[\"exitCode\"]}}"
},
{
"name": "err",
"value": "={{$node[\"Execute Command\"].json[\"stderr\"]}}"
}
]
}
},
"name": "Logger3",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
1350,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "read-from-disk"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
},
{
"name": "filename",
"value": "={{$node[\"Archive name\"].json[\"archive_name\"]}}"
}
]
}
},
"name": "Logger4",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
1650,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "prepare-upload-to-dropbox"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
},
{
"name": "book-name",
"value": "={{$node[\"Generate file name\"].json[\"book_name\"]}}"
}
]
}
},
"name": "Logger5",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
1800,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "upload-to-dropbox-finished"
},
{
"name": "value",
"value": "={{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"entities\"][$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]][\"url\"]}}/fb2"
},
{
"name": "book-name",
"value": "={{$node[\"Generate file name\"].json[\"book_name\"]}}"
}
]
}
},
"name": "Logger6",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
1950,
430
]
},
{
"parameters": {
"requestMethod": "POST",
"url": "https://http-collector.herokuapp.com/index/collected/save",
"options": {},
"bodyParametersUi": {
"parameter": [
{
"name": "action",
"value": "tg-request"
},
{
"name": "value",
"value": "=Message: {{$node[\"Telegram Trigger\"].json[\"message\"][\"text\"]}} \n{{$node[\"Telegram Trigger\"].json[\"message\"][\"reply_to_message\"][\"text\"]}}"
}
]
}
},
"name": "Logger7",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
380,
600
]
}
],
"connections": {
"IF": {
"main": [
[
{
"node": "Encode book name",
"type": "main",
"index": 0
}
],
[
{
"node": "Unsupported command response",
"type": "main",
"index": 0
}
]
]
},
"Telegram Trigger": {
"main": [
[
{
"node": "IF1",
"type": "main",
"index": 0
},
{
"node": "Logger7",
"type": "main",
"index": 0
}
]
]
},
"Encode book name": {
"main": [
[
{
"node": "Fetch search",
"type": "main",
"index": 0
}
]
]
},
"Fetch search": {
"main": [
[
{
"node": "Extract links to book pages",
"type": "main",
"index": 0
}
]
]
},
"Extract links to book pages": {
"main": [
[
{
"node": "Is Empty Search",
"type": "main",
"index": 0
}
]
]
},
"Is Empty Search": {
"main": [
[
{
"node": "Format book list message",
"type": "main",
"index": 0
}
],
[
{
"node": "Empty search response",
"type": "main",
"index": 0
}
]
]
},
"Format book list message": {
"main": [
[
{
"node": "Send book list",
"type": "main",
"index": 0
}
]
]
},
"IF1": {
"main": [
[
{
"node": "Fetch book",
"type": "main",
"index": 0
},
{
"node": "Logger",
"type": "main",
"index": 0
}
],
[
{
"node": "IF",
"type": "main",
"index": 0
}
]
]
},
"IF2": {
"main": [
[
{
"node": "Archive name",
"type": "main",
"index": 0
}
],
[
{
"node": "FB2 book was not found",
"type": "main",
"index": 0
}
]
]
},
"Dropbox": {
"main": [
[
{
"node": "Book uploaded notification",
"type": "main",
"index": 0
},
{
"node": "Logger6",
"type": "main",
"index": 0
}
]
]
},
"Generate file name": {
"main": [
[
{
"node": "Dropbox",
"type": "main",
"index": 0
},
{
"node": "Logger5",
"type": "main",
"index": 0
}
]
]
},
"Fetch book": {
"main": [
[
{
"node": "IF2",
"type": "main",
"index": 0
},
{
"node": "Logger1",
"type": "main",
"index": 0
}
]
]
},
"Write Binary File": {
"main": [
[
{
"node": "Execute Command",
"type": "main",
"index": 0
},
{
"node": "Logger2",
"type": "main",
"index": 0
}
]
]
},
"Execute Command": {
"main": [
[
{
"node": "IF3",
"type": "main",
"index": 0
},
{
"node": "Logger3",
"type": "main",
"index": 0
}
]
]
},
"Archive name": {
"main": [
[
{
"node": "Write Binary File",
"type": "main",
"index": 0
}
]
]
},
"Read Binary File": {
"main": [
[
{
"node": "Generate file name",
"type": "main",
"index": 0
},
{
"node": "Logger4",
"type": "main",
"index": 0
}
]
]
},
"IF3": {
"main": [
[
{
"node": "Read Binary File",
"type": "main",
"index": 0
}
],
[
{
"node": "Exec error",
"type": "main",
"index": 0
}
]
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment