当前位置: 首页>>代码示例>>Python>>正文


Python NamedTemporaryFile.inUse方法代码示例

本文整理汇总了Python中tempfile.NamedTemporaryFile.inUse方法的典型用法代码示例。如果您正苦于以下问题:Python NamedTemporaryFile.inUse方法的具体用法?Python NamedTemporaryFile.inUse怎么用?Python NamedTemporaryFile.inUse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tempfile.NamedTemporaryFile的用法示例。


在下文中一共展示了NamedTemporaryFile.inUse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run

# 需要导入模块: from tempfile import NamedTemporaryFile [as 别名]
# 或者: from tempfile.NamedTemporaryFile import inUse [as 别名]
	def run(self):
		self.server.start()
                while True:
			if self.exit.isSet():
				self.server.send('exit')
				with self.tempfileLock:
					for key in self.tempfiles:
						self.tempfiles[key].close()
				return
                        search = self._getSearch()
                        if search == '':#Twitter returns an error for empty searches, so this is a way around it
                                tweets = []
                        else:
                                params = {'q': self._getSearch(), 'result_type': 'recent', 'lang': 'en', 'count': 100}#Check twitter API for all parameters
                                r = requests.get('https://api.twitter.com/1.1/search/tweets.json', headers = self._headers, params = params)
                                tweets = [ tweet for tweet in r.json()['statuses'] if 'retweeted_status' not in tweet]#No need for boring retweets
			with self.tweetLock:
				self.tweets = tweets
			mediaObjs = []
			imgs = {}
			for tweet in tweets:
				if 'media' in tweet['entities']:
					mediaObjs.extend([entity for entity in tweet['entities']['media'] if entity['type'] == 'photo'])
			with self.tempfileLock:
				for mediaObj in mediaObjs:
					key = mediaObj['media_url']
					if key in self.tempfiles:
						self.tempfiles[key].inUse = True
						continue
					if 'thumb' in mediaObj['sizes']:
						imgRequest = requests.get(key + ':thumb')
					elif 'small' in mediaObj['sizes']:
						imgRequest = requests.get(key + ':small')
					else:
						imgRequest = requests.get(key)
					if imgRequest.status_code == 200:#make sure the link worked
						temp = NamedTemporaryFile(prefix = key.replace('/', ''))
						temp.write(imgRequest.content)#Saves image in temp file so it only has to be downloaded once
						temp.seek(0)#moves to start of file
						imgs[key] = b64encode(temp.read())
						temp.inUse = True
						self.tempfiles[key] = temp 
			msg = json.dumps((tweets, imgs))
			self.server.send(msg)
			self.deleteUnusedTempfiles()
			time.sleep(2)#Don't want the loop to run to often, or else you hit the twitter rate limit
开发者ID:tomgarcia,项目名称:Tweeteor,代码行数:48,代码来源:searcher.py


注:本文中的tempfile.NamedTemporaryFile.inUse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。