def dotransform(request, response): # Nespose API session login session = nexlogin() # Nexpose site creation sitename = datetime.today().strftime("%Y%m%d-%H%M%S") + '-MaltegoSite' newsite = host_site(sitename, request.value) nexsite = sitesave(session, newsite) resxml = ET.fromstring(nexsite) siteid = resxml.attrib.get('site-id') progress(10) if resxml.attrib.get('success') == '1': # Nexpose Scan Site launchscan = sitescan(session, siteid) launchres = ET.fromstring(launchscan) progress(25) if launchres.attrib.get('success') == '1': for child in launchres: scanid = child.attrib.get('scan-id') status = scanstatus(session, scanid) statusxml = ET.fromstring(status) progress(50) while statusxml.attrib.get('status') == 'running': sleep(5) status = scanstatus(session, scanid) statusxml = ET.fromstring(status) continue progress(100) response += NexposeSite(sitename, siteid=siteid, scanid=scanid, targetip=request.value) return response nexlogout(session)
def ampache_http_request(action, add=None, filter=None, limit=5000, offset=0): thisURL = build_ampache_url(action, filter=filter, add=add, limit=limit, offset=offset) req = urllib2.Request(thisURL) response = urllib2.urlopen(req) contents = response.read() contents = contents.replace("\0", "") tree = ET.fromstring(contents) response.close() if tree.findtext("error"): errornode = tree.find("error") if errornode.attrib["code"] == "401": tree = AMPACHECONNECT() thisURL = build_ampache_url(action, filter=filter, add=add, limit=limit, offset=offset) req = urllib2.Request(thisURL) response = urllib2.urlopen(req) contents = response.read() tree = ET.fromstring(contents) response.close() return tree
def Search(self, srcr, keywords, type, list, lock, message_queue, page='', total_pages=''): import elementtree.ElementTree as ET import urllib from entertainment.net import Net net = Net(cached=False) keywords = self.CleanTextForSearch(keywords) import re from entertainment import odict search_dict = odict.odict({'search_keywords':keywords}) name = urllib.urlencode(search_dict) id_search = 'http://services.tvrage.com/myfeeds/search.php?key=ag6txjP0RH4m0c8sZk2j&show='+name content = net.http_GET(id_search).content try: root = ET.fromstring(content) except: content = common.CleanText2(content, True, True) root = ET.fromstring(content) shows = root.findall('.//show') for show in shows: show_id = show.find('showid').text show_url = 'http://services.tvrage.com/myfeeds/episode_list.php?key=ag6txjP0RH4m0c8sZk2j&sid='+str(show_id) show_name = show.find('name').text show_year = show.find('started').text self.AddContent(list, srcr, common.mode_Content, show_name, '', 'tv_seasons', url=show_url, name=show_name, year=show_year)
def configure(self): if hasattr(self, 'access_token'): return h = self.http() h.fetch_request_token() url = h.authorize_token() raw_input("Visit this URL to authorize your account:\n\n<%s>\n\nthen press Enter: " % url) self.access_token = h.fetch_access_token() # Confirm that the access token works. h = self.http(self.access_token) response, content = h.request("http://api.netflix.com/users/current") if response.status != 200: raise ValueError('Could not authorize Netflix account') # Find the real user info. culink = ElementTree.fromstring(content) userlink = culink.find('link') userhref = userlink.get('href') # Look for the user name. response, content = h.request(userhref) if response.status != 200: raise ValueError('Could not fetch Netflix account') userdoc = ElementTree.fromstring(content) userid = userdoc.find('user_id').text firstname = userdoc.find('first_name').text lastname = userdoc.find('last_name').text self.userid = userid self.name = ' '.join((firstname, lastname))
def dotransform(request, response): # Nespose API session login session = nexlogin() # Nexpose site creation sitename = datetime.today().strftime("%Y%m%d-%H%M%S") + '-MaltegoSite' newsite = host_site(sitename, request.value) nexsite = sitesave(session, newsite) resxml = ET.fromstring(nexsite) siteid = resxml.attrib.get('site-id') progress(10) if resxml.attrib.get('success') == '1': # Nexpose Scan Site launchscan = sitescan(session, siteid) launchres = ET.fromstring(launchscan) progress(25) if launchres.attrib.get('success') == '1': for child in launchres: scanid = child.attrib.get('scan-id') status = scanstatus(session, scanid) statusxml = ET.fromstring(status) progress(50) while statusxml.attrib.get('status') == 'running': sleep(5) status = scanstatus(session, scanid) statusxml = ET.fromstring(status) continue progress(100) response += NexposeSite( sitename, siteid=siteid, scanid=scanid, targetip=request.value) return response nexlogout(session)
def login(self): if not self.username and not self.password: return True # fall back to free account elif self.username and self.password and len(self.username)>0 and len(self.password)>0: self.info('Login user=%s, pass=*****' % self.username) # get salt headers,req = self._create_request('',{'username_or_email':self.username}) data = util.post(self._url('api/salt/'),req,headers=headers) xml = ET.fromstring(data) if not xml.find('status').text == 'OK': self.error('Server returned error status, response: %s' % data) return False salt = xml.find('salt').text # create hashes password = hashlib.sha1(md5crypt(self.password, salt)).hexdigest() digest = hashlib.md5(self.username + ':Webshare:' + self.password).hexdigest() # login headers,req = self._create_request('',{'username_or_email':self.username,'password':password,'digest':digest,'keep_logged_in':1}) data = util.post(self._url('api/login/'),req,headers=headers) xml = ET.fromstring(data) if not xml.find('status').text == 'OK': self.error('Server returned error status, response: %s' % data) return False self.token = xml.find('token').text self.info('Login successfull') return True return False
def Search(self, srcr, keywords, type, list, lock, message_queue, page='', total_pages=''): import elementtree.ElementTree as ET import urllib from entertainment.net import Net net = Net() keywords = self.CleanTextForSearch(keywords) import re from entertainment import odict search_dict = odict.odict({'search_keywords':keywords}) name = urllib.urlencode(search_dict) id_search = 'http://services.tvrage.com/myfeeds/search.php?key=ag6txjP0RH4m0c8sZk2j&show='+name content = net.http_GET(id_search).content try: root = ET.fromstring(content) except: content = common.CleanText2(content, True, True) root = ET.fromstring(content) shows = root.findall('.//show') for show in shows: show_id = show.find('showid').text show_url = 'http://services.tvrage.com/myfeeds/episode_list.php?key=ag6txjP0RH4m0c8sZk2j&sid='+str(show_id) show_name = show.find('name').text show_year = show.find('started').text self.AddContent(list, srcr, common.mode_Content, show_name, '', 'tv_seasons', url=show_url, name=show_name, year=show_year)
def getServers(self): localServers = dict() remoteServers = dict() foundServer = False if self.isAuthenticated(): url = MyPlexService.SERVERS_URL % self.authenticationToken util.logDebug("Finding servers via: "+url) data = util.Http().Get(url) if data: tree = ElementTree.fromstring(data) for child in tree: host = child.attrib.get("address", "") port = child.attrib.get("port", "") localAddresses = child.attrib.get("localAddresses", "") accessToken = child.attrib.get("accessToken", "") machineIdentifier = child.attrib.get("machineIdentifier", "") local = child.attrib.get("owned", "0") sourceTitle = child.attrib.get("sourceTitle", "") util.logInfo("MyPlex found server %s:%s" % (host,port)) foundServer = True server = None if local == "1": #Try the local addresses #TODO: Similiar code exists in the server and this is a bit convoluted.... if localAddresses: localAddresses = localAddresses.split(',') util.logInfo("--> Resolving local addresses") resolved = False for addr in localAddresses: http = util.Http() util.logDebug("--> Trying local address %s:32400" % addr) data = http.Get("http://"+addr+":32400/?X-Plex-Token="+accessToken) if http.GetHttpResponseCode() == -1: data = http.Get("https://"+addr+":32400/?X-Plex-Token="+accessToken) if data: tree = ElementTree.fromstring(data) localMachineIdentifier = tree.attrib.get("machineIdentifier", "") if localMachineIdentifier == machineIdentifier: util.logInfo("--> Using local address %s:32400 instead of remote address" % addr) server = PlexServer(addr, "32400", accessToken) resolved = True break if not resolved: util.logInfo("--> Using remote address %s unable to resolve local address" % host) server = PlexServer(host, port, accessToken) if server is None or not server.isValid(): continue localServers[machineIdentifier] = server else: #Remote server found server = PlexServer(host, port, accessToken, sourceTitle) remoteServers[machineIdentifier] = server return localServers, remoteServers, foundServer
def pollGenerateReport (reportRequestId): soapStr = """<?xml version="1.0" encoding="utf-8"?> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <h:ApplicationToken i:nil="true" xmlns:h="https://adcenter.microsoft.com/v8" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" /> <h:CustomerAccountId i:nil="true" xmlns:h="https://adcenter.microsoft.com/v8" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" /> <h:CustomerId i:nil="true" xmlns:h="https://adcenter.microsoft.com/v8" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" /> <h:DeveloperToken xmlns:h="https://adcenter.microsoft.com/v8">%s</h:DeveloperToken> <h:Password xmlns:h="https://adcenter.microsoft.com/v8">%s</h:Password> <h:UserName xmlns:h="https://adcenter.microsoft.com/v8">%s</h:UserName> </s:Header> <s:Body> <PollGenerateReportRequest xmlns="https://adcenter.microsoft.com/v8"> <ReportRequestId>%s</ReportRequestId> </PollGenerateReportRequest> </s:Body> </s:Envelope>""" % (developertoken, password, username, reportRequestId) # Create the Web service client, and then add the required headers. _service = httplib.HTTPS(host) _service.putrequest("POST", reportProxy) _service.putheader("Accept","text/xml") _service.putheader("Accept","multipart/*") _service.putheader("Content-type", "text/xml; charset=\"UTF-8\"") _service.putheader("Content-length", "%d" % len(soapStr)) _service.putheader("SOAPAction", "PollGenerateReport") _service.putheader("HOST", str(host)) _service.endheaders() # Execute the Web service request. _service.send(soapStr) # Get the response message and results. statuscode, statusmessage, header = _service.getreply() res = _service.getfile().read() response = None if statusmessage == "OK": response = ET.fromstring(res) else: # The method call failed. print soapStr print "PollGenerateReport failed.\n" print "Status Code: ", statuscode, "\n" print "Header: ", header, "\n" print res faultTree = ET.fromstring(res) print faultTree.findtext(".//faultcode"), " ", \ faultTree.findtext(".//faultstring") return response if response else None
def login(self): if not self.username or not self.password: self.logout() return True # fall back to free account elif self.token is not None: if self.userData() is not False: return True self.token = None if self.username and self.password and len(self.username) > 0 and len( self.password) > 0: self.logout() util.info('[SC] Login user=%s, pass=*****' % self.username) try: # get salt headers, req = self._create_request( '', {'username_or_email': self.username}) data = post(self._url('api/salt/'), req, headers=headers) xml = ET.fromstring(data) if not xml.find('status').text == 'OK': util.error( '[SC] Server returned error status, response: %s' % data) return False salt = xml.find('salt').text # create hashes password = hashlib.sha1( md5crypt(self.password.encode('utf-8'), salt.encode('utf-8'))).hexdigest() digest = hashlib.md5(self.username + ':Webshare:' + self.password).hexdigest() # login headers, req = self._create_request( '', { 'username_or_email': self.username, 'password': password, 'digest': digest, 'keep_logged_in': 1 }) data = post(self._url('api/login/'), req, headers=headers) xml = ET.fromstring(data) if not xml.find('status').text == 'OK': self.clearToken() util.error( '[SC] Server returned error status, response: %s' % data) return False self.saveToken(xml.find('token').text) try: util.cache_cookies(None) except: pass util.info('[SC] Login successfull') return True except Exception, e: util.info('[SC] Login error %s' % str(e))
def _get_todo_list(id): xml = bc.todo_list(id) todo_list = {'name': ET.fromstring(xml).find('name').text, 'items': []} items = ET.fromstring(xml).findall('todo-items/todo-item') for item in items: todo_list['items'].append(item.find("content").text) return todo_list
def getServers(self): localServers = dict() remoteServers = dict() foundServer = False if self.isAuthenticated(): url = MyPlexService.SERVERS_URL % self.authenticationToken util.logDebug("Finding servers via: "+url) data = mc.Http().Get(url) if data: tree = ElementTree.fromstring(data) for child in tree: host = child.attrib.get("address", "") port = child.attrib.get("port", "") localAddresses = child.attrib.get("localAddresses", "") accessToken = child.attrib.get("accessToken", "") machineIdentifier = child.attrib.get("machineIdentifier", "") local = child.attrib.get("owned", "0") util.logInfo("MyPlex found servers %s:%s" % (host,port)) foundServer = True server = None if local == "1": #Try the local addresses if localAddresses: localAddresses = localAddresses.split(',') util.logInfo("--> Resolving local addresses") resolved = False for addr in localAddresses: data = mc.Http().Get("http://"+addr+":32400") util.logDebug("--> Trying local address %s:32400" % addr) if data: tree = ElementTree.fromstring(data) localMachineIdentifier = tree.attrib.get("machineIdentifier", "") if localMachineIdentifier == machineIdentifier: util.logInfo("--> Using local address %s:32400 instead of remote address" % addr) server = PlexServer(addr, "32400", accessToken) resolved = True server.isLocal = True break if not resolved: util.logInfo("--> Using remote address unable to resolve local address" % addr) server = PlexServer(host, port, accessToken) if server is None or not server.isAuthenticated(): continue localServers[machineIdentifier] = server else: server = PlexServer(host, port, accessToken) remoteServers[machineIdentifier] = server return localServers, remoteServers, foundServer
def get_info_for_user(username, server, admin_info): resp, content = admin_post("%s/people/%s/info.xml" % (server, username), *admin_info) #404 means the user isn't fully initialized. if resp['status'] == '404': raise ProjectNotFoundError if resp['status'] != '200': if resp['status'] == '302': # redirect probably means auth failed extra = '; did your admin authentication fail?' elif resp['status'] == '400': # Probably Zope is gone extra = '; is Zope started?' else: extra = '' raise ValueError("Error retrieving user %s: status %s%s" % (project, resp['status'], extra)) tree = etree.fromstring(content) info = {} for el in tree: info[el.tag] = el.text return info
def makeFromMimeData(mimeData): ''' Creates a new ClusterNode object from the given QtCore.QMimeData object. ''' assert mimeData.hasFormat(ClusterNode.sMimeType) node_data = mimeData.data(ClusterNode.sMimeType) data_stream = QtCore.QDataStream(node_data, QtCore.QIODevice.ReadOnly) # The MIME data for an ensemble node contains its XML element, its # state identifier, and its IP address. All of these are stored as # QtCore.QString instances. xml_elt = QtCore.QString() state = QtCore.QString() ip_addr = QtCore.QString() data_stream >> xml_elt >> state >> ip_addr if len(str(ip_addr)) == 0: ip_addr = None else: ip_addr = str(ip_addr) return ClusterNode(ET.fromstring(str(xml_elt)), state.toInt()[0], ip_addr)
def CreateClassFromXMLString(target_class, xml_string, string_encoding=None): """Creates an instance of the target class from the string contents. Args: target_class: class The class which will be instantiated and populated with the contents of the XML. This class must have a _tag and a _namespace class variable. xml_string: str A string which contains valid XML. The root element of the XML string should match the tag and namespace of the desired class. string_encoding: str The character encoding which the xml_string should be converted to before it is interpreted and translated into objects. The default is None in which case the string encoding is not changed. Returns: An instance of the target class with members assigned according to the contents of the XML - or None if the root XML tag and namespace did not match those of the target class. """ encoding = string_encoding or XML_STRING_ENCODING if encoding and isinstance(xml_string, unicode): xml_string = xml_string.encode(encoding) tree = ElementTree.fromstring(xml_string) return _CreateClassFromElementTree(target_class, tree)
def post(self, body): self.headers["Content-Length"] = "%d" % len(body) self.headers["Content-Type"] = "application/soap+xml;charset=UTF-8" self.connection.request("POST", self.url, body=body, headers=self.headers) response = self.connection.getresponse() if response.status == 401: raise HyperVAuthFailed("Authentication failed") if response.status != 200: data = response.read() xml = ElementTree.fromstring(data) errorcode = xml.find( './/{http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/MSFT_WmiError}error_Code' ) # Suppress reporting of invalid namespace, because we're testing # both old and new namespaces that HyperV uses if errorcode is None or errorcode.text != '2150858778': self.logger.debug("Invalid response (%d) from Hyper-V: %s", response.status, data) raise HyperVException( "Communication with Hyper-V failed, HTTP error: %d" % response.status) if response is None: raise HyperVException("No reply from Hyper-V") return response
def get_xml_history(self): """Returns a parsed xml object corresponding to the history""" self.home() tc.go('./history?template=history.xml' ) xml = self.last_page() tree = ElementTree.fromstring(xml) return tree
def resolve(self, ident, download_type=None): params = {'ident': ident, 'wst': self.token} if None is not download_type: params.update({ 'download_type': download_type, 'device_uuid': getSetting('uid'), 'device_res_x': infoLabel('System.ScreenWidth'), 'device_res_y': infoLabel('System.ScreenHeight'), }) headers, req = self._create_request('/', params) util.info(headers) util.info(req) try: data = post(self._url('api/file_link/'), req, headers=headers, output="content") xml = ET.fromstring(data) if not xml.find('status').text == 'OK': self.clearToken() util.error('[SC] Server returned error status, response: %s' % data) raise ResolveException(xml.find('message').text) return xml.find('link').text except Exception as e: self.clearToken() raise ResolveException(e)
def get_image_list(self, media_id): xml_url = self.url % (self.api_key,media_id) log('API: %s ' % xml_url) image_list = [] data = get_xml(xml_url) tree = ET.fromstring(data) for imagetype in self.imagetypes: imageroot = imagetype + 's' for images in tree.findall(imageroot): for image in images: info = {} info['id'] = image.get('id') info['url'] = urllib.quote(image.get('url'), ':/') info['preview'] = urllib.quote(image.get('preview'), ':/') info['type'] = imagetype info['language'] = image.get('lang') info['likes'] = image.get('likes') # Create Gui string to display info['generalinfo'] = '%s: %s | %s: %s ' %( __localize__(32141), info['language'], __localize__(32143), info['likes'] ) if info: image_list.append(info) if image_list == []: raise NoFanartError(media_id) else: return image_list
def get_task_by_name(self, name, completed='both'): completed = string.lower(completed) completed = { 'both': 'yes', 'yes': 'only', 'no': 'no', 'true': 'only', 'false': 'no' }[completed] url = "%s/tasks?%s&show_completed=%s" % \ (self._get_base_url(), self._get_url_params(), completed) self.__conn.request("GET", url, "", {"Accept": "application/xml"}) response = self.__conn.getresponse() data = response.read() if not response.status == 200: return None for task in ET.fromstring(data).findall("task"): if task.findtext("name") == name: return self._parse_task(task) return None
def categories(self): def media_tag(tag): return str(ET.QName('http://search.yahoo.com/mrss/', tag)) result = [] item = self.dir_item() item['title'] = '[COLOR=yellow][B]Video archÃv[/B][/COLOR]' item['url'] = '#relacie' item['img'] = 'https://www.mtr.sk/video/12750_big.jpg' item['plot'] = 'VideoarchÃv:\n' + (', '.join( list(self.data_videoarchiv.get('relacie').keys())[1:])) item['menu'] = {'$30070': {'list': item['url'], 'action-type': 'list'}} result.append(item) item = self.video_item() item['title'] = '[COLOR=grey]SledovaÅ¥ online[/COLOR]' item['url'] = 'rtmp://kdah.mtr.sk/oflaDemo/livestream live=true' item['img'] = 'https://www.mtr.sk/video/10942_big.jpg' item['plot'] = 'Sleduj online Mestská TV Ružomberok' result.append(item) xml = ET.fromstring(util.request(self.base_url)) for i in xml.find('channel').findall('item'): item = self.video_item() #item['title'] = '%s (%s)' % (i.find('title').text,i.find('description').text) item['title'] = i.find('title').text plot = i.find('description').text if plot: item['plot'] = plot item['img'] = i.find(media_tag('thumbnail')).attrib['url'] item['url'] = i.find(media_tag('content')).attrib['url'] result.append(item) return result
def returnGoogle() : xmlPath = os.path.join( xmlUserDir, "google.xml" ) xmlFile=open(xmlPath,"r") txt = xmlFile.read() xmlFile.close() tree = ElementTree.fromstring(txt) channel = tree.find("channel") bookmarks = channel.findall("item") googleDico = {} googleMain = [] for bookmark in bookmarks: title = bookmark.findtext("title") url = bookmark.findtext("link") guid = bookmark.findtext("{http://www.google.com/history/}bkmk_id") #print guid tags = bookmark.findall("{http://www.google.com/history/}bkmk_label") googleItem = {"title" : title , "url" : url , "guid" : guid} if len(tags) == 0 : googleMain.append(googleItem) else : for tag in tags : if googleDico.has_key(tag.text) : googleItems = googleDico[tag.text] googleItems.append(googleItem) else : googleItems = [] googleItems.append(googleItem) googleDico[tag.text] = googleItems #print googleDico #print googleMain return googleDico, googleMain
def list(self,url): result = [] headers,req = self._create_request(url,{'what':'','offset':0,'limit':25,'category':'','sort':'','wst':self.token}) data = util.post(self._url('api/search/'),req,headers=headers) xml = ET.fromstring(data) if not xml.find('status').text == 'OK': self.error('Server returned error status, response: %s' % data) return [] total = int(xml.find('total').text) for file in xml.findall('file'): item = self.video_item() item['title'] = file.find('name').text item['url'] = 'ident=%s' % file.find('ident').text size = int(file.find('size').text) item['size'] = '%d MB' % (int(size)/1024/1024) img = file.find('img').text if img: item['img'] = self._url(img) self._filter(result,item) listed = int(req['limit']) + int(req['offset']) if total > listed: req['offset'] = listed item = self.dir_item() item['type'] = 'next' item['url'] = urllib.urlencode(req) result.append(item) return result
def book_details(isbn13, goodreads, cache={}): book = cache.get(isbn13, None) print("Fetching details for: "+isbn13) if not book: response = goodreads.get("https://www.goodreads.com/book/isbn", params={'format': 'xml', 'isbn': isbn13}) tree = ElementTree.fromstring(response.text.encode("utf-8")).getchildren()[1] work = tree.find("work") title = None if work is not None: title = work.findtext("original_title") book = { 'created': datetime.datetime.utcnow(), 'title': title or tree.findtext("title"), 'isbn': tree.findtext("isbn"), 'isbn13': tree.findtext("isbn13"), 'goodreads_id': tree.findtext("id"), 'num_pages': int(tree.findtext("num_pages") or '0'), 'average_rating': float(tree.findtext("average_rating") or '0'), 'author': tree.find("authors").getchildren()[0].findtext("name") } mongo_book_details.insert(book) add_sfpl_entries(book) add_availability(book) return book
def setXml(self, xml): """\ Read the xml config file from a string """ self._xml = ElementTree.fromstring(xml) self._loadSettings()
def main(): for i in range(len(sites)): site = sites[i] + "das/dsn" trackurl = sites[i] + "hgTracks?" builds = [] try: page = urllib.urlopen(site) except: print "#Unable to connect to " + site continue text = page.read() try: tree = ElementTree.fromstring(text) except: print "#Invalid xml passed back from " + site continue print "#Harvested from", site for dsn in tree: build = dsn.find("SOURCE").attrib['id'] builds.append(build) build_dict = {} for build in builds: build_dict[build] = 0 builds = build_dict.keys() yield [names[i], trackurl, builds]
def update_task(self, task): """ Updates the given task or creates it if the task ID is 0 """ create = task.id == 0 xml = self._serialise_task(task) method = ['PUT', 'POST'][create] if create: url = "%s/tasks?%s" % \ (self._get_base_url(), self._get_url_params()) else: url = "%s/tasks/%s?%s" % \ (self._get_base_url(), task.id, self._get_url_params()) headers = { "Accept": "application/xml", "Content-Type": "application/xml" } self.__conn.request(method, url, xml, headers) response = self.__conn.getresponse() data = response.read() if not response.status == 200: raise Exception("Could not update/create task."\ " Response was [%s]: %s" % (response.status, data)) return self._parse_task(ET.fromstring(data))
def store(self, event=None): # get the verification URL v_url = self.verification_url # get the copyright information fields license_xml = etree.fromstring(api.getApp().license_doc) license = api.findField('license', api.getApp().items[0]) license_name = license_xml.find('license-name').text year = api.findField('year', api.getApp().items[0]) holder = api.findField('holder', api.getApp().items[0]) for item in api.getApp().items: # adapt the item to IEmbeddable if possible embeddable = zope.component.getGlobalSiteManager().getAdapters( [item,], IEmbeddable) if embeddable: for e in embeddable: # take e[1] since getAdapters returns a list of tuples -- # (name, adapter) e[1].embed(license, v_url, year, holder) # calculate the hash of each item # XXX see triple-x comment in store method of ia.py self.rdf = cctagutils.rdf.generate_rdfa( [n.getIdentifier() for n in api.getApp().items[1:]], license_name, license, v_url)
def get_time_entries(self, range_start=None, range_end=None): result = [] # Prepare range filter filters = [] if range_start: filters.append("range_start=%s" % self._format_date(range_start)) if range_end: filters.append("range_end=%s" % self._format_date(range_end)) filter_str = '&'.join(filters) if filter_str: filter_str = '&' + filter_str url = "%s/time_entries?%s%s" % \ (self._get_base_url(), self._get_url_params(), filter_str) self.__conn.request("GET", url, "", {"Accept": "application/xml"}) response = self.__conn.getresponse() data = response.read() if not response.status == 200: return None for entry in ET.fromstring(data).findall("time-entry"): result.append(self._parse_time_entry(entry)) return result
def createVad(basePath, sticker, s): # write a clean text warning vadWriteRow( s, 'VAD', 'This file consists of binary data and should not be touched by hands!' ) # Write the final sticker contents vadWriteRow(s, 'STICKER', sticker) # Write all the files defined in the sticker stickerTree = ET.fromstring(sticker) for f in stickerTree.findall("resources/file"): resType = f.get("type") resSource = f.get("source") targetUri = f.get("target_uri") sourceUri = f.get("source_uri") if resSource == "dav": print >> sys.stderr, "Cannot handle DAV resources" exit(1) if verbose: print >> sys.stderr, "Packing file %s as %s" % (sourceUri, targetUri) vadWriteFile(s, targetUri, sourceUri) # Write the md5 hash vadWriteRow(s, 'MD5', ctx.hexdigest())
def _logon(self): """ Get an access token and user id """ # Lazy operation if self.__token and self.__userid: return (self.__token, self.__userid) # Parameter checking if not self.__username or not self.__apikey: raise Exception("Invalid username or API key") # Build request request = '<request><user><email>%s</email>\ <password>%s</password></user><api-key>%s</api-key>\ </request>' % (self.__username, self.__password, self.__apikey) headers = { "Accept": "application/xml", "Content-Type": "application/xml" } self.__conn.request("POST", "/users/token", request, headers) response = self.__conn.getresponse() data = response.read() if response.status != 200: raise Exception("Server returned error: %s)" % data) result = ET.fromstring(data) self.__token = result.findtext("access-token") self.__userid = result.findtext("user-id") return (self.__token, self.__userid)
def doPID(self, pid, size): try: content = self.getContent('http://*****:*****@dsid="__tn_' + size + '"]'): try: return self.getContent('http://*****:*****@dsid="' + size.capitalize() + '"]'): try: return self.getContent('http://*****:*****@dsid="Thumbnail"]'): try: return self.getContent('http://localhost:8180/fedora/get/' + pid + '/Thumbnail') except urllib2.HTTPError: print "" except urllib2.HTTPError: print "" return None
def getVideoUrl(vidSrc, vidID): """Resolve the video url based on source and video id Arguments: vidSrc -- A string containing the video's source: 'vimeo' or 'ustream' vidID -- A string containing the video's ID number Returns: videoUrl -- A string containing the url of the video to be played""" # check if video is from Vimeo if vidSrc == 'vimeo': # construct path to play video using vimeo plugin videoUrl = 'plugin://plugin.video.vimeo?action=play_video&videoid=' + vidID # check if video is from ustream elif vidSrc == 'ustream': # construct url to use for API call apiUrl = 'http://api.ustream.tv/xml/video/%s/getInfo?key=A5379FCD5891A9F9A029F84422CAC98C' % vidID # initialise the tree object using the returned data so it can be parsed tree = ET.fromstring(cache.cacheFunction(getHtml, apiUrl)) video = tree.find('results') # get episode url videoUrl = video.findtext('mp4Url') # check if video url was found if not videoUrl: # check alternate field for video url videoUrl = video.findtext('liveHttpUrl') #return video url return videoUrl
def Invoke_GetSummaryInformation(self, namespace): ''' Get states of all virtual machines present on the system and return dict where `ElementName` is key and `virt.GUEST.STATE_*` is value. ''' data = self.generator.getSummaryInformationXML(namespace) response = self.post(data) d = response.read() xml = ElementTree.fromstring(d) if xml.tag != "{%(s)s}Envelope" % self.generator.namespaces: raise HyperVException("Wrong reply format") responses = xml.findall( "{%(s)s}Body/{%(vsms)s}GetSummaryInformation_OUTPUT" % { 's': self.generator.namespaces['s'], 'vsms': self.generator.vsms_namespace % { 'ns': namespace } }) if len(responses) < 0: raise HyperVException("Wrong reply format") info = {} si_namespace = self.generator.si_namespace % {'ns': namespace} for node in responses[0].getchildren(): if 'SummaryInformation' in node.tag: elementName = node.find("{%(si)s}ElementName" % { 'si': si_namespace }).text enabledState = node.find("{%(si)s}EnabledState" % { 'si': si_namespace }).text info[elementName] = ENABLED_STATE_TO_GUEST_STATE.get( enabledState, virt.Guest.STATE_UNKNOWN) return info
def switchUser(self, userId, pin): if not self.authenticationToken: return False url = MyPlexService.SWITCHUSER_URL % (userId, pin) http = self.plexManager.buildPlexHttpRequest() http.SetHttpHeader('X-Plex-Token',self.authenticationToken) data = http.Post(url) if not data: if http.ResultUnauthorised() and pin != '': util.logDebug("User switch failed PIN invalid"); return PlexManager.ERR_USER_PIN_FAILED util.logDebug("Error failed to access users %s HttpCode: %d" % (url, http.code)); return PlexManager.ERR_USER_OTHER tree = ElementTree.fromstring(data) token = None for child in tree: if child.tag == 'authentication-token': token = child.text break if token is None: return PlexManager.ERR_USER_OTHER #Set usertoken self.userToken = token #reload myplex servers self.loadServers() return PlexManager.SUCCESS
def get_title_of_arxiv_pubid(arxiv_pubid): def get_title_from_arxiv_xml(tree, tags): try: tag = tags.popleft() except IndexError: return tree.text for descendant in tree: if descendant.tag.endswith(tag): return get_title_from_arxiv_xml(descendant, tags) return None xml_referer = get_xml_referer_of_arxiv_pubid(arxiv_pubid) if xml_referer is None: return arxiv_pubid try: fxml = urlopen(xml_referer) xml = fxml.read() fxml.close() root = ET.fromstring(xml) except HTTPError: return arxiv_pubid title = get_title_from_arxiv_xml( root, deque(['GetRecord', 'record', 'metadata', 'dc', 'title'])) if title: return title return arxiv_pubid
def nexposeServiceVer(report): rptxml = ET.fromstring(report) serverlist = [] for service in rptxml.findall( ".//nodes/node/endpoints/endpoint/services/service"): for ver in service[0]: try: if 'product' in ver.attrib and 'version' in ver.attrib and 'certainty' in ver.attrib: product, version, certainty = ver.attrib[ 'product'], ver.attrib['version'], ver.attrib[ 'certainty'] serverdic = { service.attrib['name']: [product, version, certainty] } serverlist.append(serverdic) elif 'product' in ver.attrib and 'version' in ver.attrib: product, version = ver.attrib['product'], ver.attrib[ 'version'] serverdic = {service.attrib['name']: [product, version]} serverlist.append(serverdic) elif 'product' in ver.attrib: product = ver.attrib['product'] serverdic = {service.attrib['name']: [product]} serverlist.append(serverdic) elif 'version' in ver.attrib: version = ver.attrib['version'] serverdic = {service.attrib['name']: [version]} serverlist.append(serverdic) else: pass except Exception, e: pass
def get_users_for_project(project, server, admin_info): resp, content = admin_post("%s/projects/%s/members.xml" % (server, project), *admin_info) #404 means the project isn't fully initialized. if resp['status'] == '404': raise ProjectNotFoundError if resp['status'] != '200': if resp['status'] == '302': # redirect probably means auth failed extra = '; did your admin authentication fail?' elif resp['status'] == '400': # Probably Zope is gone extra = '; is Zope started?' else: extra = '' raise ValueError("Error retrieving project %s: status %s%s" % (project, resp['status'], extra)) tree = etree.fromstring(content) members = [] for member in tree: m = {} m['username'] = member.find('id').text.lower() m['roles'] = [] for role in member.findall('role'): m['roles'].append(role.text) members.append(m) return members
def updateToken(self): http = mc.Http() http.SetHttpHeader("X-Plex-Platform", "Boxee") http.SetHttpHeader("X-Plex-Platform-Version", mc.GetInfoString("System.BuildVersion")) http.SetHttpHeader("X-Plex-Provides", "player") http.SetHttpHeader("X-Plex-Product", "Plexee") http.SetHttpHeader("X-Plex-Version", "1.0") try: http.SetHttpHeader("X-Plex-Device", mc.GetPlatform()) except: http.SetHttpHeader("X-Plex-Device", "Boxee") try: http.SetHttpHeader("X-Plex-Client-Identifier", mc.GetDeviceId()) except: http.SetHttpHeader("X-Plex-Client-Identifier", str(uuid.getnode())) base64String = base64.encodestring( "%s:%s" % (self.username, self.password)).replace('\n', '') http.SetHttpHeader("Authorization", "Basic %s" % base64String) postData = "username=%s&password=%s" % (self.username, self.password) data = http.Post(MyPlexService.AUTH_URL, postData) http.Reset() if data: tree = ElementTree.fromstring(data) self.authenticationToken = tree.findtext("authentication-token", None)
def load(xml): tree = ET.fromstring(xml) for descriptor_tag in tree.findall("descriptor"): descriptor_name = descriptor_tag.get('name') dh = DescriptorHelper(descriptor_name) for item in descriptor_tag.findall('hide'): name = item.get('name') if name: dh.hide(name) for item in descriptor_tag.findall('before'): name = item.get('name') dst = item.get('dst') if name and dst: dh.move(name, dh.position(dst) - 1) for item in descriptor_tag.findall('after'): name = item.get('name') dst = item.get('dst') if name and dst: dh.move(name, dh.position(dst) + 1) dh.reload_fields()
def _logon(self): """ Get an access token and user id """ # Lazy operation if self.__token and self.__userid: return (self.__token, self.__userid) # Parameter checking if not self.__username or not self.__apikey: raise Exception("Invalid username or API key") # Build request request = '<request><user><email>%s</email>\ <password>%s</password></user><api-key>%s</api-key>\ </request>' % (self.__username, self.__password, self.__apikey) headers = { "Accept":"application/xml", "Content-Type":"application/xml" } self.__conn.request("POST", "/users/token", request, headers) response = self.__conn.getresponse() data = response.read() if response.status != 200: raise Exception("Server returned error: %s)" % data) result = ET.fromstring(data) self.__token = result.findtext("access-token") self.__userid = result.findtext("user-id") return (self.__token, self.__userid)
def update_time_entry(self, entry): """ Updates the given entry or creates it if the entry ID is 0 """ create = entry.id == 0 xml = self._serialise_time_entry(entry) method = ['PUT','POST'][create] if create: url = "%s/time_entries?%s" % \ (self._get_base_url(), self._get_url_params()) else: url = "%s/time_entries/%s?%s" % \ (self._get_base_url(), entry.id, self._get_url_params()) headers = { "Accept":"application/xml", "Content-Type":"application/xml" } self.__conn.request(method, url, xml, headers) response = self.__conn.getresponse() data = response.read() if not response.status == 200: raise Exception("Could not update/create time entry."\ " Response was [%s]: %s" % (response.status, data)) return self._parse_time_entry(ET.fromstring(data))
def getPhotoList(self, listItem): #photoUrl = self.getUrl(self.getRootUrl(), fullUrl) url = self.getUrl(self.getRootUrl(), listItem.GetProperty('parentKey')) data = mc.Http().Get(url+'/children') if data: list = mc.ListItems() tree = ElementTree.fromstring(data) for photoNode in tree.findall("Photo"): title = photoNode.attrib.get("title", "Plex Photo") key = photoNode.attrib.get('key') for part in photoNode.findall("Media/Part"): li = mc.ListItem(mc.ListItem.MEDIA_PICTURE) li.SetProperty('key',key) li.SetTitle(title) li.SetLabel(title) li.SetPath(self.getUrl(self.getRootUrl(), key)) li.SetProperty('rotation','') li.SetProperty('zoom','') #Resize images li.SetImage(0, self.getThumbUrl(part.attrib.get('key'),1280,1280)) #li.SetImage(0, self.getUrl(self.getRootUrl(), part.attrib.get('key'))) list.append(li) return list else: return None
def get_title_of_arxiv_pubid(arxiv_pubid): def get_title_from_arxiv_xml(tree, tags): try: tag = tags.popleft() except IndexError: return tree.text for descendant in tree: if descendant.tag.endswith(tag): return get_title_from_arxiv_xml(descendant, tags) return None xml_referer = get_xml_referer_of_arxiv_pubid(arxiv_pubid) if xml_referer is None: return arxiv_pubid try: fxml = urlopen(xml_referer) xml = fxml.read() fxml.close() root = ET.fromstring(xml) except HTTPError: return arxiv_pubid title = get_title_from_arxiv_xml(root, deque(["GetRecord", "record", "metadata", "dc", "title"])) if title: return title return arxiv_pubid
def get_time_entries(self, range_start = None, range_end = None): result = [] # Prepare range filter filters = [] if range_start: filters.append("range_start=%s" % self._format_date(range_start)) if range_end: filters.append("range_end=%s" % self._format_date(range_end)) filter_str = '&'.join(filters) if filter_str: filter_str = '&' + filter_str url = "%s/time_entries?%s%s" % \ (self._get_base_url(), self._get_url_params(), filter_str) self.__conn.request("GET", url, "", { "Accept": "application/xml" }) response = self.__conn.getresponse() data = response.read() if not response.status == 200: return None for entry in ET.fromstring(data).findall("time-entry"): result.append(self._parse_time_entry(entry)) return result
def test_elementtree(): xml = '<outside> \ <inside name="Test Successful"> \ </inside> \ </outside>' print "Testing ElementTree: " + str(ET.fromstring(xml).find('inside').attrib)
def do_POST(self): length = int(self.headers.getheader('content-length')) xml = self.rfile.read(length) try: self.xml = ET.fromstring(xml) self.log_message("xml: %s" % self.xml) except: self.log_message("unable to parse XML") routes = [ { 'pattern' : '/MediaServerControlReply.xml', 'function' : self.media_server_control_reply }, { 'pattern' : '/control-reply.php', 'function' : self.media_server_control_reply } ] for route in routes: re_r=re.search(route['pattern'],self.path) if re_r: args = re_r.groupdict() try: route['function'](**args) except: print sys.exc_info() return self.send_response(404,'Not Found') self.log_message("unable to find matching route for %s" % self.path)
def get_title_of_arxiv_pubid(arxiv_pubid): def get_title_from_arxiv_xml(tree, tags): try: tag = tags.popleft() except IndexError: return tree.text for descendant in tree: if descendant.tag.endswith(tag): return get_title_from_arxiv_xml(descendant, tags) return None xml_referer = get_xml_referer_of_arxiv_pubid(arxiv_pubid) if xml_referer is None: return arxiv_pubid try: fxml = urlopen(xml_referer) xml = fxml.read() fxml.close() root = ET.fromstring(xml) except URLError, e: # For python 2.6 socket.timeout cannot be caught directly if hasattr(e, "reason") and isinstance(e.reason, socket.timeout): return arxiv_pubid else: # We make sure we don't cut out other URLErrors. raise
def main(): # Some sample data from http://developer.yahoo.com/maps/rest/V1/geocode.html yahoo_geocode_test = """\ <?xml version="1.0" encoding="UTF-8"?> <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"> <Result precision="address"> <Latitude>37.416384</Latitude> <Longitude>-122.024853</Longitude> <Address>701 FIRST AVE</Address> <City>SUNNYVALE</City> <State>CA</State> <Zip>94089-1019</Zip> <Country>US</Country> </Result> </ResultSet> """ # The "ResultSet" element should be treated like a list # The "Result" element should be treated like a map, with the # child elements converted to key/value pairs tag_convert = {"ResultSet": list_of_children, "Result": children_are_mapping} doc = ET.fromstring(yahoo_geocode_test) xdata = XMLDataExtractor(tag_convert, no_ns=True, downcase=True) result = xdata.from_elem(doc) from pprint import pprint pprint(result)
def _PullOne(self, uuid, namespace): data = PULL_XML % { 'url': self.url, 'EnumerationContext': uuid, 'namespace': namespace } response = self.post(data) d = response.read() xml = ElementTree.fromstring(d) if xml.tag != "{%(s)s}Envelope" % NAMESPACES: raise HyperVException("Wrong reply format") responses = xml.findall("{%(s)s}Body/{%(wsen)s}PullResponse" % NAMESPACES) if len(responses) < 0: raise HyperVException("Wrong reply format") uuid = None instance = None for node in responses[0].getchildren(): if node.tag == "{%(wsen)s}EnumerationContext" % NAMESPACES: uuid = node.text elif node.tag == "{%(wsen)s}Items" % NAMESPACES: instance = HyperVSoap._Instance(node) return uuid, instance
def userData(self, all=False): if self.token is not None: headers, req = self._create_request('/', {'wst': self.token}) try: util.info('[SC] userData') data = post(self._url('api/user_data/'), req, headers=headers) except: self.clearToken() return False util.info('[SC] userdata dat: %s' % data) xml = ET.fromstring(data) if not xml.find('status').text == 'OK': self.clearToken() return False if all == True: return xml util.debug("[SC] userInfo: %s %s" % (xml.find('ident').text, xml.find('vip').text)) if xml.find('vip').text == '1': xbmcgui.Window(10000).setProperty('ws.vip', '1') xbmcgui.Window(10000).setProperty('ws.ident', xml.find('ident').text) xbmcgui.Window(10000).setProperty('ws.days', xml.find('vip_days').text) return int(xml.find('vip_days').text) else: xbmcgui.Window(10000).setProperty('ws.vip', '0') return False
def getServers(self): localServers = dict() remoteServers = dict() foundServer = False if self.isAuthenticated(): data = mc.Http().Get(self.getLibraryUrl()) if data: tree = ElementTree.fromstring(data) for child in tree: host = child.attrib.get("address", "") port = child.attrib.get("port", "") accessToken = child.attrib.get("accessToken", "") machineIdentifier = child.attrib.get("machineIdentifier", "") local = child.attrib.get("owned", "0") util.logInfo("MyPlex found %s:%s" % (host,port)) foundServer = True server = PlexServer(host, port, accessToken) if not server.isAuthenticated(): continue if local == "1": localServers[machineIdentifier] = server else: remoteServers[machineIdentifier] = server return localServers, remoteServers, foundServer
def get_image_list(self, media_id): xml_url = self.url % (self.api_key, media_id) log('API: %s ' % xml_url) image_list = [] data = get_xml(xml_url) tree = ET.fromstring(data) for imagetype in self.imagetypes: imageroot = imagetype + 's' for images in tree.findall(imageroot): for image in images: info = {} info['id'] = image.get('id') info['url'] = urllib.quote(image.get('url'), ':/') info['preview'] = urllib.quote(image.get('preview'), ':/') info['type'] = imagetype info['language'] = image.get('lang') info['likes'] = image.get('likes') # Create Gui string to display info['generalinfo'] = 'Language: %s | Likes: %s ' % ( info['language'], info['likes']) if info: image_list.append(info) if image_list == []: raise NoFanartError(media_id) else: return image_list
def getCategories(self): # # Get XML... # httpCommunicator = HTTPCommunicator() url = "http://videoak.microsoftpdc.com/pdc_schedule/Schedule.xml" xmlData = httpCommunicator.get(url) # # Parse response... # rootElement = ElementTree.fromstring(xmlData, ElementTree.XMLParser(encoding="utf-8")) trackElements = rootElement.findall("Tracks/Track") categories = [] categories.append({"Name": "All sessions", "Id": ""}) for trackElement in trackElements: categoryName = trackElement.get("Name") categories.append({"Name": categoryName, "Id": categoryName}) # Add entries... for category in categories: listitem = xbmcgui.ListItem(category["Name"]) url = "%s?action=sessions&category=%s" % (sys.argv[0], urllib.quote_plus(category["Id"])) xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=listitem, isFolder=True) # Disable sorting... xbmcplugin.addSortMethod(handle=int(sys.argv[1]), sortMethod=xbmcplugin.SORT_METHOD_NONE) # End of directory... xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), succeeded=True)