-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeed2hugo.py
103 lines (87 loc) · 2.84 KB
/
feed2hugo.py
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
import feedparser
import os, sys
from argparse import ArgumentParser
from slugify import slugify
def main():
#
## Command line parser
#
parser = ArgumentParser(description='Submit dataTasks or dataFlows to Queue')
parser.add_argument('-f', '--feed',
required=True,
dest='feed',
help='Feed url or path to feed file')
parser.add_argument('-t', '--target',
required=True,
dest='target',
help='Root path for Hugo project')
parser.add_argument('-c', '--contenttype',
dest='contenttype',
default='post',
help='project file')
args = parser.parse_args()
try:
HUGO_ROOT_PATH = args.target
except ValueError:
sys.exit("Hugo root path not defined with -t or --target")
try:
FEED_URL = args.feed
except ValueError:
sys.exit("Feed URL or path to feed not defined with -f or --feed")
try:
DEFAULT_CONTENT_TYPE = args.contenttype
except ValueError:
sys.exit("Hugo root path not defined with -c or --contenttype")
#
## CHECK DESTINATION PATH EXISTENCE
#
dest_path = "%s/content/%s" % (HUGO_ROOT_PATH, DEFAULT_CONTENT_TYPE)
if os.path.exists(dest_path) is False:
print("The folder %s does not exist" % dest_path)
os.makedirs(dest_path)
print("The folder %s has been created ..." % dest_path)
else:
pass
#
## PARSE FEED
#
f = feedparser.parse(FEED_URL)
#
## Check if ATOM or RSS ?
#
#
## Atom Case
#
for entry in f.entries:
title = entry.title
link = entry.link
date = entry.published
if "updated" in entry:
lastmod = entry.updated
author = entry.author
if 'tags' in entry:
tags = []
for tag in entry.tags:
tags.append(tag['term'])
content = entry.content[0].value
contenttype = DEFAULT_CONTENT_TYPE
filetitle = "%s-%s.html" % (entry.published[:10], slugify(entry.title))
full_dest_path = "%s/%s" % (dest_path, filetitle)
output = open(full_dest_path, 'w')
output.write('---\n')
output.write('date: %s\n' % date)
output.write('title: "%s"\n' % title.replace('"','\''))
output.write('author: %s\n' % author)
output.write('previous_url: %s\n' % link)
if 'tags' in entry:
output.write('tags: ["%s"] \n' % '", "'.join(tags))
output.write('lastmod: %s\n' % lastmod)
output.write('type: %s\n' % contenttype)
output.write('---\n')
output.write(content)
output.close()
#
## RSS Case - to implement ?
#
if __name__ == '__main__':
main()