Skip to content
This repository was archived by the owner on Feb 7, 2021. It is now read-only.

Commit 448d4f9

Browse files
committed
Add dev.py
1 parent 523536e commit 448d4f9

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

dev/dev.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import textwrap
5+
import requests
6+
7+
8+
GH_API = "https://api.github.com"
9+
DEVTO_API = "https://dev.to/api"
10+
11+
GIST_ID = os.environ["INPUT_GIST_ID"]
12+
GH_TOKEN = os.environ["INPUT_GH_TOKEN"]
13+
DEVTO_USERNAME = os.environ["INPUT_DEVTO_USERNAME"]
14+
15+
16+
def update_gist(description, content):
17+
params = {"scope": "gist"}
18+
headers = {"Authorization": "token {}".format(GH_TOKEN)}
19+
payload = {"description": description, "public": True, "files": {"Powered by Dev-Box!": {"content": content}}}
20+
21+
response = requests.post(GH_API + "/gists/" + GIST_ID, headers=headers, params=params, json=payload)
22+
23+
if response.status_code == 200 or response.status_code == 201:
24+
print("Done! The gist was successfully updated!")
25+
else:
26+
raise Exception("Error while updating Gist, check your token!")
27+
28+
29+
def get_article(username):
30+
response = requests.get(DEVTO_API + "/articles", params={"username": username})
31+
32+
if response.status_code == 200:
33+
try:
34+
return response.json()[0]
35+
except IndexError:
36+
raise Exception("Dev.to query failed, no posts found!")
37+
else:
38+
raise Exception("Dev.to query failed, check the provided username!")
39+
40+
41+
def main():
42+
article = get_article(DEVTO_USERNAME)
43+
44+
title = "# " + article["title"]
45+
created_at = article["readable_publish_date"]
46+
47+
# Truncate and wrap long text.
48+
description = (article["description"][:150] + "...") if len(article["description"]) > 150 else article["description"]
49+
description = textwrap.fill(description, width=58)
50+
51+
url = "dev.to" + article["path"]
52+
payload = title + "\n" + description + "\n \n" + url
53+
54+
update_gist("📚 Dev.to - {}".format(created_at), payload)
55+
56+
57+
if __name__ == "__main__":
58+
main()

0 commit comments

Comments
 (0)