Ice9Coffee / HoshinoBot

A qqbot for Princess Connect Re:Dive (and other usage :)
GNU General Public License v3.0
1.41k stars 412 forks source link

增加twitter_stream的本地id储存和读取 #186

Closed kkbllt closed 3 years ago

kkbllt commented 3 years ago

鉴于机器人其实不会经常重启,但也不能保证不会。 建议可以增加一个用于储存获取过推特id文件。

twitter uid或许是唯一且不可变更的。 此类用于读取和储存已经获得过的id,以避免多次重启导致触顶(users/show接口的15min/900限制)

class twitteruserid:
      def __init__(self):
          self.file = os.path.expanduser('~/.hoshino/twitterid.json')
          self.twtuserid = self.loadid()

      def loadid(self) -> dict:
          if os.path.exists(self.file):
              with open(self.file,'r') as tid:
                  self.twtuserid = json.load(tid)
          else:
              return {'user':{},'baduser':[]}

      def saveid(self):
          with open(self.file,'w') as tid:
              tid.write(json.dumps(self.twtuserid,indent=4))

tid = twitteruserid()

原有
follow_ids = [(await client.api.users.show.get(screen_name=i)).id for i in router.follows]
更换成(但这样做则会不好看)
    follow_ids = []
    for i in router.follows:
        if tid.twtuserid['user'].get(i,None) == None:
            try:
                _id = await client.api.users.show.get(screen_name=i)
                follow_ids.append(_id.id)
                tid.twtuserid['user'][i] = _id.id
            except:
                tid.twtuserid['baduser'][i] = _id['errors']
        else:
            follow_ids.append(tid.twtuserid['user'][i])
    tid.saveid()