-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyracuseNews.py
More file actions
85 lines (70 loc) · 2.81 KB
/
Copy pathSyracuseNews.py
File metadata and controls
85 lines (70 loc) · 2.81 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
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib.request as urllib2
import re
import pickle
import logging
import ssl
import urllib.parse
import base64
from datetime import datetime,timedelta
from time import sleep
import time
import os
import discord
from dotenv import load_dotenv
import requests
#import MySQLdb
from discord.ext.tasks import loop
#Initialize discord client
client = discord.Client()
#default directory for .env file is the current directory
#if you set .env in different directory, put the directory address load_dotenv("directory_of_.env)
load_dotenv()
#take environment variables from .env.
TOKEN = os.getenv('DISCORD_TOKEN')
#To strip HTML tags
def _remove_html_tags(html):
p = re.compile(r'<[^<]*?/?>')
return p.sub('', html)
if hasattr(ssl, '_create_unverified_context'):
ssl._create_default_https_context = ssl._create_unverified_context
#Create request and open URL to read
_opener = urllib2.build_opener()
_opener.addheaders = [('User-agent', 'Mozilla/5.0')]
_events_calendar = {}
_events_list_file = 'events.data'
_url = 'https://www.syracuse.com/'
logging.basicConfig(
filename='log_histopy.txt', level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s'
)
html = _opener.open(_url).read()
#Find articles and obtain neccessary information such as title, link, publish date..
articles = []
uls = BeautifulSoup(html, "html.parser").html.body.findAll('article')
for x in uls:
time1 = int(x.attrs['data-publishedon'])
dt_object = (datetime.fromtimestamp(time1) - timedelta(hours=5)).strftime('%Y-%m-%d %H:%M:%S')
atetime_object = datetime.strptime(dt_object, '%Y-%m-%d %H:%M:%S')
if ((atetime_object.hour == (datetime.today()- timedelta(hours=5)).hour) and (atetime_object.day == (datetime.today()- timedelta(hours=5)).day)) :
articles.append({'header':x.attrs['data-social-hed'],'link':x.attrs['data-source'],'thumb':x.attrs['data-image-thumb'],'publishedOnRaw':int(x.attrs['data-publishedon']), 'id': x.attrs['id']})
#Send articles to discord
if (len(articles) >= 1):
@loop(count=1)
async def send_articles():
channel = client.get_channel(#############)
for article in articles:
publishedOnRaw = article['publishedOnRaw']
publishedOn = (datetime.fromtimestamp(publishedOnRaw) - timedelta(hours=5))
embed=discord.Embed(title=article['header'], url=article['link'], description=str(publishedOn))
embed.set_thumbnail(url=article['thumb'])
await channel.send(embed=embed)
@send_articles.before_loop
async def before_send_articles():
await client.wait_until_ready() # Wait until bot is ready.
@send_articles.after_loop
async def after_send_articles():
await client.logout() # Make the bot log out.
send_articles.start()
client.run(TOKEN)