def _replay_chat(self, start, end): caches = [set(), set()] cache_index = 0 while start <= end: prev_cache = caches[cache_index] cache_index = (cache_index + 1) % 2 cache = caches[cache_index] = set() payload = { "video_id": self.id, "start": start, } res = API.get(RECHAT, payload) if "errors" in res: return for message in res.get("data"): mid = message.get("id") if mid in prev_cache: continue cache.add(mid) yield message start += 30
def past_streams(self, cap=None): url = "{}/channels/{}/videos" url = url.format(KRAKEN, self.name) params = { "broadcasts": "true", } read_count = 1 vods_read = 0 while read_count: if not url: return res = API.get(url, params) videos = res.get("videos", []) read_count = len(videos) for video in videos: yield Video( total=res.get("_total"), **video ) vods_read += 1 if cap and vods_read >= cap: return url = res.get("_links").get("next", None)
def live_stream(self): url = "{}/streams/{}" url = url.format(KRAKEN, self.name) res = API.get(url) stream = res.get("stream", None) if stream: return Stream(links=res.get("_links"), **stream)
def get_chatters(self): url = "{}/group/user/{}/chatters" url = url.format(TMI, self.name) res = API.get(url) return Chatters( count=res.get("chatter_count"), **res.get("chatters") )
def live_stream(self): url = "{}/streams/{}" url = url.format(KRAKEN, self.name) res = API.get(url) stream = res.get("stream", None) if stream: return Stream( links=res.get("_links"), **stream )
def _replay_boundaries(self): payload = { "video_id": self.id, "start": 0, } res = API.get(RECHAT, payload) if "errors" not in res: raise Exception("Expected chat replay boundary error message.") msg = res.get("errors")[0].get("detail") try: mat = re.match('-?\d+ is not between (\d+) and (\d+)', msg) return tuple(map(int, [mat.group(1), mat.group(2)])) except Exception as e: logger.warning(msg) logger.exception(e) raise
def get_follows(url, cursor=None): payload = {} read_count = 1 while read_count: if cursor: payload["cursor"] = cursor res = API.get(url, payload) if res: follows = res.get("follows", []) read_count = len(follows) for follow in follows: follow['total'] = res.get('_total') yield follow if read_count: cursor = res.get("_cursor") if not cursor: url = res.get("_links").get("next")
def loots_streams(self, limit=None, direction=None): url = "{}/search/channels/{}/streams" max_streams = LOOTS_MAX_GET direction = direction or 'desc' if limit: max_streams = min(limit, LOOTS_MAX_GET) params = { "json": json.dumps({ "offset": 0, "limit": max_streams, "sort_key": "_t", "sort_order": direction.lower() }) } url = url.format(LOOTS, self.id) res = API.get(url, params) for stream in res.get("data", []): yield LootsStream(**stream)
def past_streams(self, cap=None): url = "{}/channels/{}/videos" url = url.format(KRAKEN, self.name) params = { "broadcasts": "true", } read_count = 1 vods_read = 0 while read_count: if not url: return res = API.get(url, params) videos = res.get("videos", []) read_count = len(videos) for video in videos: yield Video(total=res.get("_total"), **video) vods_read += 1 if cap and vods_read >= cap: return url = res.get("_links").get("next", None)
def __set_caching(**kwargs): caching = __get_cache(kwargs.get('caching')) if caching: TwitchAPI.set_caching(caching)
def get(id): url = '{}/rechat-message/{}' url = url.format(RECHAT, id) return ChatMessage(**API.get(url))
def get(name): url = '{}/channels/{}' url = url.format(KRAKEN, name.lower().strip()) return Channel(**API.get(url))
def get(vid_id): url = '{}/videos/{}' url = url.format(KRAKEN, vid_id) return Video(**API.get(url))
def get(name): url = '{}/users/{}' url = url.format(KRAKEN, name.lower().strip()) return User(**API.get(url))
def get_chatters(self): url = "{}/group/user/{}/chatters" url = url.format(TMI, self.name) res = API.get(url) return Chatters(count=res.get("chatter_count"), **res.get("chatters"))