update-blogroll.py (2161B) - raw


      1 #!/usr/bin/env python3
      2 # Update Blogroll: updates blogroll page and OMPL file.
      3 # Copyright (C) 2020 Oscar Benedito <oscar@oscarbenedito.com>
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Affero General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Affero General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Affero General Public License
     16 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
     17 
     18 import xml.etree.ElementTree
     19 import re
     20 
     21 
     22 tree = xml.etree.ElementTree.parse('misc/blogroll.ompl')
     23 root = tree.getroot()
     24 
     25 blogs = []
     26 for category in root[0]:
     27     if category.attrib['text'] == 'Blogroll':
     28         for entry in category:
     29             blogs.append({
     30                 'text': entry.attrib['text'],
     31                 'html': entry.attrib['htmlUrl'],
     32                 'feed': entry.attrib['xmlUrl']
     33             })
     34         break
     35 
     36 ompl = '<?xml version="1.0" encoding="utf-8"?>\n<opml version="1.0">\n' \
     37        '  <head>\n    <title>Oscar Benedito\'s Blogroll</title>\n  </head>\n' \
     38        '  <body>\n    <outline text="Oscar Benedito\'s Blogroll">\n'
     39 md = '<!-- blogroll -->\n'
     40 
     41 ompl_item = '      <outline type="rss" text="{}" xmlUrl="{}" htmlUrl="{}"/>\n'
     42 md_item = '- [{}]({}) — [Feed]({})\n'
     43 
     44 for blog in sorted(blogs, key=lambda i: i['text'].lower()):
     45     ompl += ompl_item.format(blog['text'], blog['feed'], blog['html'])
     46     md += md_item.format(blog['text'], blog['html'], blog['feed'])
     47 
     48 ompl += '    </outline>\n  </body>\n</opml>\n'
     49 md += '<!-- /blogroll -->'
     50 
     51 with open('static/blogroll/blogroll.ompl', 'w') as f:
     52     f.write(ompl)
     53 
     54 with open('content/blogroll.md', 'r') as f:
     55     text = f.read()
     56 
     57 text = re.sub('<!-- blogroll -->.*<!-- /blogroll -->', md, text, flags=re.DOTALL)
     58 
     59 with open('content/blogroll.md', 'w') as f:
     60     f.write(text)