Last active
January 30, 2018 01:22
-
-
Save doyousketch2/c25ee559dccd1dcfee0a4a0bba40b692 to your computer and use it in GitHub Desktop.
Updates Conky when you have messages on Scratch.mit.edu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import requests as rq ## used to access URL's in Python | |
"""========================================================""" | |
## ScratchMessages.py | |
## Updates Conky when you have messages on Scratch.mit.edu | |
## | |
## by Doyousketch2 | |
## Oct 30, 2016 | |
## | |
## GNU GPLv3 - https://www.gnu.org/licenses/gpl-3.0.html | |
"""========================================================""" | |
## Place ScratchMessages.py script in your Conky config dir. | |
## ~/.config/conky/ | |
## | |
## make sure you can run it: | |
## chmod +x ScratchMessages.py | |
## | |
## Add the next line to your ~/.config/conky/conky.conf | |
## | |
## ${execpi 1200 python ~/.config/conky/ScratchMessages.py} | |
## | |
"""========================================================""" | |
## And place it under the part that says: conky.text = [[ | |
## | |
## Script updates every 1200 sec's. | |
## Divide by 60 to get mins, so 20 mins. | |
"""========================================================""" | |
## If you don't have PIP: | |
## sudo apt-get install pip | |
## | |
## If you don't have the REQUESTS module, depending on OS: | |
## | |
## sudo pip install requests | |
## python -m pip install requests | |
## py -m pip install requests | |
## | |
"""========================================================""" | |
## Customize announcement, if you want to, | |
## and set your user name(s) here. | |
ANNOUNCE = 'New Scratch Messages!' | |
Color1 = '#25aff4' ## Announcement (blue) | |
Color2 = '#f8a839' ## Number of messages (orange) | |
Color3 = '#aaa' ## Name (grey) | |
## Font effect for number of messages | |
BOLD = '${font :style=Bold:pixelsize=14}' | |
USERNAME = 'Put_Your_Scratch_Username_Here' | |
## ONLY IF you have multiple accounts, like me + my kid, for example... | |
## Delete the single USERNAME line above, | |
## and use this alternate method, instead: | |
##USERNAME = ('Account_1', 'Account_2', 'Account_3') | |
## IF you end up using that method, | |
## Be sure to remove the ## in front, | |
## so Python knows that you are using it, | |
## and it's not just another comment. | |
## | |
## You can change the number of accounts. | |
## | |
## Just wrap each account with quotes '' | |
## seperate 'em with a comma , | |
## and be sure the entire list is surrounded by (parenthesis) | |
## | |
"""========================================================""" | |
## Don't edit below this line. | |
"""========================================================""" | |
messageRecieved = 0 ## haven't recieved a message yet. | |
## put color codes in a format Conky can decode. | |
C1 = '${color ' + Color1 + '}' | |
C2 = '${color ' + Color2 + '}' | |
C3 = '${color ' + Color3 + '}' | |
def CheckIt(NAME): ## simple function to check server. | |
global messageRecieved ## use the variable we specified earler. | |
## Call the message URL with specified name | |
Response = rq .get('https://api.scratch.mit.edu/users/' +NAME +'/messages/count') | |
## Expect a response like: {"count":0} | |
## Convert this from json string to python dictionary. | |
Dict = Response .json() | |
## get the number-value associated with the 'count' key. | |
NumberOfMessages = Dict['count'] | |
## Only print output if we've actually recieved messages. | |
## Makes it easier to see at a glance; no info when all's clear. | |
if NumberOfMessages > 0: | |
if messageRecieved == 0: ## if this is the first time through, | |
print(C1 +ANNOUNCE) ## then announce that we have new mail (blue) | |
messageRecieved = 1 ## (only print announcement once) | |
## Concatenate the results, so we get: | |
## (bold + orange + number + reset font size + grey + name) | |
print(BOLD +C2 +str(NumberOfMessages) + '${font} ' +C3 +NAME) | |
## str(Number) just converts the number to a string | |
## so it sees the whole message as text, when piecing it together. | |
"""========================================================""" | |
## function defined. ok, go. | |
if type(USERNAME) == tuple: ## If we have a few usernames, | |
for name in USERNAME: ## iterate through those names. | |
CheckIt(name) | |
else: ## if there's only one name, | |
CheckIt(USERNAME) ## then we only need to check it once. | |
"""========================================================""" | |
if messageRecieved == 1: ## if we got a message, | |
print('') ## print a blank line after, so it's easy to read in Conky. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this Python script on Debian,
so those are Linux instructions.
I don't even know if they have Conky for Windows,
So you're 'prolly out of luck if you're on that system.
Also, my Conky config looks like this: