Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. #APRS Weather BOT
  2. #By CR7ALW
  3. #Uses the module aprslib
  4. #Uses http://openweathermap.org API
  5.  
  6. #Imports
  7. import agwlib
  8. import aprslib
  9. from parsebody import parse_body
  10. import socket
  11. import urllib
  12. import json #To understand the data
  13. import time
  14.  
  15. callsign=input('Please enter your callsign/SSID; ex. CQ0PVI-3.\n')
  16. via=input('Please enter the VIA path. (max 7, separated by commas); ex. WIDE1*,WIDE2*\n')
  17. via=via.split(',')
  18.  
  19. TCP_IP = '127.0.0.1'
  20. TCP_PORT = 8000
  21. BUFFER_SIZE =  1024 #Should be more than enough
  22.  
  23. APPID='xxxxxxxxxxxxxxxxxxxxxxxxxxxx' #THIS IS MINE, so I deleted my real key on this paste
  24.  
  25. #Open socket comunications to AGPWPE
  26. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  27. s.connect((TCP_IP, TCP_PORT))
  28.  
  29. #Basic proceidure
  30. #Add a handler to automatically decode the packets
  31. def parse_packet(unpacked):
  32.     """Remember the handlers the unpacked dict to the function. Example:
  33.    {'datalen': 1, 'DataKind': 'X', 'CallFrom': 'a', 'PID': 0, 'Port': 0, 'data': '\x01', 'CallTo': ''}"""
  34.     parsed=parse_body(unpacked['data'])
  35.     unpacked.update(parsed) #Adds the data from the dictionary given by parse_body to the unpacked stuff
  36.     return unpacked
  37. for kind in 'ISUT':
  38.     agwlib.add_handler(parse_packet,kind) #This applies to either of ISUT kinds, so solve them at once
  39.  
  40. #Add a handler for the "register callsign" data
  41. def process_X(unpacked):
  42.     if unpacked['data']!='\x01': raise Exception('Could not register callsign')
  43.     #TODO: maybe do this a little better
  44. agwlib.add_handler(process_X,'X')
  45.  
  46. #Register callsign and grab response
  47. s.send(agwlib.send_X(callsign))  #Beaware that send_# functions only generate the data. You still need the socket send function
  48. answer=agwlib.process(s.recv(BUFFER_SIZE))  #Process automatically sees what data we're having and calls the accordingly handler
  49. #Now enable packet monitorings
  50. s.send(agwlib.send_m()) #That's it
  51.  
  52. #Now prepare the openweathermap API
  53. url='http://api.openweathermap.org/data/2.5/weather'
  54.  
  55. def get_weather_data(lat,lon):
  56.     values={'lat':lat, 'lon':lon, 'APPID':APPID}
  57.     url_values = urllib.parse.urlencode(values)
  58.     full_url = url + '?' + url_values
  59.     data = urllib.request.urlopen(full_url)
  60.     data=data.read().decode(data.headers.get_content_charset())
  61.     data=json.loads(data)
  62.     return data
  63.  
  64. def weather_text(data):
  65.     """Takes weather data, makes a nice text"""
  66.     tempK=data['main']['temp']
  67.     tempC=tempK-273.15
  68.     desc=data['weather'][0]['description']
  69.     main=data['weather'][0]['main']
  70.     country=data['sys']['country']
  71.     name=data['name']
  72.     string= "Weather for %s, %s: %s (%s). Temperature: %.1fK (%.1fC)." % (name, country, main, desc, tempK, tempC)
  73.     #string="Weather for "+name+', '+country+': '+main+' ('+desc+'). Temperature: '+str(tempK)+'.'
  74.     return string
  75.    
  76. def custom_text(string, callsign):
  77.     """Takes the weasther string and adds a signature"""
  78.     #TODO: something here
  79.     return string
  80.  
  81. #Get on an infinite loop
  82. while 1:
  83.     packet = s.recv(BUFFER_SIZE) #This gets stuck in here until received
  84.    
  85.     processed=agwlib.process(packet) #Process it
  86.    
  87.     #Now send the weather data, case the user asked for it
  88.     dic=processed[1] #Forget the packet type
  89.     print(dic)
  90.     if 'comment' in dic.keys():
  91.         if 'CQWeather' in dic['comment']:
  92.             if 'latitude' in dic.keys() and 'longitude' in dic.keys():
  93.                 weather_data=get_weather_data(dic['latitude'],dic['longitude'])
  94.                 weather=weather_text(weather_data)
  95.                 print(weather)
  96.                 #TODO: maybe add a custom text
  97.                 #And now send the package back with the data
  98.                 to_send=agwlib.send_V(1, callsign, dic['CallFrom'], via, weather)
  99.                 time.sleep(0.5)
  100.                 s.send(to_send)
  101.    
  102. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement