def get_or_create_studentset(self, name, students, access_type='formal'): """ create studentset (if not exists) and return info dict """ try: find(name) except TwillAssertionError: # below code can be simplified assert access_type in ['formal', 'casual'] cmd.add_extra_header('Content-Type', 'application/xml') cmd.add_extra_header('charset', 'UTF-8') b = cmd.get_browser() mb = b._browser r = mb.open( '/py/teacher/studentset/new/ajax/?', urllib.urlencode( dict(setname=name, namelist=students, access_type=access_type, nameorder='dir', submit='Submit'))) # # return info # cmd.go('/py/teacher/studentset/list/') # id = int(soup().findAll(text=name)[0].parent['href'].split('/')[-2]) # return get_studentset_info(id) return dict(name=name, )
def login(username, password): t.add_extra_header("User-Agent", "*****@*****.**") t.go(host + "index.php/Special:UserLogin") t.fv("1", "wpName", username) t.fv("1", "wpPassword", password) t.submit("wpLoginAttempt")
def get_linkedin_viewer_count(username=None, password=None): from twill import get_browser from twill.commands import add_extra_header, go, fv, submit, reset_browser reset_browser() add_extra_header( 'User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36' ) go("https://www.linkedin.com/nhome/") #fv("login", 'session_password', 'LetsTryPrime') #fv("login", 'session_key', '*****@*****.**') fv("login", 'session_key', username) fv("login", 'session_password', password) submit() go('http://www.linkedin.com/wvmx/profile?trk=nav_responsive_sub_nav_wvmp') try: for i in get_browser().result.lxml\ .get_element_by_id('viewers_list-content')\ .iterchildren(): user_listing = simplejson.loads(i.text.replace('\\u002d', '-')) except Exception as e: log.err('Failed to extract user_listing from page: {error}'.format( error=e)) raise LinkedInFailure() try: current_count = user_listing['content']['wvmx_profile_viewers'][ 'viewersCount'] return current_count except KeyError: log.err('Profile view struct in unknown format: {user_listing}'.format( user_listing=user_listing)) raise LinkedInFailure()
def get_linkedin_viewer_count(username=None, password=None): from twill import get_browser from twill.commands import add_extra_header, go, fv, submit, reset_browser reset_browser() from twill.errors import TwillException add_extra_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36') go("https://www.linkedin.com/nhome/") # Added because LinkedIn login page no longer names the login form. b = get_browser() form_num = '' for n, f in enumerate(b.get_all_forms()): try: b.get_form_field(f, "session_key") b.get_form_field(f, "session_password") form_num = str(n+1) except TwillException: pass if form_num == '': log.err('Failed to parse LinkedIn login page - page format may have changed.') raise LinkedInFailure() #fv("login", 'session_password', 'LetsTryPrime') #fv("login", 'session_key', '*****@*****.**') fv(form_num, 'session_key', username) fv(form_num, 'session_password', password) submit() go('http://www.linkedin.com/wvmx/profile?trk=nav_responsive_sub_nav_wvmp') try: for i in get_browser().result.lxml\ .get_element_by_id('viewers_list-content')\ .iterchildren(): user_listing = simplejson.loads(i.text.replace('\\u002d','-')) except Exception as e: log.err('Failed to extract user_listing from page: {error}'.format(error=e)) raise LinkedInFailure() try: current_count = user_listing['content']['wvmx_profile_viewers']['viewersCount'] return current_count except KeyError: log.err('Profile view struct in unknown format: {user_listing}'.format(user_listing=user_listing)) raise LinkedInFailure()
def create_studentset(studentset_name, students='', access_type='formal'): """ add studentset and return info """ assert access_type in ['formal', 'casual'] cmd.add_extra_header('Content-Type', 'application/xml') cmd.add_extra_header('charset', 'UTF-8') b = cmd.get_browser() mb = b._browser r = mb.open( '/py/teacher/studentset/new/ajax/?', urllib.urlencode( dict(setname=studentset_name, namelist=students, access_type=access_type, nameorder='dir', submit='Submit'))) cmd.go('/py/teacher/studentset/list/') id = int( soup().findAll(text=studentset_name)[0].parent['href'].split('/')[-2]) return get_studentset_info(id)
def pobierzPlan(user, password): tablicaHTMLow = [] commands.add_extra_header( 'User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6') commands.clear_cookies() # Czyszczenie ciastek commands.reset_browser() #restart przegladarki commands.reset_output() #commands.config('use_BeautifulSoup', '0') commands.go("https://edukacja.pwr.wroc.pl/EdukacjaWeb/studia.do" ) # Przechodzimy do edukacji commands.showlinks() # DO USUNIECIA! Pokazuje linki commands.formclear('1') # Czysci formularz logowania commands.formvalue('1', 'login', user) # Podaje login commands.formvalue('1', 'password', password) # Podaje hasło commands.submit('0') # Klika zaloguj print("Linki po submit") # DO USUNIECIA! Pokazuje informacje commands.showlinks() # DO USUNIECIA! Pokazuje linki czyBledneLogowanie = sprawdzCzyBledneLogowanie( commands.show()) # Sprawdza czy na stronie wystapil blad czyLogowanieWInnejSesji = sprawdzCzyLogowanieWInnejSesji(commands.show()) if (czyBledneLogowanie == True): return "Bledne dane logowania" if (czyLogowanieWInnejSesji == True): return "zalogowany w innej sesji" commands.follow("zapisy.do") # Przechodzi linkiem na stronę zapisów # DO USUNIECIA! Pokazuje stronę po kliknięciu zapisy! print("Co po kliknieciu Zapisy") #commands.show() commands.showlinks() print "Forms:" formsy = commands.showforms() links = commands.showlinks() # Pobiera linki z danej strony control = None values = None select_options = None try: control = commands.browser.get_form("1").find_control('ineSluId', type="select") except: print("Nie ma selecta.") #control = commands.browser.get_form("1").find_control('ineSluId', type="select") if (control != None): # Jesli na stronie jest select values = pobierzElementySelect( commands.show()) # Pobieram parserem wartosci selecta select_options = utworzTabeleSelect( values) # Tworze nowy select podmieniajac stary select_attrs = {'id': 'some_id'} # Tworze atrybuty nowego selecta for v in values: # Lece petla po wartosciach selecta form = commands.get_browser().get_form("1") # Pobieram formularz ct = commands.browser.get_form("1").find_control( 'ineSluId', type="select") # Pobieram kontrolke z selectem add_select_to_form(form, 'ineSluId', select_attrs, select_options) # Tworze nowego selecta form.fixup( ) # Sprawdzam czy cos nowego nie zostało dodanego do form commands.showforms() commands.formvalue("1", ct.name, v.strip()) # Podaje wartosc dla selecta commands.submit('0') # Klikam submit html = pobierzZajecia(commands) # Pobieram zajecia commands.follow("zapisy.do") # Wracam do strony zapisów #commands.sleep(6) if ( html != "skreslony z kierunku" ): # Jesli funkcja zwrocila ze jest ktos skreslony to nie dodaje htmlu tablicaHTMLow.append( html ) # Jeli nie zwrocila takiego komunikatu to dodajemy ten html do tablicy else: html = pobierzZajecia( commands ) # Jesli nie ma selecta to pobieramy zajeci z tego kierunku i juz :D if (html != "skreslony z kierunku"): tablicaHTMLow.append(html) #print control.name, control.value, control.type #item = control.get("172748") #print item.name, item.selected, item.id, item.attrs #new_options #commands.formclear('1') # # #form = commands.get_browser().get_form("1") # #print('TO JEST TEN FORM:') #print(form) #print(len(form.controls)) # #notIsSelect = True #for ct in form.controls: # #print(ct) # if ct.name == 'ineSluId': # notIsSelect = False # print('JESTEM') # commands.sleep(3) # # select_attrs = {'id': 'some_id'} # values = pobierzElementySelect(commands.show()) # select_options = utworzTabeleSelect(values) # print(values) # print(select_options) # # # for v in values: # #form.fixup() # add_select_to_form(form, 'ineSluId', select_attrs, select_options) # form.fixup() # #ct.get(v).selected = True # print(ct) # print(form) # print(v) # commands.showforms() # # commands.formvalue("1", ct.name, v.strip()) # Podaje login # print("JEEDEFE") # commands.submit('0') # html = pobierzZajecia(commands) # commands.follow("zapisy.do") # print("JEEEEEEEEESSSSSTTTTTEEEEEMMM") # # commands.sleep(6) # if (html != "skreslony z kierunku"): # tablicaHTMLow.append(html) #ct.get(v).selected = False #for ct2 in form.controls: # if ct2.type == 'submit': # szukam wsrod niej tej co ma typ submit # commands.get_browser().clicked(form, ct2.attrs['name']) # klikam na ten przycisk # commands.get_browser().submit() #links = commands.showlinks() #commands.back() #commands.showforms() #return "no" #if (notIsSelect == True): # html = pobierzZajecia(commands) # if (html != "skreslony z kierunku"): # tablicaHTMLow.append(html) wyloguj(commands) commands.browser.clear_cookies() #usuwanie ciasteczek commands.reset_browser() #restart przegladarki commands.reset_output() return tablicaHTMLow
def pobierzPlan(user, password): tablicaHTMLow = [] commands.add_extra_header('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6') commands.clear_cookies() # Czyszczenie ciastek commands.reset_browser() #restart przegladarki commands.reset_output() #commands.config('use_BeautifulSoup', '0') commands.go("https://edukacja.pwr.wroc.pl/EdukacjaWeb/studia.do") # Przechodzimy do edukacji commands.showlinks() # DO USUNIECIA! Pokazuje linki commands.formclear('1') # Czysci formularz logowania commands.formvalue('1', 'login', user) # Podaje login commands.formvalue('1', 'password', password) # Podaje hasło commands.submit('0') # Klika zaloguj print("Linki po submit") # DO USUNIECIA! Pokazuje informacje commands.showlinks() # DO USUNIECIA! Pokazuje linki czyBledneLogowanie = sprawdzCzyBledneLogowanie(commands.show()) # Sprawdza czy na stronie wystapil blad czyLogowanieWInnejSesji = sprawdzCzyLogowanieWInnejSesji(commands.show()) if (czyBledneLogowanie == True): return "Bledne dane logowania" if (czyLogowanieWInnejSesji == True): return "zalogowany w innej sesji" commands.follow("zapisy.do") # Przechodzi linkiem na stronę zapisów # DO USUNIECIA! Pokazuje stronę po kliknięciu zapisy! print("Co po kliknieciu Zapisy") #commands.show() commands.showlinks() print "Forms:" formsy = commands.showforms() links = commands.showlinks() # Pobiera linki z danej strony control = None values = None select_options = None try: control = commands.browser.get_form("1").find_control('ineSluId', type="select") except: print("Nie ma selecta.") #control = commands.browser.get_form("1").find_control('ineSluId', type="select") if(control != None): # Jesli na stronie jest select values = pobierzElementySelect(commands.show()) # Pobieram parserem wartosci selecta select_options = utworzTabeleSelect(values) # Tworze nowy select podmieniajac stary select_attrs = {'id': 'some_id'} # Tworze atrybuty nowego selecta for v in values: # Lece petla po wartosciach selecta form = commands.get_browser().get_form("1") # Pobieram formularz ct = commands.browser.get_form("1").find_control('ineSluId', type="select") # Pobieram kontrolke z selectem add_select_to_form(form, 'ineSluId', select_attrs, select_options) # Tworze nowego selecta form.fixup() # Sprawdzam czy cos nowego nie zostało dodanego do form commands.showforms() commands.formvalue("1", ct.name, v.strip()) # Podaje wartosc dla selecta commands.submit('0') # Klikam submit html = pobierzZajecia(commands) # Pobieram zajecia commands.follow("zapisy.do") # Wracam do strony zapisów #commands.sleep(6) if (html != "skreslony z kierunku"): # Jesli funkcja zwrocila ze jest ktos skreslony to nie dodaje htmlu tablicaHTMLow.append(html) # Jeli nie zwrocila takiego komunikatu to dodajemy ten html do tablicy else: html = pobierzZajecia(commands) # Jesli nie ma selecta to pobieramy zajeci z tego kierunku i juz :D if (html != "skreslony z kierunku"): tablicaHTMLow.append(html) #print control.name, control.value, control.type #item = control.get("172748") #print item.name, item.selected, item.id, item.attrs #new_options #commands.formclear('1') # # #form = commands.get_browser().get_form("1") # #print('TO JEST TEN FORM:') #print(form) #print(len(form.controls)) # #notIsSelect = True #for ct in form.controls: # #print(ct) # if ct.name == 'ineSluId': # notIsSelect = False # print('JESTEM') # commands.sleep(3) # # select_attrs = {'id': 'some_id'} # values = pobierzElementySelect(commands.show()) # select_options = utworzTabeleSelect(values) # print(values) # print(select_options) # # # for v in values: # #form.fixup() # add_select_to_form(form, 'ineSluId', select_attrs, select_options) # form.fixup() # #ct.get(v).selected = True # print(ct) # print(form) # print(v) # commands.showforms() # # commands.formvalue("1", ct.name, v.strip()) # Podaje login # print("JEEDEFE") # commands.submit('0') # html = pobierzZajecia(commands) # commands.follow("zapisy.do") # print("JEEEEEEEEESSSSSTTTTTEEEEEMMM") # # commands.sleep(6) # if (html != "skreslony z kierunku"): # tablicaHTMLow.append(html) #ct.get(v).selected = False #for ct2 in form.controls: # if ct2.type == 'submit': # szukam wsrod niej tej co ma typ submit # commands.get_browser().clicked(form, ct2.attrs['name']) # klikam na ten przycisk # commands.get_browser().submit() #links = commands.showlinks() #commands.back() #commands.showforms() #return "no" #if (notIsSelect == True): # html = pobierzZajecia(commands) # if (html != "skreslony z kierunku"): # tablicaHTMLow.append(html) wyloguj(commands) commands.browser.clear_cookies() #usuwanie ciasteczek commands.reset_browser() #restart przegladarki commands.reset_output() return tablicaHTMLow
def runTest(self): """ The twill test case is useful for emulating a user using HTML forms and clicking on buttons. Note we cannot connect to the twill server using urllib2 or any other third party client. Also, we cannot verify the header content in a twill response. """ FILTER_FORM = 2 logger = logging.getLogger("TwillTestCase") url = URL_BASE twill_quiet() logger.info('accessing ' + url) tc.go(url) tc.find("html") # sanity check that this is a html request. url = URL_BASE + 'filter/' logger.info('accessing ' + url) tc.go(url) tc.formvalue(FILTER_FORM,'tag','esnet') tc.submit(9) #simulates a click on the "create filter" button tc.find("html") tc.find('<table class="sortable">') #a line in event.list.html tc.notfind('experiment') #should be filtered out tc.go(url) tc.formvalue(FILTER_FORM,'display','timeline') tc.submit(9) #click should direct us to timeline. tc.find("html") tc.notfind('<table class="sortable">') tc.go(url) tc.formvalue(FILTER_FORM,'display','json') tc.submit(9) #click should give us JSON object. tc.find('"pk": 1') #part of JSON string. tc.notfind("html") tc.go(url) tc.formvalue(FILTER_FORM,'name','upgrade') tc.formvalue(FILTER_FORM,'search','www.es.net') tc.formvalue(FILTER_FORM,'description','infrastructure') tc.submit(9) tc.find('html') tc.find('www.es.net') #part of the upgrade event. #Set headers to contain only the json mime tc.clear_extra_headers() tc.add_extra_header('Accept',JSON_MIME) url = URL_BASE logger.info('accessing ' + url) tc.go(url) logger.debug('JSON data:' + tc.show()) tc.notfind("html") tc.find('"name": "experiment"') #data from the fixture formatted by default serializer tc.find('"tags": "esnet"') url = URL_BASE + '1/' logger.info('accessing ' + url) tc.go(url) tc.notfind("html") tc.find('"name": "experiment"') tc.notfind('"tags": "esnet"')