from datetime import datetime
import requests
from bs4 import BeautifulSoup
from web_app.model import db, News

def get_html(url):
    try:
        result = requests.get(url)
        result.raise_for_status()
        return result.text
    except(requests.Exception, ValueError):
        print('Сетевая ошибка')
        return False



def get_python_news():
    html = get_html('https://www.python.org/blogs/')
    if html:
        soup = BeautifulSoup(html, 'html.parser')
        all_news = soup.find('ul', class_='list-recent-posts')
        all_news = all_news.findAll('li')
        result_news = []
        for news in all_news:
            title = news.find('a').text
            url = news.find('a')['href']
            published = news.find('time').text
            print(type(published))
            try:
                published = datetime.strptime(published, '%B %d, %Y')
                print(published)
            except(ValueError):
                published = datetime.now()
            save_news(title, url, published)


def save_news(title, url, published):
    news_exists = News.query.filter(News.url == url).count()
    print(news_exists)
    if not news_exists:
        news_news = News(title=title, url=url, published=published)
        db.session.add(news_news)
        db.session.commit( )

if __name__ == '__main__':
    #html = get_html('https://www.python.org/blogs/')
    #if html:
        #with open('python.org.html', 'w', encoding='utf8') as f:
        #    f.write(html)
    #    news = get_python_news(html)
        print(news)


