Skip to content

Instantly share code, notes, and snippets.

@doyousketch2
Last active January 30, 2018 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doyousketch2/c25ee559dccd1dcfee0a4a0bba40b692 to your computer and use it in GitHub Desktop.
Save doyousketch2/c25ee559dccd1dcfee0a4a0bba40b692 to your computer and use it in GitHub Desktop.
Updates Conky when you have messages on Scratch.mit.edu
#!/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.
@doyousketch2
Copy link
Author

doyousketch2 commented Nov 2, 2016

Image of Conky
HitCount PythonVersions Scratch License
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:

conky.config = {
	alignment = 'top_right',
	background = false,
	border_outer_margin = 5,
	border_width = 1,
	cpu_avg_samples = 2,
	default_color = white,
	default_outline_color = '#555',
	default_shade_color = 'black',
	draw_borders = false,
	draw_graph_borders = true,
	draw_outline = false,
	draw_shades = true,
	double_buffer = false,
	extra_newline = false,
	font = 'DejaVu Sans Mono:size=10',
	gap_x = 35,
	gap_y = 25,
	no_buffers = true,
	out_to_console = false,
	out_to_stderr = false,
	own_window = true,
	own_window_class = 'Conky',
	own_window_transparent = true,
	own_window_type = 'dock',
	own_window_hints = 'below',
	short_units = true,
	stippled_borders = 0,
	update_interval = 3,
	uppercase = false,
	use_xft = true,
	use_spacer = 'none',
	show_graph_scale = false,
	show_graph_range = false,
}

conky.text = [[
${execpi 1200 python ~/.config/conky/ScratchMessages.py}
${color grey}CPU : $freq_g GHz
$color $cpu% ${cpubar 4}
${color grey}RAM : $mem / $memmax
$color $memperc% ${membar 4}
${color grey}Swap : $swap / $swapmax
$color $swapperc% ${swapbar 4}
${color grey}File systems:
${color #888} /    ${color grey}${fs_used /} / ${fs_size /}
$color ${fs_bar 6 /}
${color #888} /home  ${color grey}${fs_used /home} / ${fs_size /home}
$color ${fs_bar 6 /home}
${color grey}  CPU% Name               MEM%
${color #eee}${top cpu 1} ${top name 1} ${top mem 1}
${color #ccc}${top cpu 2} ${top name 2} ${top mem 2}
${color #aaa}${top cpu 3} ${top name 3} ${top mem 3}
${color #888}${top cpu 4} ${top name 4} ${top mem 4}
]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment