Skip to content

Instantly share code, notes, and snippets.

@imankulov
Last active May 28, 2019 08:53
Show Gist options
  • Save imankulov/cface0d12cc78508539bc191d7c0c970 to your computer and use it in GitHub Desktop.
Save imankulov/cface0d12cc78508539bc191d7c0c970 to your computer and use it in GitHub Desktop.
A MacOS python script to run Google Chrome or Firefox instance with given timezone settings. Requires pytz
import pytz
import os
def guess_timezone(city):
"""
Take the city name and try to guess the timezone
"""
tz_map = {tz.split('/')[-1].lower(): tz for tz in pytz.all_timezones}
try:
return tz_map[city.lower()]
except:
return 'Europe/Moscow'
raise RuntimeError("Error. Timezone for %s not found" % city)
def get_profile_dir(browser, city):
profiles_root = os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'{}_profiles'.format(browser),
)
if not os.path.isdir(profiles_root):
os.makedirs(profiles_root)
return os.path.join(profiles_root, city.lower())
#!/usr/bin/env python
"""
A script to run a seprate instance of Google Chrome with a specific timezone.
Works for MacOS
Accepts city name or timezone offset as an argument. Name of the city has to be
a valid "second part" of one of the timezones from tzdb.
That works:
$ chrome lisbon # because there is a "Europe/Lisbon" timezone
That doesn't work:
$ chrome porto # there is no "Anything/Porto" timezone
Every chrome instance runs as an independent process with a profile in
a directory ./profiles/<city>
"""
import os
import sys
import pytz
from browser_utils import guess_timezone, get_profile_dir
def run_chrome(city):
timezone = guess_timezone(city)
os.environ['TZ'] = timezone
profile_dir = get_profile_dir('chrome', city)
print('Run chrome with %s (profile %s)' % (timezone, profile_dir))
os.execlp('open', 'open', '-na', 'Google Chrome', '--args',
'--user-data-dir=%s' % profile_dir, '--lang=en')
if __name__ == '__main__':
if len(sys.argv) != 2:
raise SystemExit(__doc__)
run_chrome(sys.argv[1])
#!/usr/bin/env python
import os
import sys
import pytz
import shutil
import subprocess as subp
from browser_utils import guess_timezone, get_profile_dir
def run_firefox(city):
try:
timezone = guess_timezone(city)
os.environ['TZ'] = timezone
except RuntimeError as e:
timezone = 'local'
print(e)
profile_dir = get_profile_dir('firefox', city)
if not os.path.isdir(profile_dir):
create_profile(city, profile_dir)
print('Run firefox with %s (profile %s)' % (timezone, profile_dir))
os.execlp('open', 'open', '-na', 'Firefox', '--args',
"-new-instance",
"-no-remote",
"-profile", profile_dir,
"-url", "https://example.com",
)
def create_profile(profile_name, profile_dir):
subp.check_call(["open", "-na", "Firefox", "--args", "-no-remote",
"-CreateProfile", "{} {}".format(profile_name, profile_dir)
])
shutil.copyfile("cert9.db", os.path.join(profile_dir, "cert9.db"))
if __name__ == '__main__':
if len(sys.argv) != 2:
raise SystemExit(__doc__)
run_firefox(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment