[twitter] Fix DMs since_id check

This commit is contained in:
Cathy Hu 2020-07-06 11:11:08 +02:00 committed by acipm
parent 3f5f5106e0
commit cc4bf13388

View file

@ -25,8 +25,8 @@ class TwitterBot(Censor):
} }
self.client = PeonyClient(**self.tokens) self.client = PeonyClient(**self.tokens)
self.polling_interval_sec = 60 self.polling_interval_sec = 60
self.mentions_since_id = None self.mentions_since_id = self.twitter_model.mentions_since_id
self.dms_since_id = None self.dms_since_id = self.twitter_model.dms_since_id
async def run(self): async def run(self):
await gather(self.poll(), self.push()) await gather(self.poll(), self.push())
@ -35,12 +35,10 @@ class TwitterBot(Censor):
while True: while True:
messages = await self._poll_direct_messages() messages = await self._poll_direct_messages()
messages.extend(await self._poll_mentions()) messages.extend(await self._poll_mentions())
print(messages) logger.debug('Polled messages: %s' % str(messages))
# TODO hold since_ids in database
await self.twitter_model.update( await self.twitter_model.update(
dms_since_id=self.dms_since_id, mentions_since_id=self.mentions_since_id dms_since_id=self.dms_since_id, mentions_since_id=self.mentions_since_id
) )
# TODO send message to censor
for message in messages: for message in messages:
await self.publish(Message(message)) await self.publish(Message(message))
await sleep(self.polling_interval_sec) await sleep(self.polling_interval_sec)
@ -52,10 +50,10 @@ class TwitterBot(Censor):
dms_filtered = [] dms_filtered = []
if dms: if dms:
for dm in dms: for dm in dms:
if dm.id == self.dms_since_id: if int(dm.id) == self.dms_since_id:
break break
dms_filtered.append(dm) dms_filtered.append(dm)
self.dms_since_id = dms[0].id self.dms_since_id = int(dms[0].id)
messages = [] messages = []
for dm in dms_filtered: for dm in dms_filtered:
filtered_text = await self._filter_text( filtered_text = await self._filter_text(
@ -65,7 +63,6 @@ class TwitterBot(Censor):
if not filtered_text: if not filtered_text:
continue continue
messages.append(filtered_text) messages.append(filtered_text)
logger.debug(messages)
return messages return messages
async def _poll_mentions(self): async def _poll_mentions(self):
@ -80,7 +77,6 @@ class TwitterBot(Censor):
if not filtered_text: if not filtered_text:
continue continue
messages.append(filtered_text) messages.append(filtered_text)
logger.debug(messages)
return messages return messages
async def _filter_text(self, entities, text): async def _filter_text(self, entities, text):