Created
August 4, 2020 18:03
-
-
Save pramitbiswas/61bfd40528b3e114dd52d3dcfb5bed07 to your computer and use it in GitHub Desktop.
Give user-agent as per installed chrome browser, and check for updated user-agent after every six months
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dateutil import relativedelta | |
from os import path | |
import json | |
import datetime | |
from selenium import webdriver | |
from webdriver_manager.chrome import ChromeDriverManager | |
def getUA(): | |
driver = webdriver.Chrome(ChromeDriverManager().install()) | |
ua = driver.execute_script("return navigator.userAgent;") | |
driver.quit() | |
return ua | |
def saveUA(ua): | |
saveContent={} | |
saveContent['ua'] = ua | |
saveContent['atTime'] = str(datetime.datetime.now()) | |
with open('ua.json', 'w') as fp: | |
json.dump(saveContent, fp) | |
def checkExistingUA(): | |
if path.exists("ua.json"): | |
try: | |
with open('ua.json') as f: | |
saveContent = json.load(f) | |
lastDate = datetime.datetime.strptime(saveContent['atTime'], '%Y-%m-%d %H:%M:%S.%f') | |
rDelta = relativedelta.relativedelta(lastDate, datetime.datetime.now()) | |
if rDelta.years <= 0 and rDelta.months <= 6: | |
ua = saveContent['ua'] | |
return True, ua | |
else: | |
return False, None | |
except: | |
return False, None | |
else: | |
return False, None | |
def getLatestUA(): | |
flag, ua = checkExistingUA() | |
if not flag: | |
ua = getUA() | |
saveUA(ua) | |
return ua | |
if __name__ == '__main__': | |
# Direct | |
print (getLatestUA()) | |
else: | |
# Import | |
ua = getLatestUA() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment