Created
August 17, 2020 21:49
-
-
Save thecodingchicken/6c66b216512921d70353413b86d4ab7b to your computer and use it in GitHub Desktop.
get_vitalsource1
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 subprocess import Popen | |
import pywinauto | |
from pywinauto.application import Application | |
import autopy | |
import cv2 | |
from gui_automation import GuiAuto | |
import gui_automation | |
import os | |
import time | |
import pyautogui | |
import re | |
import pyperclip | |
match_pbk = re.compile('<img id="pbk-page" src="(/books/\d{9,13}/images/(\d|\w){90}/encrypted/\d{3,4})" data-width="\d{3,4}" style="(\s|\d|\w|;|:)*">') | |
page_ids=['Cover','i','HEOA-1','ii','iii','iv','v','vi','vii','viii','ix','x', | |
'xi','xii','xiii','xiv','xv','xvi','xvii','xviii','xix','xx','xxi', | |
'xxii','xxiii','xxiv','xxv','xxvi','xxvii','xxviii','xxix','xxx', | |
'xxxi','xxxii','xxxiii','xxxiv','xxxv','xxxvi','xxxvii','xxxviii', | |
'xxxix','xl','xli','xlii','xliii','xliv'] | |
WEB_ADDRESS = "https://bookshelf.vitalsource.com/#/books/9781337672030/cfi/%d!/4/2@100:0.00" | |
start_dir = os.path.expanduser(os.path.join('~', 'Desktop','get_vitalsource'))#r'~/Desktop/get_vitalsource') | |
os.chdir(start_dir) | |
next_arrow_img = "next_arrow.png" | |
brave_icon = cv2.imread("brave_icon.png") | |
inspect = cv2.imread("inspect.png") | |
pbk_page = cv2.imread("img_id_pbk-page.png") | |
pbk_page2 = cv2.imread("img_id_pbk-page2.png") | |
down_arrow = cv2.imread("down_arrow.png") | |
site_address='https://jigsaw.vitalsource.com%s' | |
##close_x_button = cv2.imread("inspect_x.png") | |
close_x_button = cv2.imread("inspect_x2.png") | |
show_all_downloads = cv2.imread("downloadshowall.png") | |
save_cancel = cv2.imread("save_cancel.png") | |
ga = GuiAuto() | |
def get_pbk_page_loc(): | |
pos = ga.detect(pbk_page, 0.9) | |
pos2 = ga.detect(pbk_page2,.9) | |
if pos: | |
return pos | |
return pos2 | |
def download_image(path, number): | |
full_path = site_address%path | |
full_path = full_path[:full_path.rfind('/')+1]+'2400' | |
print("\n%s\n"%full_path) | |
pyautogui.hotkey('ctrl','t') | |
time.sleep(5) | |
autopy.key.type_string(full_path,0) | |
autopy.key.tap(autopy.key.Code.RETURN)#go to the site | |
time.sleep(5)#if i wait until i see the reload button it should be fine | |
pyautogui.hotkey('ctrl','s') | |
while not ga.detect(save_cancel,.9): | |
time.sleep(1) | |
time.sleep(2) | |
save_path = os.path.join(start_dir,'images','page%04d'%number) | |
autopy.key.type_string(save_path) | |
autopy.key.tap(autopy.key.Code.RETURN) | |
if ga.detect(show_all_downloads,.9): | |
ga.click() | |
def download_page(page_id, page_middle, address, number): | |
pos = ga.detect(brave_icon,0.9) | |
if not pos: | |
print("Error on failed page %d"%page) | |
return "Error on failed page %d"%page | |
#go to link bar stuff | |
ga.click(1,(pos.x,pos.y+10)) | |
time.sleep(.25) | |
pyautogui.hotkey('ctrl','a');pyautogui.press('del'); | |
time.sleep(.25) | |
autopy.key.type_string(address,0) | |
autopy.key.tap(autopy.key.Code.RETURN) | |
#we've typed the address, and hit enter. wait some time for it to load | |
time.sleep(10)#Adjust if it's too quick | |
autopy.mouse.move(page_middle[0],page_middle[1]) | |
autopy.mouse.click(button=autopy.mouse.Button.RIGHT) | |
time.sleep(2) | |
pos = ga.detect(inspect, 0.8) | |
## autopy.mouse.click(button=autopy.mouse.Button.LEFT) | |
ga.click() | |
pyautogui.hotkey('ctrl','f') | |
autopy.key.type_string('pbk-page',0) | |
found = False | |
while not found: | |
pos = get_pbk_page_loc() | |
if pos: | |
found = True | |
else: | |
#didn't find the <img id="pbk-page" src, so retry after | |
# pressing the down button | |
ga.detect(down_arrow,.9) | |
ga.click()#move down, no error checking here so | |
# oops if it goes wrong | |
# We found it now, so click on the line, copy, parse, then change to a better | |
# resolution(w=2000 seems like max) | |
center = pos.center() | |
autopy.mouse.move(center[0],center[1]) | |
autopy.mouse.click()#button=autopy.mouse.Button.RIGHT) | |
time.sleep(.25) | |
pyautogui.hotkey('ctrl','c') | |
time.sleep(.25) | |
pbk_data = pyperclip.paste() | |
## print("\n",pbk_data) | |
subaddress_tuple = match_pbk.search(pbk_data).groups()#i assume it has stuff | |
subaddress = subaddress_tuple[0] | |
for temp in subaddress_tuple: | |
if len(temp)>len(subaddress): | |
subaddress = temp | |
## print("[DEBUG] Address %s"%subaddress)#it works | |
download_image(subaddress, number) | |
close_inspect_window()#reset to initial state...well, close enough :-) | |
def close_inspect_window(): | |
pos = ga.detect(close_x_button,.9) | |
if pos: | |
pos=pos.bottom_right() | |
print(pos) | |
ga.click(coords=pos) | |
time.sleep(.5) | |
#otherwise, we're fine here! | |
def get_window_bounds(): | |
print("Move to top left within 5 seconds, location will be captured") | |
time.sleep(5) | |
loc1 = autopy.mouse.location() | |
print("Captured, move to bottom right") | |
time.sleep(5) | |
loc2 = autopy.mouse.location() | |
print("Thanks for giving up all your privacy") | |
return loc1, loc2 | |
def download_book(): | |
if ga.detect(show_all_downloads,.9): | |
ga.click() | |
## close_inspect_window() | |
loc1,loc2=get_window_bounds() | |
middle_of_screen = ((loc1[0]+loc2[0])/2,(loc1[1]+loc2[1])/2) | |
for page in range(0, 786): | |
print("[Start ] page %4d"%page,end='\r') | |
address = WEB_ADDRESS%page | |
pos = ga.detect(brave_icon,0.9) | |
if not pos: | |
print("Error on failed page %d"%page) | |
break | |
t=download_page(address, middle_of_screen, address, page) | |
if t: | |
print(t) | |
break | |
print("[Finish] page %4d"%page,end='\r') | |
time.sleep(1) | |
if page==2:break | |
##try:download_book() | |
##except Exception as error: | |
## print("\nStopped, here's my error for your use\n%s"%error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment