def process_blocks(self): self._get_block_dict() for section in self._block_dict: channel_list = self._block_dict[section] ii_part_out = self._output + '/' + section + "/" + section + ".ii.part" try: with open(ii_part_out, "wb") as ii_part: for channel in channel_list: xml_util = XMLUtil(channel[1]) try: doc_list = xml_util.get_document_list() for article in doc_list: article_title = article.find('title') article_date = article.find('pubDate') try: doc_key = section + '-' + channel[ 0] + '-' + article_title.text + '-' + article_date.text self._insert_doc_id(doc_key) self._process_article(doc_key, article) except (TypeError, AttributeError): self.do_callback("INDERR", article_title.text) except FileNotFoundError: self.do_callback("XMLNF", channel[1]) self._write_partial_ii_to_file(ii_part) except FileNotFoundError: self.do_callback("BLKNE", ii_part_out)
def login(self): loginVars = {'action': 'login', 'lgname': webConfig.editorRobotUser, 'lgpassword': webConfig.editorRobotPass, 'format': 'xml'} data = urllib.urlencode(loginVars) req = urllib2.Request(webConfig.wikiapi, data) response = urllib2.urlopen(req) xmlUtil = XMLUtil(response) loginVars['lgtoken'] = xmlUtil.getLoginToken() data = urllib.urlencode(loginVars) req = urllib2.Request(webConfig.wikiapi, data) self.sessionID = xmlUtil.getSessionID() req.add_header('Cookie', webConfig.sessionName + '=' + self.sessionID) response = urllib2.urlopen(req) xmlUtil = XMLUtil(response) return xmlUtil.getActionSuccess()
def start(): factoryNames = XMLUtil.getFactoryNames() for factoryName in factoryNames: try: # 利用类名的字符串来实例化一个对象 factory = globals()[factoryName]() tv = factory.produceTelevision() tv.play() ac = factory.produceAirConditioner() ac.changeTemperature() except Exception as e: print('Error:', e)
def start(): buildNames = XMLUtil.getBuildNames() for buildName in buildNames: try: # 利用类名的字符串来实例化一个对象 mb = globals()[buildName]() waiter = KFCWaiter() waiter.setMealBuilder(mb) meal = waiter.construct() print("套餐组成:") print('==========================================') print(meal.getFood()) print(meal.getDrink()) print('==========================================\n') except Exception as e: print(e)
def editPage(self, pageTitle, pageBody): safePageTitle = str.replace(pageTitle, ' ', '_') editVars = {'action': 'query', 'prop': 'info', 'intoken': 'edit', 'titles': safePageTitle, 'format': 'xml'} data = urllib.urlencode(editVars) req = urllib2.Request(webConfig.wikiapi, data) req.add_header('Cookie', webConfig.sessionName + '=' + self.sessionID) response = urllib2.urlopen(req) xmlUtil = XMLUtil(response) xmlUtil.printResponse() editToken = xmlUtil.getEditToken() submitVars = {'action': 'edit', 'title': safePageTitle, 'text': pageBody, 'contentformat': 'text/x-wiki', 'contentmodel': 'wikitext', 'bot': '1', 'token': editToken, 'format': 'xml'} data = urllib.urlencode(submitVars) req = urllib2.Request(webConfig.wikiapi, data) req.add_header('Cookie', webConfig.sessionName + '=' + self.sessionID) response = urllib2.urlopen(req) xmlUtil = XMLUtil(response) return xmlUtil.getActionSuccess()