Skip to content

Commit

Permalink
Publish date (#37)
Browse files Browse the repository at this point in the history
* Allow setting publish date of post

* version bump
  • Loading branch information
raghur authored Aug 3, 2018
1 parent 79165c8 commit a01bf92
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ which will post the blog directly (current behavior)
# create a post from stdin with title and labels


easyblogger post -t "Hello World" -l "python,hello" -c "Hello world!!!"
easyblogger post -t "Hello World" -l "python,hello" -c "Hello world!!!" --date 2018-01-01T10:00:00
```

Pipe out from any HTML generation mechanism
Expand Down Expand Up @@ -342,7 +342,7 @@ you want to change that you'll need to delete and create another post
just published it)

``` {.sourceCode .bash}
easyblogger update -t 'A new title' -l "new,labels" 3295765957555899963
easyblogger update -t 'A new title' -l "new,labels" 3295765957555899963 --date 2018-01-01T10:00:00
```

You can also update the contents by passing in the `--file` argument.
Expand Down Expand Up @@ -373,6 +373,7 @@ PostId:
Labels: a,b,c
format: markdown
published: false
PublishDate: 2018-01-01T10:00:00
filters: <path to your installed filter>
-->
# This is my content
Expand Down Expand Up @@ -435,10 +436,10 @@ are expected. Otherwise the new style Hugo compliant headers are expected.
id = "293493242234"
tags = [ "Rants", "Tips", "Utilities",]
aliases = [ "http://niftybytes.blogspot.com/2018/04/proxy-pac-file-for-work_30.html",]
publishdate = "2018-04-30T12:42:00+05:30"
publishdate = 2018-04-30T12:42:00+05:30
draft = false
date = "2018-04-30T12:42:00+05:30"
lastmod = "2018-04-30T12:47:37+05:30"
date = 2018-04-30T12:42:00+05:30
lastmod = 2018-04-30T12:47:37+05:30
+++
```
* Old style (Easyblogger)
Expand All @@ -447,6 +448,7 @@ are expected. Otherwise the new style Hugo compliant headers are expected.
Labels: [Rants, Tips, Utilities]
PostId: '8010087245053438499'
Published: true
PublishDate: 2018-04-30T12:42:00
Title: Proxy PAC file for work
-->
```
Expand Down
15 changes: 13 additions & 2 deletions blogger/blogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,14 @@ def _getMarkup(self, content, fmt, filters):
return html

def post(self, title, content, labels, filters=[], isDraft=True,
fmt="html"):
fmt="html", publishDate=None):
self._setBlog()
# url = slugify(title) + ".html"
service = self._OAuth_Authenticate()
markup = self._getMarkup(content, fmt, filters)
blogPost = {"content": markup, "title": title}
if publishDate:
blogPost["published"] = publishDate
blogPost['labels'] = EasyBlogger._parseLabels(labels)

req = service.posts().insert(blogId=self.blogId,
Expand All @@ -258,7 +260,7 @@ def deletePost(self, postId):
def updatePost(self, postId, title=None, content=None, labels=None,
filters=[],
isDraft=True,
fmt="html"):
fmt="html", publishDate=None):
self._setBlog()
service = self._OAuth_Authenticate()
blogPost = {}
Expand All @@ -269,6 +271,8 @@ def updatePost(self, postId, title=None, content=None, labels=None,
blogPost['title'] = title
if content:
blogPost['content'] = self._getMarkup(content, fmt, filters)
if publishDate:
blogPost['published'] = publishDate
blogPost['labels'] = EasyBlogger._parseLabels(labels)

logger.debug("blogpost %s", labels)
Expand Down Expand Up @@ -313,6 +317,7 @@ def __init__(self, theFile, open=open):
self.filters = []
self.title = None
self.labels = ["untagged"]
self.publishDate = None

def _inferArgsFromContent(self):
fileContent = self.theFile.read()
Expand Down Expand Up @@ -360,6 +365,8 @@ def _inferArgsFromContent(self):
self.format = frontmatter['Format']
else:
self.format = 'markdown'
if 'PublishDate' in frontmatter:
self.publishDate = frontmatter['PublishDate']
if 'Published' in frontmatter:
self.publishStatus = frontmatter['Published']
else:
Expand All @@ -385,6 +392,8 @@ def _inferArgsFromContent(self):
self.publishStatus = not frontmatter['draft']
else:
self.publishStatus = False
if 'publishdate' in frontmatter:
self.publishDate = frontmatter['publishdate']
if 'filters' in frontmatter:
self.filters = frontmatter['filters']

Expand All @@ -400,6 +409,8 @@ def updateArgs(self, args):
else:
args.command = "post"
args.publish = self.publishStatus
if self.publishDate:
args.publishDate = self.publishDate
args.filters = self.filters
logger.debug("Updated args %s", args)

Expand Down
16 changes: 13 additions & 3 deletions blogger/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ def parse_args(sysargv):
post_parser.add_argument(
"--publish", action="store_true",
help="Publish to the blog [default: false]")
post_parser.add_argument(
"--date",
dest='publishDate',
help="Publish date (ISO8601) of the post - ex:" +
"2018-01-01T10:00:00+05:30"
)
post_input = post_parser.add_mutually_exclusive_group(required=True)
post_input.add_argument("-c", "--content", help="Post content",
type=toUnicode)
Expand All @@ -222,7 +228,9 @@ def parse_args(sysargv):
update_parser = subparsers.add_parser("update", help="update a post")
update_parser.add_argument("postId", help="the post to update")
update_parser.add_argument("-t", "--title", help="Post title")

update_parser.add_argument("--date", dest='publishDate',
help="Publish date (ISO8601) of the post - ex:" +
"2018-01-01T10:00:00+05:30")
update_input = update_parser.add_mutually_exclusive_group()
update_input.add_argument("-c", "--content", help="Post content")
update_input.add_argument(
Expand Down Expand Up @@ -294,7 +302,8 @@ def processItem(args, contentArgs=None):
args.labels,
args.filters,
isDraft=not args.publish,
fmt=args.format)
fmt=args.format,
publishDate=args.publishDate)
postId = newPost['id']
logger.debug("Created post: %s", postId)
if contentArgs:
Expand All @@ -315,7 +324,8 @@ def processItem(args, contentArgs=None):
args.labels,
args.filters,
isDraft=not args.publish,
fmt=args.format)
fmt=args.format,
publishDate=args.publishDate)
print(updated['url'])

if args.command == "get":
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def readme():


setup(name="EasyBlogger",
version="3.0.1",
version="3.1.0",
author="Raghu Rajagopalan",
author_email="[email protected]",
description=("A (very) easy CLI interface to Blogger blogs"),
Expand Down
8 changes: 8 additions & 0 deletions tests/test_contentargparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_should_infer_args_from_content(self):
Labels: l
PostId: "234"
Published: false
PublishDate: 2018-01-01T10:00:00
-->
"""
parser = blogger.ContentArgParser(theFile)
Expand All @@ -24,6 +25,7 @@ def test_should_infer_args_from_content(self):
assert args.postId == "234"
assert args.format == "markdown"
assert args.command == "update"
assert args.publishDate.isoformat() == "2018-01-01T10:00:00"
assert args.publish == False

def test_should_infer_args_for_post(self):
Expand All @@ -33,12 +35,14 @@ def test_should_infer_args_for_post(self):
Title: t
Labels: l
Published: true
PublishDate: 2018-01-01T10:00:00
-->
"""
parser = blogger.ContentArgParser(theFile)
args = Mock()
parser.updateArgs(args)

assert args.publishDate.isoformat() == "2018-01-01T10:00:00"
assert args.title == "t"
assert args.labels == ["l"]
assert args.format == "markdown"
Expand All @@ -52,6 +56,7 @@ def test_should_infer_args_from_toml_header(self):
title= "t"
id= "1234"
tags= ["l", "a", "c"]
publishdate=2018-01-01T10:00:00
+++
this is the post
Expand All @@ -64,6 +69,7 @@ def test_should_infer_args_from_toml_header(self):
assert args.labels == ["l", "a", "c"]
assert args.format == "asciidoc"
assert args.command == "update"
assert args.publishDate.isoformat() == "2018-01-01T10:00:00"
assert not args.publish

def test_should_infer_args_for_post2(self):
Expand Down Expand Up @@ -103,6 +109,7 @@ def test_should_infer_args_for_post3(self):
assert args.labels == ["untagged"]
assert args.format == "markdown"
assert args.command == "post"
assert not parser.publishDate
assert not args.publish

def test_should_handle_empty_file(self):
Expand All @@ -121,6 +128,7 @@ def test_should_handle_empty_file(self):
assert args.format == "markdown"
assert args.command == "post"
assert args.content == "\nabc"
assert not parser.publishDate
assert not args.publish

def test_should_allow_format_to_be_specified(self):
Expand Down
17 changes: 13 additions & 4 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_should_process_files_for_update(self, pypandocMock, blogObjClass):
title= "t"
id= "1234"
tags= ["l", "a", "c"]
publishdate=2018-01-01T10:00:00
+++
this is the post """)
Expand All @@ -77,6 +78,7 @@ def processItemSideEffect(*positionalArgs, **kwargs):
assert args.postId == "1234"
assert args.labels == ["l", "a", "c"]
assert args.command == "update"
assert args.publishDate.isoformat() == "2018-01-01T10:00:00"
assert args.format == "asciidoc"
assert not args.publish
return DEFAULT
Expand All @@ -96,6 +98,7 @@ def test_should_process_files_for_create(self, pypandocMock, blogObjClass):
+++
title= "t"
tags= ["l", "a", "c"]
publishdate=2018-01-01T10:00:00
+++
this is the post """)
Expand All @@ -106,6 +109,8 @@ def processItemSideEffect(*positionalArgs, **kwargs):
assert args.labels == ["l", "a", "c"]
assert args.command == "post"
assert args.format == "asciidoc"
print(args.publishDate.isoformat())
assert args.publishDate.isoformat() == "2018-01-01T10:00:00"
assert not args.publish
return DEFAULT

Expand All @@ -121,15 +126,17 @@ def processItemSideEffect(*positionalArgs, **kwargs):

def test_should_invoke_post(self, pypandocMock, blogObjClass):
pypandocMock.get_pandoc_formats.return_value = [['a'], ['b']]
args = parse_args(['post', "-t", "t", "-c", "content"])
args = parse_args(['post', "-t", "t", "-c", "content", '--date',
'2018-01-01'])
print(args)
blogObj = blogObjClass.return_value
blogObj.post.return_value = {"id": "100", "url": "someurl"}

exitStatus = runner(args)

blogObj.post.assert_called_with(
"t", "content", None, [], isDraft=True, fmt="html")
"t", "content", None, [], isDraft=True, fmt="html",
publishDate='2018-01-01')
assert exitStatus == 0

def test_should_invoke_delete(self, pypandocMock, blogObjClass):
Expand All @@ -146,14 +153,16 @@ def test_should_invoke_delete(self, pypandocMock, blogObjClass):
def test_should_invoke_update(self, pypandocMock, blogObjClass):
pypandocMock.get_pandoc_formats.return_value = [['a'], ['b']]
args = parse_args(
['update', "-t", "t", "-c", "content", "100"])
['update', "-t", "t", "-c", "content", "100", '--date',
'2018-01-01'])
blogObj = blogObjClass.return_value
blogObj.updatePost.return_value = {"id": "100", "url": "someurl"}

runner(args)

blogObj.updatePost.assert_called_with(
"100", "t", "content", None, [], isDraft=True, fmt="html")
"100", "t", "content", None, [], isDraft=True, fmt="html",
publishDate='2018-01-01')

def test_should_invoke_getbyid(self, pypandocMock, blogObjClass):
pypandocMock.get_pandoc_formats.return_value = [['a'], ['b']]
Expand Down

0 comments on commit a01bf92

Please sign in to comment.