本文整理汇总了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