Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
rssfixer/rssfixer.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
71 lines (52 sloc)
2 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8; mode: python -*- | |
""" | |
Fix the rss.xml files. Replace 'testserver' with the correct server name. | |
""" | |
__version__ = "1.0" | |
__date__ = "20180730" | |
__author__ = "kthoden@mpiwg-berlin.mpg.de" | |
import logging | |
import argparse | |
import shutil | |
from lxml import etree | |
NS_ATOM = "http://www.w3.org/2005/Atom" | |
NS_MAP = {"a" : NS_ATOM} | |
logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s') | |
def replace_link_text(old_url): | |
"""Replace the link text""" | |
if old_url.find("testserver/sources") == -1: | |
new_link_text = old_url.replace("testserver", "www.edition-open-access.de") | |
else: | |
new_link_text = old_url.replace("testserver", "www.edition-open-sources.org") | |
return new_link_text | |
# def replace_link_text ends here | |
def main(): | |
"""The main bit""" | |
parser = argparse.ArgumentParser() | |
parser.add_argument("rssfile", help="The RSS file that is to be fixed.") | |
args = parser.parse_args() | |
rss_file = args.rssfile | |
rss_tree = etree.parse(rss_file) | |
atom_link_element = rss_tree.xpath("/rss/channel/a:link", namespaces=NS_MAP)[0] | |
atom_link_url = atom_link_element.get("href") | |
new_atom_link = replace_link_text(atom_link_url) | |
atom_link_element.set("href", new_atom_link) | |
feed_items = rss_tree.xpath("/rss/channel/item", namespaces=NS_MAP) | |
logging.info("Found %d feed items.", len(feed_items)) | |
for book in feed_items: | |
link_url = book.find("link") | |
link_text = link_url.text | |
new_link_text = replace_link_text(link_text) | |
link_url.text = new_link_text | |
guid_url = book.find("guid") | |
guid_text = guid_url.text | |
new_guid_text = replace_link_text(guid_text) | |
guid_url.text = new_guid_text | |
shutil.move(rss_file, rss_file.replace(".xml", "-old.xml")) | |
rss_tree.write(rss_file, pretty_print=True, xml_declaration=True, encoding="utf-8") | |
logging.info("Wrote %s.", rss_file) | |
# def main ends here | |
if __name__ == '__main__': | |
main() | |
# finis |