Skip to content

Instantly share code, notes, and snippets.

@untalsanders
Created May 25, 2021 17:56
Show Gist options
  • Save untalsanders/7a86287ff0ce81105ba9a356c3c8364b to your computer and use it in GitHub Desktop.
Save untalsanders/7a86287ff0ce81105ba9a356c3c8364b to your computer and use it in GitHub Desktop.
Article Generator

Article Generator

Randomly generate blog posts.

Requirements

You must have a directory to save generated files. Create it if not exists still.

mkdir -p content/posts

Run

python article-generator.article-generatore

License

MIT License Copyright (c) 2021 Sanders Gutierrez

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
import random
import string
from sys import argv
def generate_word():
length = random.randint(1, 10)
word = ''.join(random.choice(string.ascii_letters) for _ in range(length))
return word
def generate_sentence(words):
return ' '.join([generate_word() for i in range(words)])
def generate_categories(num_categories):
return [generate_word() for i in range(num_categories)]
def get_random_date():
year = random.choice(range(1950, 2021))
month = random.choice(range(1, 13))
day = random.choice(range(1, 29))
hours = random.choice(range(0, 24))
minutes = random.choice(range(0, 60))
seconds = random.choice(range(0, 60))
return datetime(year, month, day, hours, minutes, seconds).strftime('%Y-%m-%d_%H:%M:%S')
def create_post(output_dir):
title = generate_sentence(8)
desc = generate_sentence(20)
cat = random.choice(categories)
slug = title.replace(' ', '-').lower()
slug = ''.join(c for c in slug if c.isalnum() or c == '-')
with open('%s/%s.md' % (output_dir, get_random_date()), 'w') as f:
f.write('+++\n')
f.write('title = "%s"\n' % title)
f.write('description = "%s"\n' % desc)
f.write('categories = [\n "%s"\n]\n' % cat)
f.write('date = "%s"\n' % datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S-00:03'))
f.write('slug = "%s"\n' % slug)
f.write('+++\n\n')
num_paragraphs = random.randint(5, 10)
for i in range(num_paragraphs):
f.write(generate_sentence(random.randint(50, 100)))
f.write('\n\n')
# Set defaults
output_dir = 'content/posts'
num_posts = 200
num_categories = 10
# Generate random categories
categories = generate_categories(num_categories)
if __name__ == '__main__':
for i in range(num_posts):
create_post(output_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment