Skip to content

add Devto Posts scraper via public API #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions python program/devto_posts_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# USAGE:
# python devto_posts_backup.py [username]

import requests
import os
import sys

if len(sys.argv) > 2:
print("Please only call me with one parameter")
sys.exit()

params = {
'username': sys.argv[1]
}

result = requests.get('https://dev.to/api/articles', params=params)
articles = result.json()

username = params['username']
posts_path = os.path.join(sys.path[0], 'dev.to', username, 'posts')
if not os.path.exists(posts_path):
os.makedirs(posts_path)

for article in articles:
slug = article['slug']
article_id = article['id']
filename = f'{slug}.md'

article_result = requests.get(f'https://dev.to/api/articles/{article_id}')
article = article_result.json()

f = open(os.path.join(posts_path, filename), "w+", encoding="utf-8")
f.write(article['body_markdown'])
f.close()