This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linqz.py
executable file
·156 lines (140 loc) · 4.08 KB
/
linqz.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
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python3
#
# Linqz Discord Bot Link and Info Getter
# Copyright (c) 2017 Rachael Alexanderson
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import discord
import asyncio
import aiohttp
import urllib
from urllib.parse import urlparse
from django.core.exceptions import ValidationError
from django.core.validators import URLValidator
from bs4 import BeautifulSoup
client = discord.Client()
url = URLValidator()
useragent = 'Mozilla/5.0 (compatible; linqz/0.0; +https://github.com/raa-eruanna/linqz)'
bottoken = 'INSERT BOT TOKEN HERE'
whitelist = {
"zdoom.org",
"drdteam.org",
"tdgmods.com",
#"zandronum.com",
#"doomworld.com",
#"doomwiki.org",
}
@client.event
async def on_ready():
print('---Logged in as---')
print(client.user)
print(client.user.id)
appdata = await client.application_info()
client.owner = appdata.owner
print('-----My Owner-----')
print(client.owner)
print(client.owner.id)
print('------------------')
@client.event
async def on_message(message):
link = ''
for x, value in enumerate(message.content.split()):
try:
url(value)
iswhitelisted = 0
parsed_uri = urlparse(value)
parsed_domain = parsed_uri.hostname
for y, domainlisting in enumerate(whitelist):
if domainlisting == parsed_domain: iswhitelisted = 1
if parsed_domain.endswith('.' + domainlisting): iswhitelisted = 1
if iswhitelisted == 1:
try:
print("Accepted ({}) #{} {} > {}".format(message.guild.name, message.channel, message.author, value))
except:
print("Accepted {} > {}".format(message.author, value))
link = value
else:
try:
print("Rejected ({}) #{} {} > {}".format(message.guild.name, message.channel, message.author, value))
except:
print("Rejected {} > {}".format(message.author, value))
except ValidationError:
pass
if (link != ''):
tmp = await message.channel.send('URL detected')
async with aiohttp.ClientSession() as session:
async with session.get(
link,
headers={
'User-Agent': useragent
}
) as resp:
try:
if resp.status == 200:
soup = BeautifulSoup(await resp.text(), "lxml")
title = soup.title.string
try:
metadescription = soup.find(
'meta',
attrs={'name':'description'}
)
description = metadescription["content"]
except:
description = 'No description'
try:
firstimagesrc = soup.find(
'img',
attrs={'name':'src'}
)
firstimage = firstimagesrc["content"]
except:
firstimage = ''
await tmp.edit(
content = "Link `{}` sent by `{}`".format(
link,
message.author.name
),
embed = discord.Embed(
title = title,
type = "rich",
description = description,
url = link,
thumbnail = firstimage
)
)
else:
await tmp.edit(
content = "Link `{}` sent by `{}`".format(
link,
message.author.name
),
embed = discord.Embed(
title = "Error {}".format(resp.status),
type = "rich",
description = resp.reason
)
)
except BaseException as e:
print(repr(e), flush = False)
await tmp.edit(
content = "Error retrieving URL `{}` from `{}`!".format(
link,
message.author.name
)
)
if (message.author.id == client.owner.id):
if message.content.startswith('!quit'):
await message.channel.send('Logging off, {}!'.format(message.author.name))
await client.close()
client.run(bottoken)