#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# notify about new poems of favouite autors
import os
import subprocess
import pickle
import requests
from bs4 import BeautifulSoup
from pytils import numeral
class Parser(object):
url = 'https://www.stihi.ru/'
form_data = {
'block': '',
'login':'LOGIN',
'password':'PASSWD'
}
def login(self):
s = requests.Session()
req = s.post(self.url + 'cgi-bin/login/intro.pl',
self.form_data)
html_content = s.get(self.url +
'poems/list.html?type=recommended')
return html_content
def search(self, html):
html = BeautifulSoup(html.text, 'html.parser')
soup = html.find(
text='Произведения ваших избранных авторов').findNext('ul')
soup = soup.findAll('li')
parsed = {}
for e in soup:
parsed[''.join([self.url, e.a['href']])] = \
' - '.join([e.em.get_text(), e.a.get_text()])
return parsed
def save(self, parsed):
with open('parsed.pickle', 'wb') as f:
pickle.dump(parsed, f)
def load(self):
if not os.path.exists('parsed.pickle'):
f = open('parsed.pickle', 'wb')
f.close()
try:
with open('parsed.pickle', 'rb') as f:
archive = pickle.load(f)
return archive
except EOFError:
return {}
def if_new(self, parsed, archive):
new = {}
for key, val in parsed.items():
if key not in archive:
new[key] = val
archive[key] = val
else:
new.clear()
self.save(archive)
return new
def notify_me(self, new):
linked = []
for key, val in new.items():
linked.append('<a href="{link}">{title}</a> \n'.format(
link=str(key),
title=str(val)))
def plural(num):
p_num = numeral.get_plural(len(num),
"новое произведение, \
новых произведения, \
новых произведений")
return p_num
ms = ''.join(linked)
message = []
message.append('notify-send')
message.append('--expire-time=10000')
message.append('stihi.ru')
message.append('\n'.join(['{0} <i>сегодня</i>',
ms]).format(str(plural(new.keys()))))
subprocess.call(message, shell=False)
if __name__ == '__main__':
parsing = Parser()
dict1 = parsing.search(parsing.login())
dict2 = parsing.load()
new = parsing.if_new(dict1, dict2)
if new:
parsing.notify_me(new)