-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (56 loc) · 2.31 KB
/
main.py
File metadata and controls
75 lines (56 loc) · 2.31 KB
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
import asyncio
import json
import os
import pprint
import time
import requests
import random
import pytz
from birdbuddy.feed import FeedNodeType
from birdbuddy.feeder import FeederState
from slack_sdk import WebClient
from dotenv import load_dotenv
from birdbuddy.client import BirdBuddy
from datetime import datetime
async def main():
print(">> Start")
bb = BirdBuddy(os.getenv('BIRD_BUDDY_EMAIL'), os.getenv('BIRD_BUDDY_PASSWORD'))
slack_client = WebClient(token=os.getenv('SLACK_TOKEN'))
slogans = json.load(open('slogans.json'))
since: datetime = datetime.now(tz=pytz.utc)
while True:
sleep_time = await get_sleep_time(bb)
print(">> Sleep time in seconds:", sleep_time)
time.sleep(sleep_time)
print(">> Call BirdBuddy.feed() and filter...")
feeds = (await bb.feed(50)).filter([FeedNodeType.SpeciesSighting], newer_than=since)
# pprint.pprint(feeds)
since = datetime.now(tz=pytz.utc)
print(">> Found items in feed:", len(feeds))
for feed in feeds:
# pprint.pprint(feed)
name = 'Mystery Visitor'
if 'species' in feed['collection']:
name = feed['collection']['species']['name']
url = feed['media']['contentUrl']
print(">> Upload Bird to slack:", url)
# https://www.tutorialspoint.com/downloading-files-from-web-using-python
r = requests.get(url, allow_redirects=True)
open('bird.jpg', 'wb').write(r.content)
# https://plazagonzalo.medium.com/send-messages-to-slack-using-python-4b986586cb6e
# https://stackoverflow.com/questions/62229535/uploading-image-to-slack-channel-with-python-script
response = slack_client.files_upload(
file='bird.jpg',
initial_comment="{}: {}".format(name, random.choice(slogans)),
channels=os.getenv('SLACK_CHANNEL_ID')
)
async def get_sleep_time(bb: BirdBuddy) -> int:
print(">> Call BirdBuddy.refresh()")
await bb.refresh()
if next(iter(bb.feeders.values())).state == FeederState.DEEP_SLEEP:
# Bird Buddy is deep sleeping.... zzZZzz
return int(os.getenv('DEEP_SLEEP_TIME'))
return int(os.getenv('SLEEP_TIME'))
if __name__ == '__main__':
load_dotenv()
asyncio.run(main())