-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot_methods.py
74 lines (58 loc) · 2.26 KB
/
bot_methods.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
import wikipedia
from telebot import types
import bot_methods
import datetime
import generate_telegraph
import users_controller
import requests
from bs4 import BeautifulSoup
WIKI_PHOTO = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSEaF0CsI_3jjaTkV1Cv3f-WD4evU8EFuqcTUH8_-Fpcrkwo57kQA'
def parse_article(url):
page = requests.get(url)
html = page.text
title = url.split('/')[-1].replace('_', ' ')
soup = BeautifulSoup(html, 'lxml')
body_content = soup.find('div', class_='mw-parser-output')
return body_content, title
def parse_main_photo(article=None, body_content=None, lang=None):
if article:
url = 'https://{}.wikipedia.org/wiki/'.format(lang) + article.replace(' ', '_')
body_content, title = parse_article(url)
try:
main_photo = body_content.find('table', class_='infobox').find('a', class_='image').find('img')['src']
img_src = 'https:' + main_photo
return img_src
except:
print('no main photo')
return None
def get_photo_url(article, lang):
main_photo_url = parse_main_photo(article=article, lang=lang)
if main_photo_url:
print('yep')
return main_photo_url
wikipedia.set_lang(lang)
try:
page = wikipedia.page(article)
images = [image for image in page.images if 'jpg' in image]
if images:
return images[0]
except Exception as e:
print('[Exception] {}'.format(e))
return WIKI_PHOTO
def make_code_pretty(content):
new_content = BeautifulSoup(content, 'lxml')
for code_section in new_content.find_all('pre'):
code_section = str(code_section)[5:][:-6]
new_code_section = code_section.replace('<', '<').replace('>', '>')
new_content = str(new_content).replace(code_section, new_code_section)
return str(new_content)
def add_button(article, i, lang, queue):
queue.put(types.InlineQueryResultArticle(
id=str(i), title=article,
#description='test',
input_message_content=types.InputTextMessageContent(article),
#thumb_url=get_photo_url(article, lang),
thumb_width=48,
thumb_height=48
))
print('finished at:{}'.format(datetime.datetime.now()))