-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamemc.py
154 lines (116 loc) · 4.46 KB
/
namemc.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
'''
LOW BUDGET TRELLO:
DONE:
- !check -- check the availility of a certain name
- !history -- get history of people with a certain name
THINGS TO DO:
- !list -- get the names with a max length
- !remind -- set up reminder for when a specific name becomes available
'''
# bot.py
import os
import random
import requests
from bs4 import BeautifulSoup
from discord.ext import commands
from dotenv import load_dotenv
import dateutil.parser
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
url = 'https://namemc.com/search?q='
url2 = 'https://namemc.com/minecraft-names?sort=asc&length_op=le&length=%s&lang=&searches=%s'
format = '''NAME SEARCHES/MONTH
DATE OF AVAILIBILITY
--------------------------------------'''
names = []
urls = []
bot = commands.Bot(command_prefix='!')
def get_name_status(name):
request = requests.get(url + name).text
soup = BeautifulSoup(request, 'html.parser')
if 'availability-time' in request:
for time in soup.find_all('time'):
if time['id'] == 'availability-time':
d = dateutil.parser.parse(time['datetime'])
formatted = d.strftime('%m/%d/%y, %H:%M:%S')
for searches in soup.find_all('div', class_='tabular'):
second = searches.contents[0]
return f'Time of Availability: {formatted}, Searches: {second}'
else:
for tag in soup.find_all('meta'):
if 'name="description"' in str(tag):
return tag['content']
def get_names(maxlength, search):
request = requests.get(url2 % (maxlength, search)).text
soup = BeautifulSoup(request, 'html.parser')
table = soup.find_all('div', class_='card-body p-0')[0]
for tag in table.find_all('div', class_='row no-gutters py-1 px-3 border-top'):
x = []
for tag2 in tag:
if tag2 != '\n':
if str(tag2.contents)[:2] == '<a':
print('hkljfdshglk')
contents = tag2.contents
x.append(str(contents)[contents.index('>'):-5])
else:
x.append(tag2.contents)
print(x)
print()
# !history <name>
def name_history(name):
global names, urls
format = 'Naming history:\n'
request = requests.get('https://namemc.com/search?q=' + name).text
soup = BeautifulSoup(request, 'html.parser')
names = soup.find_all('h3', class_='mb-0')
urls = soup.find_all('div', class_='card-header py-0')
if not names:
return format + 'Nobody has ever had this name!'
for index, name in enumerate(names):
format += f'{index+1}. {name.contents[0]}'
if index != len(names) - 1:
format += '\n'
format += '\n\nUse !select <number> to select a specific person'
return format
# !select <number>
def select(number):
try:
tag = urls[int(number) - 1]
except:
return 'Invalid number!'
format = f'History for {names[int(number) - 1].contents[0]}:\n'
subtag = tag.findChildren('a', recursive=False)[0]
link = subtag['href']
request = requests.get('https://namemc.com' + link).text
soup = BeautifulSoup(request, 'html.parser')
people = soup.find_all('div', class_='col order-lg-1 col-lg-4 text-nowrap')
times = soup.find_all('time')
knames = []
thimes = []
for person in people:
knames.append(person.findChildren('a', recursive=False)[0].contents[0])
for time in times:
thimes.append(time.contents[0])
for index, kname in enumerate(knames):
if index != len(knames) - 1:
format += f'{kname} -- {dateutil.parser.parse(thimes[index]).strftime("%m/%d/%y, %H:%M:%S")}\n'
else:
format += f'{kname} -- Unknown (account creation)\n'
return format
@bot.command(name='check', help='Checks availability of name')
async def available(ctx, name):
resp = get_name_status(name)
await ctx.send(resp)
@bot.command(name='list', help='Lists names with specified max length and popularity')
async def list_em(ctx, maxlength, search):
resp = get_names(maxlength, search)
await ctx.send(resp)
@bot.command(name='history', help='Returns the naming history of a certain name')
async def history(ctx, name):
resp = name_history(name)
await ctx.send(resp)
@bot.command(name='select', help='In response to !history, tells the history of a specific user you selected')
async def personjfhklfdsjhls(ctx, number):
resp = select(number)
await ctx.send(resp)
bot.run(TOKEN)