Last active
April 19, 2018 09:04
-
-
Save tfgrahame/fbef4af1fa3b47e27958e6fc9ac2df6e to your computer and use it in GitHub Desktop.
Replaces the parent brand of a clip
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 python | |
from requests import get, put | |
import os | |
import argparse | |
import xml.etree.ElementTree as ET | |
parser = argparse.ArgumentParser(description='Entities and their pids.') | |
parser.add_argument('child_pid', type=str, help='The pid of the child entity') | |
parser.add_argument('parent_pid', type=str, help='The pid of the parent entity') | |
args = parser.parse_args() | |
cert = os.environ.get('CERT') | |
pips_base = os.environ.get('PIPS_BASE') | |
ET.register_namespace('pips', 'http://ns.webservices.bbc.co.uk/2006/02/pips') | |
ns = {'pips': 'http://ns.webservices.bbc.co.uk/2006/02/pips'} | |
def make_link(element_name, parent_pid): | |
element = ET.Element('pips:' + element_name) | |
link = ET.SubElement(element, 'pips:link') | |
link.set('rel', 'pips-meta:brand') | |
link.set('pid', parent_pid) | |
link.set('href', pips_base + 'brand/pid.' + parent_pid + '/') | |
return element | |
def main(): | |
r = get(pips_base + 'clip/pid.' + args.child_pid + '/', cert=cert) | |
root = ET.XML(r.content) | |
for element in ['clip_of', 'member_of_brand']: | |
root[0].remove(root[0].find('pips:' + element, ns)) | |
root[0].insert(2, make_link(element, args.parent_pid)) | |
# note that this makes a bytestring not a unicode value | |
data = ET.tostring(root) | |
put_response = put(pips_base + 'clip/pid.' + args.child_pid + '/', cert=cert, data=data) | |
print(put_response) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment