~yacinechaouche/+junk/BZR

« back to all changes in this revision

Viewing changes to CODE/TEST/PYTHON/seleniumff.py

  • Committer: yacinechaouche at yahoo
  • Date: 2015-01-14 22:23:03 UTC
  • Revision ID: yacinechaouche@yahoo.com-20150114222303-6gbtqqxii717vyka
Ajout de CODE et PROD. Il faudra ensuite ajouter ce qu'il y avait dan TMP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from selenium import webdriver
 
2
from selenium.webdriver.common.keys import Keys
 
3
from cmd import Cmd
 
4
import commands
 
5
 
 
6
def raise_problem(function):
 
7
    def wrapper(*args,**kw): 
 
8
        try:
 
9
            return function(*args,**kw)
 
10
        except:
 
11
            print traceback.print_exc()
 
12
    return wrapper
 
13
 
 
14
class MyApp(Cmd):
 
15
    def __init__(self,*args):
 
16
        Cmd.__init__(self,*args)
 
17
        self.browser       = webdriver.Firefox()
 
18
        self.new_window    = webdriver.ActionChains(self.browser).send_keys(Keys.CONTROL + "n").perform
 
19
        self.close_window  = webdriver.ActionChains(self.browser).send_keys(Keys.CONTROL + "w").perform
 
20
        self.visited_links = []
 
21
        self.setup()
 
22
 
 
23
    def setup(self,*args):
 
24
        #self.browser.get("http://www.grooveshark.com")
 
25
        #grooveshark_window = self.browser.window_handles[0]
 
26
        #self.new_window()
 
27
        self.browser.get("about:cache?device=disk")
 
28
        about_window = self.browser.window_handles[0]
 
29
 
 
30
    def filter_links(self,links):
 
31
        links              = [link for link in links if link.get_attribute("href") not in self.visited_links]
 
32
        self.visited_links += [link.get_attribute("href") for link in links]
 
33
        return links
 
34
 
 
35
    @raise_problem
 
36
    def do_q(self,*args):
 
37
        self.browser.close()
 
38
        return True
 
39
 
 
40
 
 
41
    def do_sync(self,*args):
 
42
        links = self.browser.find_elements_by_tag_name("a")
 
43
        links = self.filter_links(links)        
 
44
        for index,link in enumerate(links):
 
45
            href = link.get_attribute("href")
 
46
            print "got",href
 
47
            self.new_window()
 
48
            self.browser.get(href)
 
49
            in_file  = "/tmp/yassine%s.html" % index
 
50
            out_file = "/home/chaouche/MUSIQUE/cachemusic%s.mp3" % index
 
51
            html     = open(in_file,"w")
 
52
            html.write(self.browser.page_source)
 
53
            html.close()
 
54
            print commands.getoutput("groovy %s %s" % (in_file,out_file))
 
55
            self.close_window()
 
56
 
 
57
app = MyApp()
 
58
app.cmdloop()