Skip to content

Commit

Permalink
blacked
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Lai <[email protected]>
  • Loading branch information
soraxas committed Nov 11, 2021
1 parent 3d31cd2 commit 19ff0ff
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 370 deletions.
12 changes: 6 additions & 6 deletions echo360.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
import sys
from echo360.main import main
import re
import sys
from echo360.main import main

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(main())
if __name__ == "__main__":
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
sys.exit(main())
30 changes: 15 additions & 15 deletions echo360/binary_downloader/chromedriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

class ChromedriverDownloader(BinaryDownloader):
def __init__(self):
self._name = 'chromedriver'
self._download_link_root = 'https://chromedriver.storage.googleapis.com'
self._version = '2.38'
self._name = "chromedriver"
self._download_link_root = "https://chromedriver.storage.googleapis.com"
self._version = "2.38"

def get_os_suffix(self):
self._os_linux_32 = 'linux32'
self._os_linux_64 = 'linux64'
self._os_windows_32 = 'win32'
self._os_windows_64 = 'win32'
self._os_darwin_32 = 'mac64'
self._os_darwin_64 = 'mac64'
self._os_linux_32 = "linux32"
self._os_linux_64 = "linux64"
self._os_windows_32 = "win32"
self._os_windows_64 = "win32"
self._os_darwin_32 = "mac64"
self._os_darwin_64 = "mac64"
return super(ChromedriverDownloader, self).get_os_suffix()

def get_download_link(self):
os_suffix = self.get_os_suffix()
filename = 'chromedriver_{0}.zip'.format(os_suffix)
download_link = '{0}/{1}/{2}'.format(self._download_link_root,
self._version, filename)
filename = "chromedriver_{0}.zip".format(os_suffix)
download_link = "{0}/{1}/{2}".format(
self._download_link_root, self._version, filename
)
return download_link, filename

def get_bin_root_path(self):
return super(ChromedriverDownloader, self).get_bin_root_path()

def get_bin(self):
extension = '.exe' if 'win' in self.get_os_suffix() else ''
return '{0}/{1}{2}'.format(self.get_bin_root_path(), self._name,
extension)
extension = ".exe" if "win" in self.get_os_suffix() else ""
return "{0}/{1}{2}".format(self.get_bin_root_path(), self._name, extension)

def download(self):
super(ChromedriverDownloader, self).download()
44 changes: 24 additions & 20 deletions echo360/binary_downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,44 @@


class BinaryDownloader(object):

def __init__(self):
raise NotImplementedError

def get_os_suffix(self):
arch = '64' if sys.maxsize > 2**32 else '32'
if 'linux' in sys.platform:
if arch == '64':
arch = "64" if sys.maxsize > 2 ** 32 else "32"
if "linux" in sys.platform:
if arch == "64":
return self._os_linux_64
else:
return self._os_linux_32
elif 'win32' in sys.platform:
if arch == '64':
elif "win32" in sys.platform:
if arch == "64":
return self._os_windows_64
else:
return self._os_windows_32
elif 'darwin' in sys.platform:
if arch == '64':
elif "darwin" in sys.platform:
if arch == "64":
return self._os_darwin_64
else:
return self._os_darwin_32
else:
raise Exception('NON-EXISTING OS VERSION')
raise Exception("NON-EXISTING OS VERSION")

def get_download_link(self):
raise NotImplementedError

def get_bin_root_path(self):
return '{0}/bin'.format(os.getcwd())
return "{0}/bin".format(os.getcwd())

def get_bin(self):
raise NotImplementedError

def download(self):
print('>> Downloading {0} binary file for "{1}"'
.format(self._name, self.get_os_suffix()))
print(
'>> Downloading {0} binary file for "{1}"'.format(
self._name, self.get_os_suffix()
)
)
# Download bin for this os
link, filename = self.get_download_link()
bin_path = self.get_bin_root_path()
Expand All @@ -53,20 +55,22 @@ def download(self):
shutil.rmtree(bin_path)
os.makedirs(bin_path)
# remove existing binary file or folder
wget.download(link, out='{0}/{1}'.format(bin_path, filename))
wget.download(link, out="{0}/{1}".format(bin_path, filename))
print('\r\n>> Extracting archive file "{0}"'.format(filename))
if sys.version_info >= (3, 0): # compatibility for python 2 & 3
shutil.unpack_archive('{0}/{1}'.format(bin_path, filename),
extract_dir=bin_path)
shutil.unpack_archive(
"{0}/{1}".format(bin_path, filename), extract_dir=bin_path
)
else:
if '.zip' in filename:
if ".zip" in filename:
import zipfile
with zipfile.ZipFile('{0}/{1}'
.format(bin_path, filename), 'r') as zip:

with zipfile.ZipFile("{0}/{1}".format(bin_path, filename), "r") as zip:
zip.extractall(bin_path)
elif '.tar' in filename:
elif ".tar" in filename:
import tarfile
with tarfile.open('{0}/{1}'.format(bin_path, filename)) as tar:

with tarfile.open("{0}/{1}".format(bin_path, filename)) as tar:
tar.extractall(path=bin_path)
# Make the extracted bin executable
st = os.stat(self.get_bin())
Expand Down
37 changes: 20 additions & 17 deletions echo360/binary_downloader/firefoxdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,39 @@

class FirefoxDownloader(BinaryDownloader):
def __init__(self):
self._name = 'geckodriver'
self._download_link_root = 'https://github.com/mozilla/geckodriver/releases/download'
self._version = 'v0.26.0'
self._name = "geckodriver"
self._download_link_root = (
"https://github.com/mozilla/geckodriver/releases/download"
)
self._version = "v0.26.0"

def get_os_suffix(self):
self._os_linux_32 = 'linux32'
self._os_linux_64 = 'linux64'
self._os_windows_32 = 'win32'
self._os_windows_64 = 'win64'
self._os_darwin_32 = 'macos'
self._os_darwin_64 = 'macos'
self._os_linux_32 = "linux32"
self._os_linux_64 = "linux64"
self._os_windows_32 = "win32"
self._os_windows_64 = "win64"
self._os_darwin_32 = "macos"
self._os_darwin_64 = "macos"
return super(FirefoxDownloader, self).get_os_suffix()

def get_download_link(self):
os_suffix = self.get_os_suffix()
filename = 'geckodriver-{0}-{1}.{2}'.format(
self._version, self.get_os_suffix(),
'zip' if 'win' in self.get_os_suffix() else 'tar.gz'
filename = "geckodriver-{0}-{1}.{2}".format(
self._version,
self.get_os_suffix(),
"zip" if "win" in self.get_os_suffix() else "tar.gz",
)
download_link = "{0}/{1}/{2}".format(
self._download_link_root, self._version, filename
)
download_link = '{0}/{1}/{2}'.format(self._download_link_root,
self._version, filename)
return download_link, filename

def get_bin_root_path(self):
return super(FirefoxDownloader, self).get_bin_root_path()

def get_bin(self):
extension = '.exe' if 'win' in self.get_os_suffix() else ''
return '{0}/{1}{2}'.format(self.get_bin_root_path(), self._name,
extension)
extension = ".exe" if "win" in self.get_os_suffix() else ""
return "{0}/{1}{2}".format(self.get_bin_root_path(), self._name, extension)

def download(self):
super(FirefoxDownloader, self).download()
36 changes: 18 additions & 18 deletions echo360/binary_downloader/phantomjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@

class PhantomjsDownloader(BinaryDownloader):
def __init__(self):
self._name = 'phantomjs'
self._download_link_root = 'https://bitbucket.org/ariya/phantomjs/downloads'
self._version = '2.1.1'
self._name = "phantomjs"
self._download_link_root = "https://bitbucket.org/ariya/phantomjs/downloads"
self._version = "2.1.1"

def get_os_suffix(self):
self._os_linux_32 = 'linux-i686'
self._os_linux_64 = 'linux-x86_64'
self._os_windows_32 = 'windows'
self._os_windows_64 = 'windows'
self._os_darwin_32 = 'macosx'
self._os_darwin_64 = 'macosx'
self._os_linux_32 = "linux-i686"
self._os_linux_64 = "linux-x86_64"
self._os_windows_32 = "windows"
self._os_windows_64 = "windows"
self._os_darwin_32 = "macosx"
self._os_darwin_64 = "macosx"
return super(PhantomjsDownloader, self).get_os_suffix()

def get_download_link(self):
os_suffix = self.get_os_suffix()
filename = 'phantomjs-{0}-{1}'.format(self._version, os_suffix)
if 'linux' in os_suffix:
filename = '{0}.tar.bz2'.format(filename)
filename = "phantomjs-{0}-{1}".format(self._version, os_suffix)
if "linux" in os_suffix:
filename = "{0}.tar.bz2".format(filename)
else:
filename = '{0}.zip'.format(filename)
download_link = '{0}/{1}'.format(self._download_link_root, filename)
filename = "{0}.zip".format(filename)
download_link = "{0}/{1}".format(self._download_link_root, filename)
return download_link, filename

def get_bin_root_path(self):
return super(PhantomjsDownloader, self).get_bin_root_path()

def get_bin(self):
extension = '.exe' if 'windows' in self.get_os_suffix() else ''
return '{0}/phantomjs-{1}-{2}/bin/phantomjs{3}'.format(
self.get_bin_root_path(), self._version, self.get_os_suffix(),
extension)
extension = ".exe" if "windows" in self.get_os_suffix() else ""
return "{0}/phantomjs-{1}-{2}/bin/phantomjs{3}".format(
self.get_bin_root_path(), self._version, self.get_os_suffix(), extension
)

def download(self):
super(PhantomjsDownloader, self).download()
50 changes: 32 additions & 18 deletions echo360/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class EchoCourse(object):

def __init__(self, uuid, hostname=None, alternative_feeds=False):
self._course_id = None
self._course_name = None
Expand All @@ -31,10 +30,14 @@ def get_videos(self):
if not self._videos:
try:
course_data_json = self._get_course_data()
videos_json = course_data_json["section"]["presentations"]["pageContents"]
videos_json = course_data_json["section"]["presentations"][
"pageContents"
]
self._videos = EchoVideos(videos_json, self._driver)
except KeyError as e:
self._blow_up("Unable to parse course videos from JSON (course_data)", e)
self._blow_up(
"Unable to parse course videos from JSON (course_data)", e
)
except selenium.common.exceptions.NoSuchElementException as e:
self._blow_up("selenium cannot find given elements", e)

Expand All @@ -54,25 +57,31 @@ def url(self):

@property
def video_url(self):
return "{}/ess/client/api/sections/{}/section-data.json?pageSize=100".format(self._hostname, self._uuid)
return "{}/ess/client/api/sections/{}/section-data.json?pageSize=100".format(
self._hostname, self._uuid
)

@property
def course_id(self):
if self._course_id is None:
try:
# driver = webdriver.PhantomJS() #TODO Redo this. Maybe use a singleton factory to request the lecho360 driver?s
self.driver.get(self.url) # Initialize to establish the 'anon' cookie that Echo360 sends.
self.driver.get(
self.url
) # Initialize to establish the 'anon' cookie that Echo360 sends.
self.driver.get(self.video_url)
course_data_json = self._get_course_data()

self._course_id = course_data_json["section"]["course"]["identifier"]
self._course_name = course_data_json["section"]["course"]["name"]
except KeyError as e:
self._blow_up("Unable to parse course id (e.g. CS473) from JSON (course_data)", e)
self._blow_up(
"Unable to parse course id (e.g. CS473) from JSON (course_data)", e
)

if type(self._course_id) != str:
# it's type unicode for python2
return self._course_id.encode('utf-8')
return self._course_id.encode("utf-8")
return self._course_id

@property
Expand All @@ -82,7 +91,7 @@ def course_name(self):
self.course_id
if type(self._course_name) != str:
# it's type unicode for python2
return self._course_name.encode('utf-8')
return self._course_name.encode("utf-8")
return self._course_name

@property
Expand All @@ -98,9 +107,11 @@ def nice_name(self):
def _get_course_data(self):
try:
self.driver.get(self.video_url)
_LOGGER.debug("Dumping course page at %s: %s",
self.video_url,
self._driver.page_source)
_LOGGER.debug(
"Dumping course page at %s: %s",
self.video_url,
self._driver.page_source,
)
json_str = self.driver.find_element_by_tag_name("pre").text
except ValueError as e:
raise Exception("Unable to retrieve JSON (course_data) from url", e)
Expand All @@ -117,7 +128,6 @@ def _blow_up(self, msg, e):


class EchoCloudCourse(EchoCourse):

def __init__(self, *args, **kwargs):
super(EchoCloudCourse, self).__init__(*args, **kwargs)

Expand All @@ -128,7 +138,9 @@ def get_videos(self):
try:
course_data_json = self._get_course_data()
videos_json = course_data_json["data"]
self._videos = EchoCloudVideos(videos_json, self._driver, self.hostname, self._alternative_feeds)
self._videos = EchoCloudVideos(
videos_json, self._driver, self.hostname, self._alternative_feeds
)
# except KeyError as e:
# print("Unable to parse course videos from JSON (course_data)")
# raise e
Expand Down Expand Up @@ -165,9 +177,9 @@ def course_name(self):
if self._course_name is None:
# try each available video as some video might be special has contains
# no information about the course.
for v in self.course_data['data']:
for v in self.course_data["data"]:
try:
self._course_name = v['lesson']['video']['published']['courseName']
self._course_name = v["lesson"]["video"]["published"]["courseName"]
break
except KeyError:
pass
Expand All @@ -183,9 +195,11 @@ def nice_name(self):
def _get_course_data(self):
try:
self.driver.get(self.video_url)
_LOGGER.debug("Dumping course page at %s: %s",
self.video_url,
self._driver.page_source)
_LOGGER.debug(
"Dumping course page at %s: %s",
self.video_url,
self._driver.page_source,
)
# use requests to retrieve data
session = requests.Session()
# load cookies
Expand Down
Loading

0 comments on commit 19ff0ff

Please sign in to comment.