Skip to content

Instantly share code, notes, and snippets.

@guissalustiano
Created September 11, 2021 02:58
Show Gist options
  • Save guissalustiano/c5a1e87fb23cbbc18aea44c1d6ad7ba7 to your computer and use it in GitHub Desktop.
Save guissalustiano/c5a1e87fb23cbbc18aea44c1d6ad7ba7 to your computer and use it in GitHub Desktop.
Replace moodle redirects
import requests
import re
from functools import cache
MOODLE_SESSION = ''
FILENAME = 'readme.md'
@cache
def file_content():
with open(FILENAME) as fp:
return fp.read()
def save_file_content(content: str):
with open(FILENAME, 'w') as fp:
fp.write(content)
def redirect_url(url: str):
response = requests.get(url, cookies={
'MoodleSession': MOODLE_SESSION,
})
return response.url
def get_raw_urls():
content = file_content()
pattern = r'https://cursosextensao.usp.br/mod/url/[-a-zA-Z0-9@:%_\+.~#?&//=]*'
return re.findall(pattern, content)
if __name__ == '__main__':
content = file_content()
for raw_url in get_raw_urls():
url = redirect_url(raw_url)
content = content.replace(raw_url, url)
print(f'replace {raw_url} by {url}')
save_file_content(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment