|
| 1 | +# USAGE: |
| 2 | +# python devto_posts_backup.py [username] |
| 3 | + |
| 4 | +importrequests |
| 5 | +importos |
| 6 | +importsys |
| 7 | + |
| 8 | +iflen(sys.argv)>2: |
| 9 | +print("Please only call me with one parameter") |
| 10 | +sys.exit() |
| 11 | + |
| 12 | +params= { |
| 13 | +'username':sys.argv[1] |
| 14 | +} |
| 15 | + |
| 16 | +result=requests.get('https://dev.to/api/articles',params=params) |
| 17 | +articles=result.json() |
| 18 | + |
| 19 | +username=params['username'] |
| 20 | +posts_path=os.path.join(sys.path[0],'dev.to',username,'posts') |
| 21 | +ifnotos.path.exists(posts_path): |
| 22 | +os.makedirs(posts_path) |
| 23 | + |
| 24 | +forarticleinarticles: |
| 25 | +slug=article['slug'] |
| 26 | +article_id=article['id'] |
| 27 | +filename=f'{slug}.md' |
| 28 | + |
| 29 | +article_result=requests.get(f'https://dev.to/api/articles/{article_id}') |
| 30 | +article=article_result.json() |
| 31 | + |
| 32 | +f=open(os.path.join(posts_path,filename),"w+",encoding="utf-8") |
| 33 | +f.write(article['body_markdown']) |
| 34 | +f.close() |