# \[Python\]I can't make !uptime command for my bot

**URL:** <https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023>\
**Category:** API\
**Created:** [January 25, 2017, 3:32pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023 "2017-01-25T15:32:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![metalun4](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@metalun4](https://discuss.dev.twitch.com/u/metalun4)\
**Post date:** [January 25, 2017, 3:32pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023/1 "2017-01-25T15:32:02Z")

</div>

My bot joins my chat perfectly and respond any other commands but uptime command doesn’t work. I’m very new to this chat bot thing.

Error I got:  
Traceback (most recent call last):  
File “./bot.py”, line 152, in   
get\_stream\_uptime()  
File “./bot.py”, line 39, in get\_stream\_uptime  
if get\_stream\_status():  
File “./bot.py”, line 35, in get\_stream\_status  
if online\_data[‘stream’] != None:  
KeyError: ‘stream’

My Bot:  
[https://hastebin.com/yabijedoho.py](https://hastebin.com/yabijedoho.py)

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [January 25, 2017, 3:44pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023/2 "2017-01-25T15:44:42Z")

</div>

1. Remove you PASS from the hastebin
2. The problem is that your request code is a) Not checking the HTTP Status Response and b) passing no headers.

So you are making a request to a API v5 URL but asking for the default of a v3 response.

You need to add a header to your requests.get of `Accept: application/vnd.twitchtv.v5+json`

I’m not familiar with python’s request function, so you’ll have to look that up yourself.

---

<div class="post-metadata">

**Author:** ![metalun4](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@metalun4](https://discuss.dev.twitch.com/u/metalun4)\
**Post date:** [January 25, 2017, 4:11pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023/3 "2017-01-25T16:11:34Z")

</div>

Even if I try with v3 url it’s not responding and giving the same error.

---

<div class="post-metadata">

**Author:** ![TournyMasterBot](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/tournymasterbot/32/2013_2.png) [@TournyMasterBot](https://discuss.dev.twitch.com/u/TournyMasterBot)\
**Post date:** [January 25, 2017, 4:17pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023/4 "2017-01-25T16:17:24Z")

</div>

If you haven’t already, I would change your password if you put it on a public paste site – And probably regenerate your applications secret key to be safe. (Anybody who subscribes to emails from twitch dev forums still has access to the password.)

Re: The original post,

If the stream is offline it returns a null object, not a stream object (you will also get a null object if you had a web exception, such as not passing a ClientID]. You need to check if your api object is null first, and if not, THEN do your checks.

I would recommend also catching web exceptions (whatever that looks like in python) and printing the http status code + error message if you get one.

Re: @BarryCarlyon 's point, you need to either send your ClientID as a header value in your api request, or as a ?client\_id=xxxxxx at the end of the url.

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [January 25, 2017, 6:29pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023/5 "2017-01-25T18:29:19Z")

</div>

Not entirely sure but I think my library ([`asynctwitch`](http://GitHub.com/martmists/asynctwitch)) supports it.

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [February 24, 2017, 6:29pm UTC](https://discuss.dev.twitch.com/t/python-i-cant-make-uptime-command-for-my-bot/9023/6 "2017-02-24T18:29:18Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
