Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/spirita1204/leetcode

🎯Record my Leetcode daily challenge📚Automatically package the LeetCode problems written daily into an .MD file through Notion using a Zapier workflow + Webhooks and upload it to the github.
https://github.com/spirita1204/leetcode

github-api leetcode notion workflow zapier

Last synced: about 3 hours ago
JSON representation

🎯Record my Leetcode daily challenge📚Automatically package the LeetCode problems written daily into an .MD file through Notion using a Zapier workflow + Webhooks and upload it to the github.

Awesome Lists containing this project

README

        

# Leetcode

Automatically package the LeetCode problems written daily into an .MD file through Notion using a **Zapier workflow + Webhooks** and upload it to the github.

Tools:



notion
zapier
github_api



```jsx
-------------------------------
1.Run Javascript 取代標題底線
-------------------------------

// this is wrapped in an `async` function
// you can use await throughout the function
const content = inputData.content;
const replacedContent = content
.replace(/\s+/g, '-') // 將所有空白替換為 '-'
.replace(/\./g, '') // 移除 '.'
.replace(/\//g, '-'); // 將 '/' 替換為 '-'

return { result: replacedContent };

-------------------------------
2.Run Javascript 將內容 Base64 進行傳輸
-------------------------------

const content = inputData.content;
const methods = inputData.methods;
const dataStructure = inputData.dataStructure;
const diff = inputData.diff;
const similar = inputData.similar;
// Information to prepend
let combinedContent = "";
combinedContent += "# " + inputData.title + " \n\n ";// 標題
if(methods)
combinedContent += "Methods: " + methods + " ";
if(dataStructure)
combinedContent += "Data Structure: " + dataStructure + " ";
if(diff)
combinedContent += "Difficulty: " + diff + " ";
if(similar)
combinedContent += "Similar: " + similar + " ";
combinedContent += ("" + content);
// Base64 encode the combined content
const encodedContent = Buffer.from(combinedContent).toString('base64');

return { encoded: encodedContent };

-------------------------------
3.Run Python 透過Github API 自動創建/更新文件
-------------------------------

import requests
import base64

# Define the inputData with necessary details (make sure to pass the actual data)
inputData = {
'title': inputData['title'] , # Replace with the actual file title
'encoded': inputData['encoded'] # Replace with the actual base64-encoded content
}

# URL and data placeholders
url = f"https://api.github.com/repos/spirita1204/Leetcode/contents/{inputData['title']}.md"

# Data for the request
data = {
"message": f"Add {inputData['title']}.md file",
"content": inputData['encoded'], # Base64-encoded content
"branch": "main"
}

# Authorization and headers
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer #YOUR_AUTHORIZATION_TOKEN#",
"X-GitHub-Api-Version": "2022-11-28"
}

# Make the PUT request to GitHub API
response = requests.put(url, json=data, headers=headers)

# Check the response
if response.status_code == 201:
print("File created successfully!")
else:
print(f"Error: {response.status_code}, {response.text}")
if(response.status_code == 422): #如果是文件已存在的錯誤碼,則嘗試覆蓋文件
# 獲取文件sha
response = requests.get(url, headers=headers)
if response.status_code == 200:
sha = response.json()['sha']
print("sha: ", sha)
dataAddSha = {
"message": f"Update {inputData['title']}.md file",
"content": inputData['encoded'], # Base64-encoded content
"sha": sha
}
# 更新文件
response = requests.put(url, json=dataAddSha, headers=headers)
print("File updated successfully!")
else:
print(f"Error: {response.status_code}, {response.text}")
# Returning the result (for Zapier or further use in your code)

return {'success': 'success'}

```