Last active
November 26, 2015 06:45
-
-
Save nnarain/d8f729fbc7220a60bbf9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# | |
# Script for generating new posts for my jekyll base blog | |
# | |
# @author Natesh Narain | |
import os | |
import datetime | |
import requests | |
import json | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument('-t', '--title', required=True, help='post title') | |
parser.add_argument('-d', '--description', help='post description') | |
parser.add_argument('-l', '--tags', help='post tags') | |
parser.add_argument('-i', '--thumbnail', help='post thumbnail') | |
parser.add_argument('-c', '--comments', action='store_true', help='create a comment thread') | |
args = vars(parser.parse_args()) | |
post_title = args['title'] | |
post_des = args['description'] if args['description'] else ' ' | |
post_tags = args['tags'] if args['tags'] else ' ' | |
post_thumb = args['thumbnail'] if args['thumbnail'] else ' ' | |
use_comments = args['comments'] | |
user = 'nnarain' | |
token_path = 'C:/github/tokens/blog.auth' | |
def main(): | |
# if this post has a comment section | |
issue_number = '' | |
if use_comments: | |
# create the issue and get the issue number | |
num = createGithubIssue(user, token_path, post_title) | |
issue_number = str(num) if num else '' | |
# create the date string | |
date = datetime.datetime.now() | |
date_string = "%d-%02d-%02d-" % (date.year, date.month, date.day) | |
date_path = date_string.replace('-', '/') | |
# create the file name | |
file_name = date_string + post_title + ".md" | |
# create the post file and add the yaml front matter | |
with open("_posts/" + file_name, 'w+') as file: | |
file.write("---\n") | |
file.write("layout: post\n") | |
file.write("title: " + post_title + "\n") | |
file.write("description: %s\n" % (post_des)) | |
file.write("tag: %s\n" % (post_tags)) | |
file.write("thumbnail: /assets/%s%s\n" % (date_path, post_thumb)) | |
file.write("repo_url: \n") | |
file.write("issue_number: %s\n" % (issue_number)) | |
file.write("---\n\n") | |
# create the date's asset folder | |
date_array = [str(date.year), "%02d" % (date.month), "%02d" % (date.day)] | |
path = 'assets/' | |
for e in date_array: | |
path = path + e + "/" | |
if not os.path.exists(path): | |
os.mkdir(path) | |
# create a github issue in the blog repository for this posts comment section | |
def createGithubIssue(user, tokenpath, title): | |
# fetch the auth token | |
token = None | |
with open(tokenpath, 'r') as tokenfile: | |
token = tokenfile.read() | |
# create the POST data. title of issue and issue body text | |
payload = { | |
'title':'%s - Comments' % (title), | |
'body':('Issue thread for comment section of %s' % title), | |
'labels':['blog post comments'] | |
} | |
response = requests.post( | |
'https://api.github.com/repos/nnarain/nnarain.github.io/issues', | |
auth=(user, token), | |
data=json.dumps(payload) | |
) | |
issuenumber = None | |
if response.status_code < 400: | |
issuenumber = response.json()['number'] | |
return issuenumber | |
# run | |
if __name__ == '__main__': | |
main() | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment