Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/tools/dev/update_user_agent_strings.py
Views: 11768
#!/usr/bin/python31import requests2import re34def replace_agent_string(lines, replace_marker, url, regex):5VALID_CHARS = 'a-zA-Z0-9\\(\\);:\\.,/_ '6regex = regex.replace('{VALID_CHARS}', VALID_CHARS)7print(f'Updating {replace_marker}')8for x in range(0, len(lines)):9if replace_marker in lines[x]:10break11else:12raise RuntimeError(f"Couldn't find marker {replace_marker}")1314response = requests.get(url)15if response.status_code != 200:16raise RuntimeError(f"Can't retrieve {url}")1718match = re.search(regex, response.text)19if match is None:20raise RuntimeError(f"Couldn't match regex {regex}")2122new_string = match.groups()[0]23print(f'New value is: {new_string}')24old_line = lines[x]25if f"'{new_string}'" in old_line:26print('(This is unchanged from the previous value)')27else:28new_line = re.sub("'(.*)'", f"'{new_string}'", old_line)29if old_line == new_line:30raise RuntimeError(f"Line didn't change: {old_line}")3132lines[x] = new_line333435chrome_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome"36edge_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/edge"37safari_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/safari"38firefox_url = "https://www.whatismybrowser.com/guides/the-latest-user-agent/firefox"3940user_agent_filename = 'lib/rex/user_agent.rb'41with open(user_agent_filename,'r') as f:42lines = f.read().splitlines()4344replace_agent_string(lines, 'Chrome Windows', chrome_url, '<td>Chrome \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Windows NT[{VALID_CHARS}]*)</span>')45replace_agent_string(lines, 'Chrome MacOS', chrome_url, '<td>Chrome \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Macintosh[{VALID_CHARS}]*)</span>')46replace_agent_string(lines, 'Edge Windows', edge_url, '<td>Edge \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Windows NT[{VALID_CHARS}]*)</span>')47replace_agent_string(lines, 'Safari iPad', safari_url, '<td>\s*Safari on <b>Ipad</b>\s*</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*iPad[{VALID_CHARS}]*)</span>')48replace_agent_string(lines, 'Safari MacOS', safari_url, '<td>Safari \\(Standard\\)</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Macintosh[{VALID_CHARS}]*)</span>')49replace_agent_string(lines, 'Firefox Windows', firefox_url, '<td>\s*Firefox on <b>Windows</b>\s*</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Windows NT[{VALID_CHARS}]*)</span>')50replace_agent_string(lines, 'Firefox MacOS', firefox_url, '<td>\s*Firefox on <b>Macos</b>\s*</td>\s*<td>\s*<ul>\s*<li><span class="code">([{VALID_CHARS}]*Macintosh[{VALID_CHARS}]*)</span>')5152with open(user_agent_filename, 'w') as f:53f.write('\n'.join(lines) + '\n')5455print('Done')565758