use base64 to do magic for rss

This commit is contained in:
Louis Abel 2023-09-13 01:39:11 -07:00
parent 8b428ef521
commit e68f40bf23
Signed by untrusted user: label
GPG Key ID: B37E62D143879B36

View File

@ -7,9 +7,11 @@
import sys import sys
import os import os
import re
import argparse import argparse
import time import time
import binascii import binascii
import base64
# The old yum-utils repo-rss used string manipulation. We're instead going to # The old yum-utils repo-rss used string manipulation. We're instead going to
# use the XML python library to do the work for us. This is cleaner, imo. # use the XML python library to do the work for us. This is cleaner, imo.
from xml.sax.saxutils import escape as xmlescape from xml.sax.saxutils import escape as xmlescape
@ -30,6 +32,27 @@ def to_unicode(string: str) -> str:
return string return string
return str(string) return str(string)
def to_base64(string: str) -> str:
"""
Converts a string to base64, but we put single quotes around it. This makes
it easier to regex the value.
"""
string_bytes = string.encode('utf-8')
string_conv = base64.b64encode(string_bytes)
base64_str = "'" + string_conv.decode('utf-8') + "'"
return str(base64_str)
def from_base64(string: str) -> str:
"""
Takes a base64 value and returns a string. We also strip off any single
quotes that can happen.
"""
stripped = string.replace("'", "")
conv_bytes = stripped.encode('utf-8')
convd_bytes = base64.b64decode(conv_bytes)
decoded = convd_bytes.decode('utf-8')
return decoded
class DnfQuiet(dnf.Base): class DnfQuiet(dnf.Base):
""" """
DNF object DNF object
@ -141,6 +164,7 @@ class RepoRSS:
description = '<p><strong>{}</strong> - {}</p>\n\n'.format(xmlescape(package.name), xmlescape(package.summary)) description = '<p><strong>{}</strong> - {}</p>\n\n'.format(xmlescape(package.name), xmlescape(package.summary))
description += '<p>%s</p>\n\n<p><strong>Change Log:</strong></p>\n\n' % xmlescape(to_unicode(pkg_description.replace("\n", "<br />\n"))) description += '<p>%s</p>\n\n<p><strong>Change Log:</strong></p>\n\n' % xmlescape(to_unicode(pkg_description.replace("\n", "<br />\n")))
description += xmlescape('<pre>{}</pre>'.format(xmlescape(to_unicode(changelog)))) description += xmlescape('<pre>{}</pre>'.format(xmlescape(to_unicode(changelog))))
base64_description = to_base64(description)
# start item # start item
etbobj.start('item', {}) etbobj.start('item', {})
@ -166,7 +190,7 @@ class RepoRSS:
# end link # end link
# start description # start description
etbobj.start('description', {}) etbobj.start('description', {})
etbobj.data(description) etbobj.data(base64_description)
etbobj.end('description') etbobj.end('description')
# end description # end description
etbobj.end('item') etbobj.end('item')
@ -180,9 +204,23 @@ class RepoRSS:
etree = ElementTree(rss) etree = ElementTree(rss)
some_string = tostring(etree.getroot(), encoding='utf-8') some_string = tostring(etree.getroot(), encoding='utf-8')
xmlstr = minidom.parseString(some_string).toprettyxml(indent=" ") xmlstr = minidom.parseString(some_string).toprettyxml(indent=" ")
#etree.write(file, encoding='utf-8')
with open(file, 'w+', encoding='utf-8') as f: # When writing to the file, we split the string by the newlines. This
f.write(xmlstr) # appears as a list. We loop through the list and find <description>',
# the reason is because we did a base64 encoding of the package
# description to keep the etree from encoding the HTML. We decode it
# and then write it back, along with everything else line by line. This
# is very inefficient, but as far as I can tell, there's no way with
# the built in xml library in python to keep it from doing this.
base64_regex = r"'(.*)'"
with open(f'{file}', 'w+', encoding='utf-8') as f:
for line in xmlstr.splitlines():
new_line = line
if "<description>'" in line:
result = re.search(base64_regex, line)
record = from_base64(result.group(0))
new_line = line.replace(result.group(0), record)
f.write(new_line + '\n')
f.close() f.close()
def make_rss_feed(filename, title, link, description, recent): def make_rss_feed(filename, title, link, description, recent):