import pywikibot
import re

def save_page(target, gallery):
"""Saves the page to enwiki, making sure to leave text above the line"""
#oldWikitext = target.text
oldWikitext = '''This report is generated automatically every 8 hours. Edits to this page will not be preserved on update. For more information, see [[:m:Steward_requests/Miscellaneous#Hijacked_domain_and_predatory_spam|here]] and [[meta:User:Praxidicae/DOI fix]]
<!-- Only text ABOVE this line will be preserved on updates -->

'''

print(oldWikitext)
print(gallery)
regex = re.compile(
'(?<=<!-- Only text ABOVE this line '
'will be preserved on updates -->\n).*', re.M | re.S)
newWikitext = re.sub(regex, gallery, oldWikitext)
print(newWikitext)
target.text = newWikitext
print(target.text)

tsite = pywikibot.Site('en', 'wikipedia')
tpage = pywikibot.Page(tsite, 'User:AntiCompositeBot/HijackSpam/Report')

save_page(tpage, 'bar')

This report is generated automatically every 8 hours. Edits to this page will not be preserved on update. For more information, see [[:m:Steward_requests/Miscellaneous#Hijacked_domain_and_predatory_spam|here]] and [[meta:User:Praxidicae/DOI fix]]
<!-- Only text ABOVE this line will be preserved on updates -->

bar
This report is generated automatically every 8 hours. Edits to this page will not be preserved on update. For more information, see [[:m:Steward_requests/Miscellaneous#Hijacked_domain_and_predatory_spam|here]] and [[meta:User:Praxidicae/DOI fix]]
<!-- Only text ABOVE this line will be preserved on updates -->
bar
This report is generated automatically every 8 hours. Edits to this page will not be preserved on update. For more information, see [[:m:Steward_requests/Miscellaneous#Hijacked_domain_and_predatory_spam|here]] and [[meta:User:Praxidicae/DOI fix]]
<!-- Only text ABOVE this line will be preserved on updates -->
bar