-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshashi.py
146 lines (124 loc) · 3.84 KB
/
shashi.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# t.me/ReQuestChat
# Copyright (C) 2021 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
""" logging things """
import logging
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s - %(levelname)s] - %(name)s - %(message)s",
datefmt='%d-%b-%y %H:%M:%S',
handlers=[
logging.StreamHandler()
]
)
def LOGGER(name: str) -> logging.Logger:
""" get a Logger object """
return logging.getLogger(name)
""" mandatory imports """
import asyncio
from bs4 import BeautifulSoup
from json import dumps
from urllib.parse import urlencode
""" bloated imports """
import aiohttp
import os
from typing import Dict
""" helper function to make an HTTP request """
async def fetch_website(
input_url: str,
custom_headers: Dict = None
) -> str:
# LOGGER(__name__).info(input_url)
second_response = None
async with aiohttp.ClientSession() as session:
try:
one_response = await session.get(
input_url,
headers=custom_headers
)
second_response = await one_response.text()
except: # noqa: E722
return False
# LOGGER(__name__).info(second_response)
return second_response
""" wrapper for getting the credentials """
def get_config(name: str, d_v=None, should_prompt=False):
""" accepts one mandatory variable
and prompts for the value, if not available """
val = os.environ.get(name, d_v)
if not val and should_prompt:
try:
val = input(f"enter {name}'s value: ")
except EOFError:
val = d_v
print("\n")
return val
""" fetch posts and returns in the required format """
async def ig(a: str):
io = "https://t.me/ReQuestChat"
F = get_config("F", "A B C D E F A B C D E F")
G = get_config("G", "G")
H = get_config("H", "H")
b = await fetch_website(
a,
{
H: G
}
)
hp = "html.parser"
bp1, cp, lp, pi, lta, otb, gmi, ilu, src, lui, pp, cc = F.split(" ")
f = []
s = BeautifulSoup(b, hp)
d = s.find(lui, class_=bp1)
if not d:
return f
e = d.find_all(ilu)
for g in e:
p = g.find(gmi, class_=pi)
j = g.find(otb, class_=lp).text.strip()
k = g.find(otb, class_=cp).text.strip()
i = p.get(src)
c = p.get(lta)
f.append({
"photo": i,
"caption": c,
"reply_markup": dumps({
"inline_keyboard": [
[
{
"text": f"{j} {pp}",
"url": io,
},
{
"text": f"{k} {cc}",
"url": io,
}
]
]
})
})
return f
async def shashi():
A = get_config("A", "A")
B = get_config("B", "B")
C = get_config("C", "C")
D = get_config("D", "D")
E = get_config("E", "E")
a = await ig(A)
for e in a:
e["chat_id"] = C
e["message_thread_id"] = E
c = f"{D}/bot{B}/sendPhoto?{urlencode(e)}"
await fetch_website(c)
asyncio.run(shashi())