from os import makedirs
from datetime import datetime
import requests
import yaml
import re
import sh
import sys
import logging
from pathlib import Path


TEMPLATE = """\
---
{front_matter}
---

{content}
"""

ROOT_DIR = Path.cwd()
logger = logging.getLogger(__name__)

def slugify(name):
    out = re.sub(r'[^\w\d\s]', '', name)
    return re.sub(r'\s', '-', out)


def create_item(item):
    slug = slugify(item.get('title'))
    makedirs('./content/post/', exist_ok=True)
    file_name = './content/post/{}.md'.format(slug)
    with open(file=file_name, mode='w', encoding='utf-8') as f:
        print('{}...'.format(slug))
        item['date'] = datetime.fromtimestamp(item.get('time')).isoformat()
        item['linkurl'] = item.get('url')
        item['slug'] = slug
        item['tags'] = []
        item['categories'] = []
        if item.get('time', None):
            del item['time']
        if item.get('url', None):
            del item['url']
        if item.get('type', None):
            item['categories'] = [ "{}".format(item.get('type')) ]
            del item['type']
        f.write(TEMPLATE.format(front_matter=yaml.dump(item).strip(), content=""))


def hugo_build():
    hugo = sh.hugo.bake(_cwd=str(ROOT_DIR))
    hugo('--baseURL=https://davidejones.github.io/hugo-hn/', [], _out=sys.stdout)


def main():
    logger.info("Generating hacker news content...")
    end_points = { 'topstories': 'story', 'askstories': 'ask', 'showstories': 'show', 'jobstories': 'job'}
    for point, type in end_points.items():
        items_request = requests.get('https://hacker-news.firebaseio.com/v0/{}.json'.format(point))
        for id in items_request.json():
            item_request = requests.get('https://hacker-news.firebaseio.com/v0/item/{}.json'.format(id))
            item = item_request.json()
            item['type'] = type
            create_item(item)
    hugo_build()

if __name__ == '__main__':
    main()

#!/usr/bin/env python3

import logging
import re
import requests
import sh
import sys
import yaml
from datetime import datetime
from os import makedirs
from pathlib import Path

TEMPLATE = """\
---
{front_matter}
---

{content}
"""

ROOT_DIR = Path.cwd()
logger = logging.getLogger(__name__)


def timing(f):
    """
    decorator to time a function and print it
    :param f: function to wrap
    :return: wrapped function
    """
    def wrap():
        start = time.time()
        f(x)
        end = time.time()
        logger.info(f"{f.__name__} took {end - start!r}")
    return wrap


def slugify(name):
    """
    Takes a article name and returns a slug appropriate version using hyphens
    :param name: string to be converted
    :return: converted string
    """
    out = re.sub(r'[^\w\d\s]', '', name)
    return re.sub(r'\s', '-', out)


def create_item(item):
    """
    Takes a hacker news json object and creates a hugo markdown page from it
    :param item: dict of article data
    """
    yml = {}
    makedirs('./content/post/', exist_ok=True)
    file_name = './content/post/{}.md'.format(slug)
    with open(file=file_name, mode='w', encoding='utf-8') as f:
        yml['date'] = datetime.fromtimestamp(item.get('time')).isoformat()
        yml['linkurl'] = item.get('url')
        yml['slug'] = slugify(item.get('title'))
        yml['tags'] = []
        yml['categories'] = ["{}".format(item.get('type'))] if item.get('type', None) else []
        f.write(TEMPLATE.format(front_matter=yaml.dump(yml).strip(), content=""))


def hugo_build():
    """
    Builds the hugo site
    We overwrite the baseurl all other settings are fine
    """
    hugo = sh.hugo.bake(_cwd=str(ROOT_DIR))
    hugo('--baseURL=https://davidejones.github.io/hugo-hn/', [], _out=sys.stdout)


@timing
def main():
    """
    Entry function that grabs hacker news content saves it and builds the html site
    """
    end_points = {'topstories': 'story', 'askstories': 'ask', 'showstories': 'show', 'jobstories': 'job'}
    for point, type in end_points.items():
        items_request = requests.get('https://hacker-news.firebaseio.com/v0/{}.json'.format(point))
        for id in items_request.json():
            item_request = requests.get('https://hacker-news.firebaseio.com/v0/item/{}.json'.format(id))
            item = item_request.json()
            item['type'] = type
            create_item(item)
    hugo_build()


if __name__ == '__main__':
    main()

update python