Skip to content

Instantly share code, notes, and snippets.

@WisdomCode
Created May 28, 2019 18:47
Show Gist options
  • Save WisdomCode/f4c6fa618d07c59a8ec968d2c3e176ff to your computer and use it in GitHub Desktop.
Save WisdomCode/f4c6fa618d07c59a8ec968d2c3e176ff to your computer and use it in GitHub Desktop.
Installs chrome Extensions, their IDs are given in an external Textfile as a parameter. According extension get installed or updated as needed automatically
#!/usr/bin/env python3
import json
import os
import subprocess
import re
import requests
from bs4 import BeautifulSoup
from distutils.version import LooseVersion
import shutil
import sys
if os.geteuid() != 0:
exit("You need to have root privileges to run this script.\nPlease try again, this time using 'sudo'. Exiting.")
if len(sys.argv) < 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
exit("Syntax: " + sys.argv[0] + " /path/to/textfile/with/extensions.txt")
#Extract the path of the crx file from the json
def getcrx(metafiledest):
with open(metafiledest, 'r') as jsonfile:
metadata = json.load(jsonfile)
return metadata["external_crx"]
#Check if the given version number is higher than the one in the given json file
def isnewer(extversion, metafiledest):
with open(metafiledest, 'r') as jsonfile:
metadata = json.load(jsonfile)
return LooseVersion(metadata["external_version"]) < LooseVersion(extversion)
#download the crx file and create an according json file.
def download(extid, filedest, metadata, metafiledest):
r = requests.get("https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&prodversion=" + version + "&x=id%3D" + extid + "%26installsource%3Dondemand%26uc", stream=True)
if r.status_code == 200:
with open("tmp.crx", 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
os.rename("tmp.crx", filedest)
with open(metafiledest, 'w') as jsonfile:
json.dump(metadata, jsonfile)
#This Part reads the text file you give it. This file will be cleansed of empty lines and whitespace. Also, all input will be converted from URL to the ID if necessary.
#The line should look as followed:
#https://chrome.google.com/webstore/detail/ublock-origin/cjpalhdlnbpafiamejdnhcphjbkeiagm
#https://chrome.google.com/webstore/detail/cjpalhdlnbpafiamejdnhcphjbkeiagm
#
#cjpalhdlnbpafiamejdnhcphjbkeiagm
with open(sys.argv[1], 'r') as infile:
rawlist = infile.readlines()
extlist=[]
for extension in rawlist:
ex=extension.replace(" ", "").replace("\t", "").replace("\n", "")
if ex == "":
continue
if "https://chrome.google.com/webstore/detail/" in ex:
ex = ex.replace("https://chrome.google.com/webstore/detail/", "")
if ex[-1] == "/":
ex = ex[:-1]
if len(ex.split("/")) == 2:
extlist.append(ex.split("/")[1])
else:
extlist.append(ex)
else:
extlist.append(ex)
#Get Version of chromium
rawversion = subprocess.check_output("chromium --version", shell=True).decode("utf-8")
version = re.findall("([0-9\.]+)", rawversion)[0]
#Create the extension folders if they don't yet exist
extfolder="/usr/share/chromium/crxfiles"
extmeta="/usr/share/chromium/extensions"
if not os.path.exists(extfolder):
os.makedirs(extfolder)
os.system("chmod 744 -R " + extfolder)
if not os.path.exists(extmeta):
os.makedirs(extmeta)
os.system("chmod 744 -R " + extmeta)
#A simple work directory is created and deleted at the end
if not os.path.exists("/tmp/chromeext"):
os.mkdir("/tmp/chromeext")
os.chdir("/tmp/chromeext")
for extid in extlist:
r=requests.get("https://chrome.google.com/webstore/detail/" + extid)
soup=BeautifulSoup(r.text, "lxml")
#All the different extension data is collected from the previous site loading and the ID itself
extname=r.url.split("/")[-2]
extversion=soup.find(class_="C-b-p-D-Xe h-C-b-p-D-md").text
filename=extname + "_" + extversion.replace(".", "_") + ".crx"
filedest=extfolder + "/" + filename
metadata={"external_crx": filedest, "external_version": extversion}
metafiledest=extmeta + "/" + extid + ".json"
if not os.path.isfile(metafiledest):
print(extname + " is not yet installed, installing now")
download(extid, filedest, metadata, metafiledest)
elif isnewer(extversion, metafiledest):
print("Updating " + extname)
os.remove(getcrx(metafiledest))
os.remove(metafiledest)
download(extid, filedest, metadata, metafiledest)
else:
print(extname + " is installed and up to date")
os.rmdir("/tmp/chromeext")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment