-
Zed_Oud
@cook
Thanks for looking at my code. I'm glad to know I hadn't overlooked something silly.As it is, I am trying to make an extension to re-display or mobilize certain kinds of websites. Imagine if I went to a website full of thumbnails, and the script grabbed all of the linked full-size images. There's other websites and whatnot, but I was mostly playing around with extensions for now.
Thanks again for the help.
-
Zed_Oud
The HTML I'm using works everywhere, but not loaded through in an extension. That's my whole goal, I am trying to replace webbrowser.open("local file") for use in an extension. I haven't tried to load a local image using my HTML doc, I'll try that out.
Here is an example of my HTML when I point it at "http://xkcd.com"<html> <body bgcolor="#000000"> <img src="http://imgs.xkcd.com/static/terrible_small_logo.png" alt="http://imgs.xkcd.com/static/terrible_small_logo.png" ><br><br> <img src="http://imgs.xkcd.com/comics/tire_swing.png" alt="http://imgs.xkcd.com/comics/tire_swing.png" ><br><br> <img src="http://imgs.xkcd.com/store/te-pages-sb.png" alt="http://imgs.xkcd.com/store/te-pages-sb.png" ><br><br> <img src="http://imgs.xkcd.com/s/a899e84.jpg" alt="http://imgs.xkcd.com/s/a899e84.jpg" > </body> </html>
Here is my full code (cleaned and formatted, but just as dysfunctional when used as an extension):
# coding: utf-8 import appex from urllib2 import urlopen import os, console, requests, urlparse def write_text(name, text, writ='w'): with open(name, writ) as o: o.write(text) def img_page(file_list, link_list=None): if link_list is None: link_list = file_list links = zip(file_list, link_list) x = '<br><br>\n'.join(['<img src="{0}" alt="{1}" >'.format(a,b) for a,b in links]) y = """ <html> <body bgcolor="#000000"> {0} </body> </html> """.format(x) return y def view_doc(text): import ui w = ui.WebView() w.scales_page_to_fit = False w.load_html(text) w.present() def open_file(file_path): import ui file_path = os.path.abspath(file_path) file_path = urlparse.urljoin('file://', os.path.abspath(file_path)) #v = ui.View() #file_path = 'http://xkcd.com' wv = ui.WebView() #v.add_subview(wv) wv.load_url(file_path) #v.frame = (0,0,320,568) #wv.frame = (0,0,320,568) #v.present() wv.present() def view_temp_index(file_url_list): temp_fn = '__temp.html' write_text(temp_fn, img_page(file_url_list)) open_file(temp_fn) def get_Pic_Links_Content(content,url=None): from bs4 import BeautifulSoup as bs if url is None: url = '' # 'http://' s = bs(content) p = s.findAll('img') pics = [] for x in p: y = urlparse.urljoin(url, x['src']) if y not in pics: pics.append(y) return pics def get_Pic_Links(url): r = requests.get(url) #print 'viewing pics from url:', r.url return get_Pic_Links_Content(r.content, url) def pick(url): choice = console.alert('View:','Pick where to view source:','Make File','View Directly','Console') pics = get_Pic_Links(url) if choice == 1: view_temp_index(pics) elif choice == 2: view_doc(img_page(pics)) else: print '\n'.join(pics) def main(): if not appex.is_running_extension(): print '\nRunning using test data...' url = 'http://xkcd.com' else: url = appex.get_url() if url: pick(url) else: print 'No input URL found.' if __name__ == '__main__': main()```
-
Zed_Oud
Correct me if I'm wrong, but does the memory limit while running Python as an appex extension prevent us from loading things with WebView.load_url or WebView.load_html ?
I've managed to get a local html file to load, but it will not populate its img tags, images will not load and leave the default blank bar/box ( "<img src="http://url.jpg"> ). The same html file will load perfectly using ui.webview or webbrowser NOT running from an extension (the html doc will work anywhere and everywhere else).
import ui def view(text): v = ui.View() wv = ui.WebView() v.add_subview(wv) wv.load_html(text) v.frame = (0,0,320,568) wv.frame = (0,0,320,568) v.present()
There's a cleaned up example of the function called to run my html doc as a string, though I've also tried running as a local file.