Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Dendi Handian
Dendi Handian

Posted on

     

Backup Your DEV.to Posts Using Python

I treat my posts on DEV.to as notes. Backing up my notes to prevent any unexpected incident is a must, even though I believe DEV.to will protect my posts unless it's us that makes the accident. So, why don't we back up them by ourselves?

Of course, we don't back up the posts by copying and pasting the markdown bodies manually into a file one by one. Let's automate them by using DEV.to API and Python.

The Goal

By requesting tohttps://dev.to/api/articles?username={username}, we can get the article list of the given username. We will parse the response and make the markdown files for each article. It's that simple.

The Packages We Need

For consuming the DEV.to API, we need the infamousrequests package. So, add the package to the machine usingpip orpipenv or anything that you use. We also need the built-inos andsys modules for working with directories and paths. Let's import them to our python file:

importrequestsimportosimportsys
Enter fullscreen modeExit fullscreen mode

The Parameters for the Request

By looking at the URL for request, we only need theusername parameter. So, let's create adict with that param:

params={'username':'dendihandian'# change it to your username}
Enter fullscreen modeExit fullscreen mode

Perform the request

Let's request and get the articles with this script:

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

Apparently, the articles here don't have thebody_markdown and we will get the attribute by requesting the full resources of each article later.

Making the directories for saving the markdown files

We will save the markdown files under this path./dev.to/{username}/posts and we use bothos andsys modules to make the directories first:

username=params['username']posts_path=os.path.join(sys.path[0],'dev.to',username,'posts')ifnotos.path.exists(posts_path):os.makedirs(posts_path)
Enter fullscreen modeExit fullscreen mode

We have to create the directories first because we can't directly set the full path to the file within theopen() function if the directories not exist.

Parse the Articles and Make The Markdown Files

We loop the articles objects and request to get thebody_markdown of each article and write them into a markdown file:

forarticleinarticles: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()
Enter fullscreen modeExit fullscreen mode

And that's it. Execute the python file and see if your posts are saved into markdown files.


Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Data Engineer - Building Data Lake & Modern Data Architecture
  • Location
    Jakarta
  • Education
    B.S. Computer Science
  • Work
    Data Engineer at Media Production Company
  • Joined

More fromDendi Handian

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp