-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.py
98 lines (81 loc) · 3.08 KB
/
news.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
94
95
96
97
98
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 12 18:50:47 2017
@author: clesiemo3
"""
import feedparser
import html2text
import requests
import html
import re
import sheets
import os
import datetime as dttm
from datetime import datetime as dt
import sendgrid
from sendgrid.helpers.mail import *
# py files
import auth_flow
def main():
sep = "############################################"
message = "Weekly Announcements<br>Generated by Bot - Report issues in comments<br>"
message += sheets.ya_schedule() + "<br>"
try:
sermon = feedparser.parse("http://www.efree.org/sermons/feed/").entries[0]
vimeo_link = re.findall(r"https://player.vimeo.com/video/[0-9]+", sermon['content'][0]['value'])[0]
sermon_string = "Last Week's Sermon - {0} - {1} <br>Link: {2} <br>Vimeo: {3} <br>MP3 download: {4} <br>"
message += sermon_string.format(sermon.title, sermon.author, sermon.link, vimeo_link, sermon.links[1]['href'])
# message += "<br>{sep}<br>{sep}<br>".format(sep=sep)
except Exception as e:
print(e)
pass
# facebook
group_id = os.environ['fb_group_id']
now = dt.now()
buffer = dttm.timedelta(days=31)
until = now + buffer
efree_mask = '%Y-%m-%d %H:%M:%S'
output_mask = '%A %B %d %I:%M %p'
message += "<br>Efree Events<br>" + sep + "<br>" + sep + "<br>"
exclude = re.compile("(Junior High Spring Retreat|Senior High|KampOut)")
# efree
efree_events = "http://www.efree.org/wp-json/wp/v2/posts/"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
}
events = requests.get(efree_events, headers=headers)
h = html2text.HTML2Text()
h.ignore_links = True
h.ignore_emphasis = True
for x in events.json():
# event category number
if 35 in x.get("categories"):
msg_x = "<br>" + x.get("title", {}).get("rendered", "Event Name")
msg_x += "<br>" + x.get("link", "")
msg_x += h.handle(x.get("excerpt", {}).get("rendered", ""))
msg_x += "<br>" + sep + "<br>"
if exclude.search(msg_x):
print("Excluded!")
print(msg_x)
continue
else:
message += msg_x
message = html.unescape(message)
if eval(os.environ['print_only']):
print(message)
else:
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
from_email = Email("[email protected]")
to_email = Email(os.environ['send_email'])
subject = "Young Adults News - {0}".format(now.date().isoformat())
content = Content("text/html", message)
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
if __name__ == '__main__':
main()