Skip to content

Instantly share code, notes, and snippets.

@wayofnumbers
Last active February 1, 2020 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wayofnumbers/334dc5bac61732e7cba43833d556efd2 to your computer and use it in GitHub Desktop.
Save wayofnumbers/334dc5bac61732e7cba43833d556efd2 to your computer and use it in GitHub Desktop.
Automatically turns Medium article URLs into Markdown files and deploy using Pelican
#!/bin/bash
# Define variables
filename='articles.txt'
n=1
# Read in file and do processing on each one
while read line; do
# reading each line
n=$((n+1))
slug=$(echo $line | sed 's/https:\/\/towardsdatascience.com\///' ) # get slug from URL
FILE="$HOME/wayofnumbers.github.io/content/$slug.md" # generate Markdown file name from slug
mediumexporter $line > $FILE # convert medium article to markdown file
# Processing the markdown file
tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE" # remove the first line
fl=$(head -n 1 $FILE) # put first line (title) into fl
firstline=$(echo $fl | sed 's/# //') # Remove '# '
tail -n +3 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE" # remove the first line
subtitle=$(head -n 1 $FILE) # put first line (subtitle) into subtitle
tail -n +2 "$FILE" > "$FILE.tmp" && mv "$FILE.tmp" "$FILE" # remove the first two line
# handle metadata for Pelican
meta="Title: $firstline
Slug: $slug
Subtitle: $subtitle
Date: $(date)
Category: Machine Learning
Tags: Machine Learning, Artificial Intelligence
author: Michael Li
Summary: $firstline
[TOC]
"
{ echo -n "$meta"; cat $FILE; } >$FILE.new # sticth meta-data and article content together
mv $FILE{.new,}
head -n -8 $FILE > $FILE.new # Remove medium's recommended articles
mv $FILE{.new,}
done < $filename
# push to server
cd $HOME/wayofnumbers.github.io
pelican content -s publishconf.py
git add .
git commit -m "fix"
git push origin dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment