all repos — nasg @ f5c599cef923afe23974252f778dd4e4cb214b80

new.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
#!/home/petermolnar.net/.venv/bin/python3.5

"""Usage: new.py [-h] [-t TAGS] [-d DATE] [-s SLUG] [-l TITLE] [-b BOOKMARK] [-r REPLY] [-p REPOST] [-c CONTENT] [-u SUMMARY] [-i REDIRECT] [-a CATEGORY]

-h --help                 show this
-t --tags TAGS            ';' separated, quoted list of tags
-d --date DATE            YYYY-mm-ddTHH:MM:SS+TZTZ formatted date, if not now
-s --slug SLUG            slug (normally autogenerated from title or pubdate)
-l --title TITLE          title of new entry
-b --bookmark BOOKMARK    URL to bookmark
-r --reply REPLY          URL to reply to
-p --repost REPOST        URL to repost
-c --content CONTENT      content of entry
-u --summary SUMMARY      summary of entry
-i --redirect REDIRECT    ';' separated, quoted list of redirects
-a --category CATEGORY    to put the content in this category
"""

import os
import sys
import datetime
import calendar
import logging
import json
import glob
import iso8601
import pytz
from docopt import docopt
from slugify import slugify
from ruamel import yaml
import singular

class ContentCreator(object):
    def __init__(
            self,
            category='note',
            tags=[],
            date='',
            slug='',
            title='',
            bookmark='',
            reply='',
            repost='',
            content='',
            summary='',
            redirect=[]
    ):
        self.category = category

        if date:
            self.date = iso8601.parse_date(date)
        else:
            self.date = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
        self.time = calendar.timegm(self.date.timetuple())

        self.title = title

        if slug:
            self.slug = slug
        elif title:
            self.slug = slugify(title, only_ascii=True, lower=True)
        else:
            self.slug = singular.SingularHandler.baseN(self.time)

        self.tags = tags
        self.bookmark = bookmark
        self.reply = reply
        self.repost = repost
        if content:
            self.content = content
        else:
            self.content = ''
        self.summary = summary
        self.redirect = redirect

        self._makeyaml()
        self._write()


    def _makeyaml(self):
        self.yaml = {
            'published': self.date.strftime("%Y-%m-%dT%H:%M:%S%z")
        }

        if self.title:
            self.yaml['title'] = self.title

        if self.tags:
            self.yaml['tags'] = self.tags

        if self.bookmark:
            self.yaml['bookmark-of'] = self.bookmark

        if self.repost:
            self.yaml['repost-of'] = self.repost

        if self.reply:
            self.yaml['in-reply-to'] = self.reply

        if self.summary:
            self.yaml['summary'] = self.summary

        if self.redirect:
            self.yaml['redirect'] = self.redirect

    def _write(self):
        fdir = os.path.join(glob.CONTENT, self.category)
        if not os.path.isdir(fdir):
            sys.exit("there is no category %s" % (self.category))

        self.fpath = os.path.join(glob.CONTENT, self.category, "%s.md" % (self.slug))
        self.out = "---\n" + yaml.dump(self.yaml, Dumper=yaml.RoundTripDumper) + "---\n\n" + self.content
        with open(self.fpath, "w") as archive:
            logging.info("writing %s", self.fpath)
            logging.info("contents: %s", self.out)
            archive.write(self.out)
            archive.close()


class ParseCMDLine(object):
    def __init__(self, arguments):
        for x in ['--redirect', '--tags']:
            if x in arguments and arguments[x]:
                arguments[x] = arguments[x].split(";")

        self.entry = ContentCreator(
            category=arguments['--category'],
            tags=arguments['--tags'],
            date=arguments['--date'],
            slug=arguments['--slug'],
            title=arguments['--title'],
            bookmark=arguments['--bookmark'],
            reply=arguments['--reply'],
            repost=arguments['--repost'],
            content=arguments['--content'],
            summary=arguments['--summary'],
            redirect=arguments['--redirect']
        )

if __name__ == '__main__':
    args = docopt(__doc__, version='new.py 0.1')

    with open(os.path.join(glob.CACHE, "slugs.json")) as sf:
        slugs = json.loads(sf.read())
        sf.close()

    if not args['--category']:
        c = 'note'
        args['--category'] = input('Category [%s]: ' % (c)) or c

    if not args['--date']:
        d = datetime.datetime.utcnow().replace(tzinfo=pytz.utc).strftime("%Y-%m-%dT%H:%M:%S%z")
        args['--date'] = input('Date [%s]' % (d)) or d

    if not args['--title']:
        args['--title'] = input('Title []:') or ''

    if not args['--tags']:
        args['--tags'] = input('Tags (separated by ;, no whitespace) []:') or []

    if not args['--bookmark']:
        args['--bookmark'] = input('Bookmark of URL []:') or ''

    if not args['--reply']:
        args['--reply'] = input('Reply to URL []:') or ''

    if not args['--repost']:
        args['--repost'] = input('Repost of URL []:') or ''

    if not args['--slug']:
        if args['--title']:
            slug = slugify(args['--title'], only_ascii=True, lower=True)
        elif args['--bookmark']:
            slug = slugify("re: %s" % (args['--bookmark']), only_ascii=True, lower=True)
        elif args['--reply']:
            slug = slugify("re: %s" % (args['--reply']), only_ascii=True, lower=True)
        elif args['--repost']:
            slug = slugify("re: %s" % (args['--repost']), only_ascii=True, lower=True)
        else:
            d = iso8601.parse_date(args['--date'])
            t = calendar.timegm(d.timetuple())
            slug = singular.SingularHandler.baseN(t)
        args['--slug'] = input('Slug [%s]:' % (slug)) or slug

        if args['--slug'] in slugs:
            logging.warning("This slug already exists: %s", args['--slug'])
            slugbase = args['--slug']
            inc = 1
            while args['--slug'] in slugs:
                args['--slug'] = "%s-%d" % (slugbase, inc)
                inc = inc+1
            logging.warning("Using %s as slug", args['--slug'])

    if not args['--summary']:
        args['--summary'] = input('Summary []:') or ''

    if not args['--content']:
        args['--content'] = input('Content []:') or ''

    if not args['--redirect']:
        args['--reditect'] = input('Additional slugs (separated by ;, no whitespace) []:') or []

    p = ParseCMDLine(args)