You are not logged in.

#1 2011-08-09 02:26:13

kairu
Member
Registered: 2011-08-02
Posts: 7

Executing Python scripts with Conky.

Hey Folks, hope this isn't too simple where its slapping me in the face.

So I've created a weather script in python a few months ago and decided to throw it into my conky config. The problem is, conky won't output any of it. It has to be the script because I've tried making just a script that simply prints "hi"

#!/usr/bin/python
# hehe.py
print( "hi" )

And that works just fine. When I try my weather script, it just outputs a blank line as if nothing were there. The weather script works, I've executed it in my terminal just fine. I've set the mode to executable and everything. What am I doing wrong?

Here's the script (Don't make fun of it, now):

#!/usr/bin/python2
# coding: utf-8
# weather.py
# Coded on 09.25.09

import httplib, re, sys, os

rs = '######################'
ind = '\n# '
if len(sys.argv) !=1:
   city = sys.argv[1:]
   place = ""
   for e in city:
      if (place == ""):
         place = e
      else:
         place = place + '+' + e
else:
   print('Please enter a Zip/Area code or city name.')
   sys.exit()

def GrabHTML(place):
   '''Grab HTML'''
   try:
      url = '/' + place
      web = httplib.HTTPConnection('www.weatherreports.com')
      web.request("GET", url)
      ra = web.getresponse()
      html = ra.read()
      return html
   except:
      return '[!] Connection Error!'

def GetWeather(html):
   '''Grab Basic Weather Info.''' 
   try:
      if re.search('We have more than one location for your search.<br>', html):
         return '[!] Please be more specific in your location.'
      else:
         r = re.split(r'<span class="currentValue">(.+?)</span>', html)[1] # Time
         s = re.split(r'<div class="currentTemperature">(.+?)</div>', html)[1]
         x = re.split(r'Wind Spd:</span><span class="currentValue">(.+?)</span>', html)[1]
         g = re.split(r'Wind Dir:</span><span class="currentValue">(.+?)</span>', html)[1]
         p = re.split(r'Pressure:</span><span class="currentValue">(.+?)</span>', html)[1]
         t = re.split(r'Dewpoint:</span><span class="currentValue">(.+?)</span>', html)[1]
         y = re.split(r'Feels Like:</span><span class="currentValue">(.+?)</span>', html)[1]
         v = re.split(r'Visibility:</span><span class="currentValue">(.+?)</span>', html)[1]
         if re.search('&deg;', html):
            s = s.replace('&deg;', '°')
            g = g.replace('&deg;', '°')
            t = t.replace('&deg;', '°')
            y = y.replace('&deg;', '°')
         temp = '# Updated: ' + r + ind + 'Temp: ' + s + ind + 'Wind Spd: ' + x + ind + 'Wind Dir: ' + g + ind + 'Pressure: ' + p + ind + 'Dewpoint: ' + t + ind + 'Feels Like: ' + y + ind + 'Visibility: ' + v 
         return rs + '\n' + temp + '\n' + rs
   except:
      return '[!] Please use a real location.'

def Location(html):
   e = re.split(r'<title>Weather Report and Weather Forecast for (.+?)</title>', html)[1]
   return '# Weather for...' + ind + e

def Logo():
   print rs
   print '# Google.the.Kid Productions'
   return '# googlethekid[at]null[dot]net'   

html = GrabHTML(place)
print
print(Logo())
#print(Location(html))
print(GetWeather(html))
print '\n'

(Keep in mind some of the stuff in the piece is just for the output looks.)

And heres my conky config (again, its probably really messy):

alignment top_right
background yes
border_width 1
cpu_avg_samples 2
default_color white
default_outline_color white
default_shade_color white
draw_borders no
draw_graph_borders yes
draw_outline no
draw_shades no
use_xft yes
xftfont Comfortaa Regular:size=8
font Comfortaa_Regular:size=8
gap_x 5
gap_y 60
net_avg_samples 2
no_buffers yes
out_to_console no
out_to_stderr no
extra_newline no
own_window yes
own_window_transparent yes
own_window_class Conky
own_window_type desktop
stippled_borders 0
update_interval 1.0
uppercase no
use_spacer left
show_graph_scale no
show_graph_range no
double_buffer yes
format_human_readable yes
short_units yes

TEXT
${alignc}$nodename - $sysname $kernel
${alignc}${execi 3600 wget -O - http://whatismyip.org/ | tail}
${if_match ${battery_percent BAT1} <= 15 }${alignc}${color red}${blink Plug In Charger!!!}${endif}${color}
$hr
${color #C85A17}Uptime:$color $uptime
${color #C85A17}RAM Usage  :$color $memperc%  ${alignc}${membar 4}
${color #C85A17}Swap Usage:$color $swapperc%    ${alignc}${swapbar 4}
${color #C85A17}CPU Usage  :$color $cpu%    ${alignc}${cpubar 4}
${color #C85A17}Processes   :$color$processes  ${color #C85A17}Running:$color $running_processes
${color #C85A17}Load           :$color ${loadavg}

${color Tan1}BATTERY ${color DarkSlateGray}${hr 2}
    
   ${color #8D38C9}${battery_percent BAT1}% ${if_match ${battery_percent BAT1} >= 25 }${color green}${battery_bar 4 BAT1}${color}${endif}${if_match ${battery_percent BAT1} < 25 }${color red}${battery_bar 4 BAT1}${color}${endif}
${color Tan1}File systems ${color DarkSlateGray}${hr 2}
 ${color #C85A17}/ $color         ${fs_used /}/${fs_size /} ${fs_bar 6 /}
 ${color #C85A17}/home $color${fs_used /home}/${fs_size /home}  ${fs_bar 6 /home}

${font}${color Tan1}NETWORK ${color DarkSlateGray}${hr 2}
${font}${color #C85A17}Connected to: ${color #437C17}${wireless_essid wlan0} ${color #C85A17}@ ${color #437C17}${wireless_bitrate wlan0}
${color #2B65EC}eth0${color #707070} :${color white} ${addr eth0}${color #707070}  (${color white}${downspeed eth0}${color #707070}/${color white}${upspeed eth0}${color #707070})${color white} ${color #707070}
${color #C85A17}wlan0${color #707070}:${color white} ${addr wlan0} ${color #707070}(${color white}${downspeed wlan0}${color #707070}/${color white}${upspeed wlan0}${color #707070})${color white} ${color #707070}

${font}${color Tan1}PROCESSES ${color DarkSlateGray}${hr 2}
${font}${alignr}${color #C85A17}Name              PID   CPU%   MEM%
 ${color red} ${top name 1} ${alignr}${top pid 1} ${top cpu 1} ${top mem 1}
 ${color #E55B3C} ${top name 2} ${alignr}${top pid 2} ${top cpu 2} ${top mem 2}
 ${color #FDD017} ${top name 3} ${alignr}${top pid 3} ${top cpu 3} ${top mem 3}
 ${color #59E817} ${top name 4} ${alignr}${top pid 4} ${top cpu 4} ${top mem 4}${color}

${color Tan1}GMail ${color DarkSlateGray}${hr 2}
${color}${execp  python /home/kairu/scripts/weather.py 95815}
${exec python ~/scripts/hehe.py}

(The hehe.py is the "hi" script")

Thanks!

Last edited by kairu (2011-08-09 02:26:57)

Offline

#2 2011-08-09 13:28:37

Inxsible
Forum Fellow
From: Chicago
Registered: 2008-06-09
Posts: 9,183

Re: Executing Python scripts with Conky.

The problem, I think, is that you have set the environment to python2 in the weather.py script. but you are calling python in conky -- which points to python3 in Arch.

If your script works with python3, simply change the environment in the script OR if it doesn't then in conky call it using python2


Forum Rules

There's no such thing as a stupid question, but there sure are a lot of inquisitive idiots !

Offline

#3 2011-08-09 14:44:38

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: Executing Python scripts with Conky.

Sort of off-topic:
That weather script is somewhat nasty. if you know python I would suggest reawriting it using lxml or similar for sane parsing while you are at it. (and prefably, get some plain xml forcasts instead of parsing a html page)

Also, the try: except: block can actually cause problems since it will catch any errors, which would at worst case end up blocking.

Last edited by Mr.Elendig (2011-08-09 14:44:54)


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#4 2011-08-10 15:44:11

kairu
Member
Registered: 2011-08-02
Posts: 7

Re: Executing Python scripts with Conky.

Inxsible wrote:

The problem, I think, is that you have set the environment to python2 in the weather.py script. but you are calling python in conky -- which points to python3 in Arch.

If your script works with python3, simply change the environment in the script OR if it doesn't then in conky call it using python2

It worked! Well, sort of. I did see it flash on conky a couple times but disappear quickly. So it's trying to work, perhaps it's the script?

Mr.Elendig wrote:

Sort of off-topic:
That weather script is somewhat nasty. if you know python I would suggest reawriting it using lxml or similar for sane parsing while you are at it. (and prefably, get some plain xml forcasts instead of parsing a html page)

Also, the try: except: block can actually cause problems since it will catch any errors, which would at worst case end up blocking.

Yeah, I know it's a bit rough. I'm sorta new to python so cleaning up is needed. Thanks for the tips. So like use Google's? I think they have an XML forecast.
Seeing as how it does run without errors I'll try removing those and see if that works. Thanks!

Offline

Board footer

Powered by FluxBB