-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
93 lines (82 loc) · 3.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import logging
import discord
import configparser
import feedparser
import geopy.distance
import time
import asyncio
# Logging
#logger = logging.getLogger('discord')
#logger.setLevel(logging.DEBUG)
#handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
#handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
#logger.addHandler(handler)
logging.basicConfig(level=logging.INFO)
# Config
config = configparser.ConfigParser()
config.read('conf.ini')
bottoken = config['default']['token']
channelid = int(config['default']['channel'])
roleid = config['default']['roleid']
looptime = int(config['default']['frequency'])
waypoint1 = (float(config['default']['waypoint1lat']), float(config['default']['waypoint1lon']))
waypoint1name = config['default']['waypoint1name']
waypoint2 = (float(config['default']['waypoint2lat']), float(config['default']['waypoint2lon']))
waypoint2name = config['default']['waypoint2name']
waypoint3 = (float(config['default']['waypoint3lat']), float(config['default']['waypoint3lon']))
waypoint3name = config['default']['waypoint3name']
pingdist = int(config['default']['distance'])
print('channelid: ' + str(channelid))
print('roleid: ' + str(roleid))
print('waypoint1: ' + waypoint1name + ' ' + str(waypoint1) + ' ' + str(type(waypoint1)))
print('waypoint2: ' + waypoint2name + ' ' + str(waypoint2) + ' ' + str(type(waypoint2)))
print('waypoint3: ' + waypoint3name + ' ' + str(waypoint3) + ' ' + str(type(waypoint3)))
print('pingdist: ' + str(pingdist) + ' miles')
print('looptime: ' + str(looptime) + ' seconds')
# Discord Bot
client = discord.Client()
@client.event
async def on_ready(): # Yes this is bad, I know... Tell me how to do it better
print('------------')
print('logged in as {0.user}'.format(client))
print('------------')
# Setup
channel = client.get_channel(channelid)
d = feedparser.parse('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.atom')
modified = d.modified
try:
oldid = d.entries[0].id
except:
print('no earthquakes in last hour')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="USGS Earthquakes"))
#print(d)
# Main Loop
while True:
# Parser
d = feedparser.parse('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.atom', modified=modified)
try:
if d.status == 304 or d.entries[0].id == oldid:
#print('unchanged')
await asyncio.sleep(looptime)
continue
except:
await asyncio.sleep(looptime)
continue
modified = d.modified
print()
print('--updated at ' + modified)
quakecords = (float(d.entries[0].where.coordinates[1]), float(d.entries[0].where.coordinates[0]))
# Find Distance
distance1 = geopy.distance.distance(waypoint1, quakecords).miles
distance2 = geopy.distance.distance(waypoint2, quakecords).miles
distance3 = geopy.distance.distance(waypoint3, quakecords).miles
print(str("%.2f" % distance1) + ' miles away --- ' + str(d.entries[0].title) + ' ' + str(quakecords))
# Send Message if within distance
if distance1 < pingdist or distance2 < pingdist or distance3 < pingdist:
print('Under ' + str(pingdist) + ' miles sending Discord Message!!')
depth = abs(float(d.entries[0].georss_elev))
depth /= 1000.
await channel.send('<@&' + roleid + '>\n**' + str(d.entries[0].title) + '**\n`' + str("%.2f" % distance1) + '` miles from ' + waypoint1name + '\n`' + str("%.2f" % distance2) + '` miles from ' + waypoint2name + '\n`' + str("%.2f" % distance3) + '` miles from ' + waypoint3name + '\nSummary: ' + d.entries[0].summary +'\n' + 'Time: `' + d.entries[0].updated + '`\nDepth: `' + str(depth) + ' Km`\nLocation: `' + str(quakecords) + '`\n' + d.entries[0].link)
oldid = d.entries[0].id
await asyncio.sleep(looptime)
client.run(bottoken)