~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to extra/conky/helioviewer_status.py

  • Committer: Keith Hughitt
  • Date: 2012-04-23 16:02:25 UTC
  • mto: This revision was merged to the branch mainline in revision 732.
  • Revision ID: keith.hughitt@nasa.gov-20120423160225-xzoh82ejf37c8yr7
Incorporated HVPull code

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#-*- coding:utf-8 -*-
3
 
"""Helioviewer.org Status Information Conky Script
4
 
 
5
 
This script queries Helioviewer.org to find how far behind data is for
6
 
each instrument, and generates a small conky snippet to display the
7
 
results. This can be used with the conky execp/execpi commands, e.g.:
8
 
 
9
 
The net result should be similar to the information obtained when visiting
10
 
the Helioviewer.org status page at http://www.helioviewer.org/status.
11
 
 
12
 
Example usage:
13
 
 
14
 
  text_buffer_size 1024
15
 
  ${voffset 4}${execpi 5 ~/.conky/helioviewer_status.py}
16
 
 
17
 
"""
18
 
from urllib2 import urlopen
19
 
import json
20
 
 
21
 
# Conky formatting parameters'
22
 
# Better: allow user to specify as command-line arguments
23
 
CONKY_FONT = "DroidSansMono"
24
 
CONKY_FONT_SIZE = 7.6
25
 
CONKY_COLOR_NUM = 3
26
 
CONKY_VOFFSET = 0
27
 
CONKY_ALIGNC = 60
28
 
 
29
 
def main():
30
 
    """Main"""
31
 
    HV_QUERY_URL = "http://www.helioviewer.org/api/?action=getStatus"
32
 
    
33
 
    # Status icon colors
34
 
    colors = {
35
 
        1: "green",
36
 
        2: "yellow",
37
 
        3: "orange",
38
 
        4: "red",
39
 
        5: "gray"
40
 
    }
41
 
 
42
 
    # Query Helioviewer.org
43
 
    response = urlopen(HV_QUERY_URL).read()
44
 
    instruments = json.loads(response)
45
 
    
46
 
    # Generate conky snippet
47
 
    voffset = "${voffset %d}" % CONKY_VOFFSET
48
 
    font = "${font %s:size=%0.1f}" % (CONKY_FONT, CONKY_FONT_SIZE)
49
 
    color = "${color%d}" % CONKY_COLOR_NUM
50
 
    alignc = "${alignc %d}" % CONKY_ALIGNC
51
 
 
52
 
    # Iterate through instruments in sorted order
53
 
    iterator = iter(sorted(instruments.iteritems()))
54
 
 
55
 
    for inst, status in iterator:
56
 
        # Ignore non-active datasets (30 days or more behind real-time)
57
 
        if status['secondsBehind'] > (30 * 24 * 60 * 60):
58
 
            continue
59
 
 
60
 
        # Status icon
61
 
        icon = "${offset 3}${font Webdings:size=%0.1f}${color %s}n${font}  " % (CONKY_FONT_SIZE * 0.85, colors[status['level']])
62
 
        
63
 
        # Time
64
 
        if status['secondsBehind'] < (60 * 60):
65
 
            time = "%d minutes" % (status['secondsBehind'] / 60)
66
 
        elif status['secondsBehind'] < (24 * 60 * 60):
67
 
            time = "%0.1f hours" % (status['secondsBehind'] / (60 * 60.))
68
 
        else:
69
 
            time = "%0.1f days" % (status['secondsBehind'] / (24 * 60 * 60.))
70
 
 
71
 
        # Print snippet
72
 
        print (voffset + icon + font + color + alignc + inst + "${alignr}" + time  + "${font}")
73
 
 
74
 
if __name__ == '__main__':
75
 
    main()
76