Error Implementing free dictionary Api in discord.py - discord.py

I am trying to implement this api but the code says error every single time.
My code so far:
#client.command()
async def mean(ctx,word):
response = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}")
if response.status_code == 404:
await ctx.send("No such word")
return
else:
wordx = response.json()
the_dictionary = wordx[0]
meanings = the_dictionary['meanings']
definitions = meanings[0]
definition = definitions['definitions']
meaningg = definition[0]
meaning = meaningg['definition']
example = meaningg.get('example',['None'])
synonymslist = meaningg.get("synonyms",['None'])
if isinstance(synonymslist,str):
synonymslist = [synonymslist]
pass
synonyms = ','.join(synonymslist)
deffinal= discord.Embed(title=f"`{word.upper()}`")
deffinal.add_field(name = "Definition", value=f"{meaning}")
deffinal.add_field(name = 'Example', value = f"{example}")
deffinal.add_field(name = "Synonyms", value = f"{synonyms}")
await ctx.channel.send(embed = deffinal)
Here is the error message:

Your error does not come from the API call to the dictionary api, but rather from your call to the discord api.
The error message says discord.errors.HTTPException: [...] In embed.fields.2.value: This field is required.
So the error comes from an empty field in your embed! The field has index 2 so it is actually the third field (Synonyms) which is causing the problem.
You can simply check if a string is empty, before even adding the field. And if it is empty, just don't add it.
deffinal= discord.Embed(title=f"`{word.upper()}`")
if meaning:
deffinal.add_field(name = "Definition", value=f"{meaning}")
if example:
deffinal.add_field(name = 'Example', value = f"{example}")
if synonyms:
deffinal.add_field(name = "Synonyms", value = f"{synonyms}")
#client.command()
async def mean(ctx,word):
response = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}")
if response.status_code == 404:
await ctx.send("No such word")
return
else:
wordx = response.json()
the_dictionary = wordx[0]
meanings = the_dictionary['meanings']
definitions = meanings[0]
definition = definitions['definitions']
meaningg = definition[0]
meaning = meaningg['definition']
example = meaningg.get('example',['None'])
synonymslist = meaningg.get("synonyms",['None'])
if isinstance(synonymslist,str):
synonymslist = [synonymslist]
pass
synonyms = ','.join(synonymslist)
deffinal= discord.Embed(title=f"`{word.upper()}`")
if meaning:
deffinal.add_field(name = "Definition", value=f"{meaning}")
if example:
deffinal.add_field(name = 'Example', value = f"{example}")
if synonyms:
deffinal.add_field(name = "Synonyms", value = f"{synonyms}")
await ctx.channel.send(embed = deffinal)

Adding to itzFlubby answer, using requests will be blocking read more which means if you block for too long then your bot will freeze since it has not stopped the function’s execution at that point to do other things.
Here is the final code, also I changed the format a bit to make it easier to read.
# import aiohttp
#bot.command()
async def mean(ctx, word):
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}") as r:
if r.status == 200:
info = await r.json()
else:
return await ctx.reply("No such word")
the_dictionary = info[0]['meanings'][0]['definitions'][0]
definition = the_dictionary.get('definition')
example = the_dictionary.get('example')
synonymslist = the_dictionary.get("synonyms")
# if only one synonym is avaliable
if isinstance(synonymslist, str):
synonymslist = [synonymslist]
synonyms = '\n'.join(synonymslist)
deffinal = discord.Embed(title=f"`{word.upper()}`")
if definition:
deffinal.add_field(name="Definition", value=f"```{definition}```")
if example:
deffinal.add_field(name='Example', value=f"```{example}```")
if synonyms:
deffinal.add_field(name="Synonyms", value=f"```{synonyms}```")
await ctx.reply(embed=deffinal)

Related

Change discord.py embed field position

Is it possible to change the position of an embed field in discord.py?
Here is my more detailed problem:
I have an embed, but I have to make it modular, which means that with a command we can either add a field or delete one. The delete command works and the add command works, but now I need to be able to change the position of the field to be the same as the position of the role (because each field corresponds to a role).
How can I do that?
We can't edit embed field's position, but we can try to add every fields in the good ordor so we don't have to edit the field's position !
Here are my codes (working):
import asyncio
import discord
from discord.ext import commands
from datetime import datetime
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "!1789", help_command = None, intents=intents)
async def get_hour():
now = ""
now = datetime.now()
hour = now.strftime("%H:%M")
return hour
async def role_members_mention(role: discord.Role, emoji):
role_str = ""
if len(role.members):
for member in role.members:
role_str += f"**|** {emoji} **➜** {member.mention}\n"
role_str[:-1]
return role_str
async def soon(role, emoji, max):
soon = f"**|** {emoji} **➜ Soon**\n" * (int(max[role.name].replace("/", "")) - len(role.members))
soon[:-1]
return soon
async def get_all_roles():
guild = bot.get_guild(966749409314504774)
global ceo_aceo_roles_dict
ceo_aceo_roles_dict = {}
ceo = discord.utils.get(guild.roles, id = 966749523949023262)
aceo = discord.utils.get(guild.roles, id = 992137775883243530)
global all_roles_dict
all_roles_dict = {}
all_roles_dict[ceo.position] = ceo
all_roles_dict[aceo.position] = aceo
ceo_aceo_roles_dict[ceo.position] = ceo
ceo_aceo_roles_dict[aceo.position] = aceo
async def update_effective():
ceo_aceo = ""
embed = discord.Embed(title = "**E͟͟͟F͟͟͟F͟͟͟E͟͟͟C͟͟͟T͟͟͟I͟͟͟F͟͟͟ S͟͟͟T͟͟͟A͟͟͟F͟͟͟F͟͟͟**. <:3446blurplecertifiedmoderator:1001403463214825542>", description = "", color = 0x000000)
max = {}
total = {}
effectif_staff_emojis = ["<:adcrown:1001400557073866762>", "<:bluesettings:1001472768161894532>", "<:diplome:1001472522811883621>", "<:5961blurpleemployee:1001403806828994570>"]
roles_staff_emojis = ["<:5961blurpleemployee:1001403806828994570>", "<:Dev:1001412860091580487>", "<:direction:1001476149051924591>", "<:bluevoicechannel:1001473637091659836", "<:direction:1001476149051924591>", "<:spideyn7:996331019735158864>", "<:logo_twitch:1005196891010629702>"]
all_roles = sorted(all_roles_dict)
ceo_aceo_roles = sorted(ceo_aceo_roles_dict)
all_roles.reverse()
ceo_aceo_roles.reverse()
x = -1
for role in all_roles:
x += 1
all_roles[x] = all_roles_dict[role]
x = -1
for role in ceo_aceo_roles:
x += 1
ceo_aceo_roles[x] = ceo_aceo_roles_dict[role]
for role in all_roles:
number = 0
for car in role.name:
if car == "‎":
number += 1
max[role.name] = "/" + str(number)
if len(role.members) >= int(max[role.name].replace("/", "")):
total[role.name] = f"🔒 **{str(len(role.members))} {max[role.name]}**"
else:
total[role.name] = f"🔓 **{str(len(role.members))} {max[role.name]}**"
message = await (await get_effective_staff_channel()).fetch_message(1059250505689337998)
for role in ceo_aceo_roles:
ceo_aceo += f"{role.mention} {total[role.name]}\n{await role_members_mention(role, effectif_staff_emojis[0])}{await soon(role, effectif_staff_emojis[0], max)}\n\n"
embed.add_field(name = "**C.E.O & A.C.E.O** <:adcrown:1001400557073866762>", value = ceo_aceo, inline = False)
embed.set_thumbnail(url = "https://media.discordapp.net/attachments/1001404772496187392/1059246720669733014/Picsart_22-10-19_21-20-35-971.jpg?width=1326&height=529")
embed.set_image(url = "https://media.discordapp.net/attachments/1001404772496187392/1059246720875241542/Picsart_22-10-13_21-14-41-363.jpg?width=618&height=618")
await message.edit(embed = embed)
async def delete_messages(ctx, number_of_message_to_delete = 1, time_to_wait = 0):
await asyncio.sleep(time_to_wait)
await ctx.channel.purge(limit=number_of_message_to_delete)
#bot.event
async def on_ready():
print(f"[{await get_hour()}] Ready !\n Username: {bot.user.name}\n----------------------------------------------------")
await get_all_roles()
await update_effective()
while True:
await asyncio.sleep(600)
await update_effective()
#bot.command()
#commands.has_permissions(administrator = True)
async def addeffectifstaff(ctx, role: discord.Role, number = 1):
await delete_messages(ctx)
role = discord.utils.get(ctx.guild.roles, id = role.id)
if role in all_roles_dict.values():
await ctx.send("Désolé mais ce rôle est déjà dans l'effectif.")
else:
ceo_aceo_roles_dict[role.position] = role
all_roles_dict[role.position] = role
await update_effective()
await ctx.send(f"Bien ! Le rôle {role.name} a été ajouté à l'effectif !")
await delete_messages(ctx, 1, 3)
print(f'[{await get_hour()}] {ctx.author.name} used the command "addeffectifstaff" to make me add the role "{role.name}" to the effective message.')
#bot.command()
#commands.has_permissions(administrator = True)
async def deleffectif(ctx, role: discord.Role):
await delete_messages(ctx)
role = discord.utils.get(ctx.guild.roles, id = role.id)
if not role in all_roles_dict.values():
await ctx.send("Désolé mais ce rôle n'est déjà pas dans l'effectif.")
else:
ceo_aceo_roles_dict.pop(role.position)
all_roles_dict.pop(role.position)
await update_effective()
await ctx.send(f"Bien ! Le rôle {role.name} a été supprimé de l'effectif !")
await delete_messages(ctx, 1, 3)
print(f'[{await get_hour()}] {ctx.author.name} used the command "deleffectif" to make me remove the role "{role.name}" to the effective message.')
bot.run(token)

discord py 'keyerror' (discord components, slash commands)

I'm trying to use the bot by adding a slash command and a button. For once, the slash command is working as intended. But every time I run the command, I get a warning like that. I'm not sure what the problem is. It's a part of the code.
Ignoring exception in on_socket_response
Traceback (most recent call last):
File "/home/yuna21724/.local/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "/home/yuna21724/.local/lib/python3.8/site-packages/discord_buttons_plugin/__main__.py", line 17, in soclistener
await self.emit(data["data"]["custom_id"], data)
KeyError: 'custom_id'
import discord
import asyncio
import SeagamiCommandsListening
import SeagamiNotice
import SeagamiCharacter
from discord_buttons_plugin import *
from discord.ext import commands
from discord_buttons_plugin import *
from discord_slash import SlashCommand
from discord import Intents
from discord_components.client import DiscordComponents
token = ""
seagami = commands.Bot(command_prefix='.', intents = Intents.all())
slash = SlashCommand(seagami, sync_commands = True)
buttons = ButtonsClient(seagami)
#seagami.event
async def on_ready():
DiscordComponents(seagami)
await seagami.change_presence(status=discord.Status.online)
print('[알림]디스코드 봇 서비스가 시작되었습니다...',
seagami.user.name)
while True:
if SeagamiNotice.a['title'] == SeagamiNotice.b['title']:
print('')
else:
embed = discord.Embed(color = 0x2F3136)
embed.add_field(name = "```🌷로스트아크 공지사항```",
value = "``🌷로스트아크 점검 시, 봇의 기능이 제한됩니다.``", inline =
False)
embed.add_field(name = f"```👑
{SeagamiNotice.a['title']}```", value = '``📕 추가 정보를 확인하려면
아래의 바로가기 버튼을 눌러주세요.``', inline = False)
components_notice = [Button(label = "바로가기", style =
5, url = f"{SeagamiNotice.a['Link']}")]
await
seagami.get_channel(int(878004091505221643)).send(embed = embed,
components = components_notice)
await asyncio.sleep(335)
#seagami.event
async def on_member_join(member):
message_ = "<#{}>".format(str(member.id))
embed = discord.Embed(color = 0x2F3136)
embed.add_field(name = "```🌷환영해요.```", value = f"``🌷
``{message_}``님, 새가미와 아이들에 오신 걸 환영합니다.``", inline =
False)
buttons = [Button(label = "역할 받기", style = 2, custom_id =
"role")]
await seagami.get_channel(int(879993490996600852)).send(embed =
embed, components = buttons)
await
member.add_roles(seagami.get_guild(int(870543165004849152)).get_role(int(
870552731419566111)))
#slash.slash(name="입장", description="새가미 봇이 음성채널로 이동합니
다.", guild_ids=[870543165004849152])
async def in_voice(ctx: SlashCommand):
try:
if (ctx.channel.id == int(878003511961464883)):
await
SeagamiCommandsListening.commands_Listening.in_(ctx)
await ctx.reply('``🌷``새가미 봇이 음성채널로 이동하였습니
다.')
except:
await ctx.reply('``🌷``해당 명령어는 여기서 사용할 수 없습니
다.')
#slash.slash(name="퇴장", description="새가미 봇이 음성채널에서 퇴장
합니다.", guild_ids=[870543165004849152])
async def out_voice(ctx: SlashCommand):
try:
if (ctx.channel.id == int(878003511961464883)):
await
`enter code here`SeagamiCommandsListening.commands_Listening.out_(ctx)
await ctx.reply('``🌷``새가미 봇이 음성채널에서 퇴장하였습
니다.')
except:
await ctx.reply('``🌷``해당 명령어는 여기서 사용할 수 없습니
다.')
#slash.slash(name="재생", description="새가미 봇을 통해 노래를 재생할
수 있습니다.", guild_ids=[870543165004849152])
async def play_voice(ctx: SlashCommand, 링크):
try:
if (ctx.channel.id == int(878003511961464883)):
await
SeagamiCommandsListening.commands_Listening.play_(ctx, 링크)
await ctx.reply('``🌷``신청하신 노래를 ``1회``재생 합니
다.')
await buttons.send(
content = "아래의 버튼을 눌러 ``👑명령어``를 사용하실 수 있습니다.",
channel = ctx.channel.id,
components = [
ActionRow([
Button(label = "일시 정지", style = 1, custom_id = "button_pause"),
Button(label = "다시 듣기", style = 3, custom_id = "button_resume"),
Button(label = "그만 듣기", style = 4, custom_id = "button_stop")
]
)])
except:
await ctx.reply('해당 명령어는 여기서 사용할 수 없습니다.')
#slash.slash(name="반복재생", description="새가미 봇을 통해 노래를 반복 재생할 수 있습니다.", guild_ids=[870543165004849152])
async def replay_voice(ctx: SlashCommand, 링크):
try:
if (ctx.channel.id == int(878003511961464883)):
await SeagamiCommandsListening.commands_Listening.replay_(ctx, 링크)
await ctx.reply('``🌷``신청하신 노래를 반복 재생 합니다.')
await buttons.send(
content = "아래의 버튼을 눌러 ``👑명령어``를 사용하실 수 있습니다.",
channel = ctx.channel.id,
components = [
ActionRow([
Button(label = "일시 정지", style = 1, custom_id = "button_pause"),
Button(label = "다시 듣기", style = 3, custom_id = "button_resume"),
Button(label = "그만 듣기", style = 4, custom_id = "button_stop")
]
)])
except:
await ctx.reply('해당 명령어는 여기서 사용할 수 없습니다.')
#slash.slash(name="검색", description="캐릭터 정보를 검색 할 수 있습니다.", guild_ids=[870543165004849152])
async def search_(ctx: SlashCommand, 캐릭터명):
if (ctx.channel.id == int(882916863779168267)):
await SeagamiCharacter.search_info.info_(ctx, 캐릭터명)
if SeagamiCharacter.code['code'] != '100':
embed = discord.Embed(color = 0x2F3136)
embed.add_field(name = f"> ```🌷{SeagamiCharacter.name_['name']}```", value = f"> ``🌷{SeagamiCharacter.class_info['class']}``", inline = False)
embed.add_field(name = "```📕캐릭터 레벨```", value = f"``Lv. {SeagamiCharacter.Level_['Level'][8:]}``", inline = True)
embed.add_field(name = "```📕원정대 레벨```", value = f"``Lv. {SeagamiCharacter.expedition_Level['expedition'][9:]}``", inline = True)
embed.add_field(name = "```📕아이템 레벨```", value = f"``Lv. {SeagamiCharacter.item_Level['item_Level'][12:]}``", inline = True)
embed.add_field(name = "> ```🔯특성```", value = "> ``🔯펫 효과가 적용 된 특성입니다.``", inline = False)
embed.add_field(name = "```🔯치명```", value = f"``{SeagamiCharacter.ability_battle['ability_battle1']}``", inline = True)
embed.add_field(name = "```🔯특화```", value = f"``{SeagamiCharacter.ability_battle['ability_battle3']}``", inline = True)
embed.add_field(name = "```🔯제압```", value = f"``{SeagamiCharacter.ability_battle['ability_battle5']}``", inline = True)
embed.add_field(name = "```🔯신속```", value = f"``{SeagamiCharacter.ability_battle['ability_battle7']}``", inline = True)
embed.add_field(name = "```🔯인내```", value = f"``{SeagamiCharacter.ability_battle['ability_battle9']}``", inline = True)
embed.add_field(name = "```🔯숙련```", value = f"``{SeagamiCharacter.ability_battle['ability_battle11']}``", inline = True)
embed.add_field(name = "> ```👑각인```", value = "> ``👑현재 사용중인 각인입니다.``", inline = False)
try:
for count in range(8):
embed.add_field(name = "```👑각인```", value = f"``{SeagamiCharacter.ability_engrave[f'ability_engrave{count}']}``", inline = True)
except:
components_ = [[
Button(label = "바로가기", style = 5, url = f'https://lostark.game.onstove.com/Profile/Character/{캐릭터명}')
]]
await ctx.reply(f'``🌷{캐릭터명}``검색이 완료되었습니다.')
await seagami.get_channel(int(882916863779168267)).send(embed = embed, components = components_)
else:
await ctx.reply('``🌷``로스트아크 점검 중 입니다.')
SeagamiCharacter.code['code'] = '200'
else:
await ctx.reply('해당 명령어는 여기서 사용할 수 없습니다.')
#buttons.click
async def button_pause(ctx):
await SeagamiCommandsListening.commands_Listening.pause_()
await ctx.reply('``🌷``노래가 일시 정지 되었습니다.')
#buttons.click
async def button_resume(ctx):
await SeagamiCommandsListening.commands_Listening.resume_()
await ctx.reply('``🌷``노래가 다시 재생 되고 있습니다.')
#buttons.click
async def button_stop(ctx):
await SeagamiCommandsListening.commands_Listening.stop_()
await ctx.reply('``🌷``재생중인 노래를 종료 하였습니다.')
seagami.run(token)
enter image description here
enter image description here
enter image description here
enter image description here
A KeyError is raised when a mapping (dictionary) key is not found in the set of existing keys.
If you're unsure why you're getting a KeyError, here are some things you can do
Print out the dictionary to see what's in it
Check that the key is what you intend it to be
If you want to provide a default for a value when the key isn't in the dictionary, you can use dict.get which defaults to None
>>> person["pets"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pets'
>>> person.get("pets")
>>> person.get("pets", [])
[]
Docs:
https://docs.python.org/3/library/exceptions.html#KeyError%3E
https://docs.python.org/3/library/stdtypes.html#dict.get%3E
Helpful link: https://realpython.com/python-keyerror/%3E
I believe the cause of your error was the 3rd party package you installed called discord_buttons_plugin, which as far as I can tell requires you to add a custom_id to all buttons except the ButtonType().Link. In your _search command, you've forgot to add it, which causes an error in the package's code. Try adding a custom_id to the button there.
Long Story Short
Try adding a custom_id in your button component in the _search command.

Google Users: List users data of a specific group

I am in the need of listing the users data belonging to a specific group within the organization. The documentation does not specify if this is possible. I was really hoping there could be some kind of query that would allow this. For example email in (1#domain.com,2#domain.com). However, I don't see that being possible. The only way I could think to accomplish this would be:
Get a list of all the members in the group (https://developers.google.com/admin-sdk/directory/reference/rest/v1/members/list)
Get each user data by email (https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/get)
The problem with the above approach is that if a group contains 50+ members, this means that I have to make all that amount of requests, which is counter productive. Imagine how long that would take.
Any ideas? Greatly appreciate it.
Unfortunately I don’t think you can skip this two step process, but you can speed it up using batch requests. This
allows you to request up to 1000 calls in a single request. The steps would be:
Make a batch request to get all the members of all the groups you want (using members.list).
Make a batch request to get all the user info that you need using their id (using user.get).
Notice that the data in the result won’t be sorted, but they will be tagged by Content-ID.
References
Sending Batch Requests (Directory API)
Method: members.list (Directory API)
Method: users.get (Directory API)
I thought about the batching request a couple of hours after I posted the question. The problem with Node JS is that it does not has built in support for batch requests, unlike the php client library for example; Therefore, I had to spent some time implementing support for it on my own since I was not able to find any example. I'll share the solution in case it helps someone else or for my future reference.
async function getGroupMembersData(){
const groupEmail = "group#domain.com"; //google group email
const groupMembers = await getGroupMembers(groupEmail).catch(error=>{
console.error(`Error querying group members: ${error.toString()}`);
});
if(!groupMembers){ return; }
const url = "https://www.googleapis.com/batch/admin/directory_v1";
const scopes = ["https://www.googleapis.com/auth/admin.directory.user.readonly"];
const requests = [];
for(let i=0; i<groupMembers.length; ++i){
const user = groupMembers[i];
const request = {
email: user,
endpoint: `GET directory_v1/admin/directory/v1/users/${user}?fields=*`
};
requests.push(request);
}
const batchRequestData = await batchProcess(url, scopes, requests).catch(error=>{
console.error(`Error processing batch request: ${error.toString()}`);
});
if(!batchRequestData){ return; }
const usersList = batchRequestData.map(i=>{
return i.responseBody;
});
console.log(usersList);
}
//get group members using group email address
async function getGroupMembers(groupKey){
const client = await getClient(scopes); //function to get an authorized client, you have to implement on your own
const service = google.admin({version: "directory_v1", auth: client});
const request = await service.members.list({
groupKey,
fields: "members(email)",
maxResults: 200
});
const members = !!request.data.members ? request.data.members.map(i=>i.email) : [];
return members;
}
//batch request processing in groups of 100
async function batchProcess(batchUrl, scopes, requests){
const client = await getClient(scopes); //function to get an authorized client, you have to implement on your own
let results = [];
const boundary = "foobar99998888"; //boundary line definition
let batchBody = ""; const nl = "\n";
const batchLimit = 100; //define batch limit (max supported = 100)
const totalRounds = Math.ceil(requests.length / batchLimit);
let batchRound = 1;
let batchItem = 0;
let roundLimit = batchLimit;
do{
roundLimit = roundLimit < requests.length ? roundLimit : requests.length;
//build the batch request body
for(batchItem; batchItem<roundLimit; batchItem++){
const requestData = requests[batchItem];
batchBody += `--${boundary}${nl}`;
batchBody += `Content-Type: application/http${nl}`;
batchBody += `Content-Id: <myapprequest-${requestData.email}>${nl}${nl}`;
batchBody += `${requestData.endpoint}${nl}`;
}
batchBody += `--${boundary}--`;
//send the batch request
const batchRequest = await client.request({
url: batchUrl,
method: "POST",
headers: {
"Content-Type": `multipart/mixed; boundary=${boundary}`
},
body: batchBody
}).catch(error=>{
console.log("Error processing batch request: " + error);
});
//parse the batch request response
if(!!batchRequest){
const batchResponseData = batchRequest.data;
const responseBoundary = batchRequest.headers["content-type"].split("; ")[1].replace("boundary=", "");
const httpResponses = batchResponseParser(batchResponseData, responseBoundary);
results.push(...httpResponses);
}
batchRound++;
roundLimit += batchLimit;
} while(batchRound <= totalRounds);
return results;
};
//batch response parser
function batchResponseParser(data, boundary){
const nl = "\r\n";
data = data.replace(`--${boundary}--`,"");
const responses = data.split(`--${boundary}`);
responses.shift();
const formattedResponses = responses.map(i=>{
const parts = i.split(`${nl}${nl}`);
const responseMetaParts = (parts[0].replace(nl, "")).split(nl);
let responseMeta = {};
responseMetaParts.forEach(part=>{
const objectParts = part.split(":");
responseMeta[objectParts[0].trim()] = objectParts[1].trim();
});
const responseHeadersParts = parts[1].split(nl);
let responseHeaders = {};
responseHeadersParts.forEach(part=>{
if(part.indexOf("HTTP/1.1") > -1){
responseHeaders.status = part;
} else {
const objectParts = part.split(":");
responseHeaders[objectParts[0].trim()] = objectParts[1].trim();
}
});
const reg = new RegExp(`${nl}`, "g");
const responseBody = JSON.parse(parts[2].replace(reg, ""));
const formatted = {
responseMeta: responseMeta,
responseHeaders: responseHeaders,
responseBody: responseBody
};
return formatted;
});
return formattedResponses;
}

Post request for bulk API is giving status code of 406, How to resolve it?

I am using Elastic search 6.1 version
My data is appending correctly and I am adding '\n' at the end of the request.
My code is as follows:
def insert_in_bulk(self, filee, rtype):
U = urljoin(self.args.host, "/_bulk")
body = []
f = open(filee)
for line in f:
action = {
'index' :{
'_index' : self.args.index,
'_type' : rtype,
}
}
item = {
'word' : line.strip()
}
body.append(json.dumps(action))
body.append(json.dumps(item))
f.close()
body = '\n'.join(body)+'\n'
success = False
try:
r = requests.post(U, data=body)
self.log.info("after request")
if r.status_code == 200:
success = True
r = r.json()
self.log.info("inserted %s items of type = %s", self.args.index , rtype)
except (SystemExit, KeyboardInterrupt): raise
except:
self.log.exception("during bulk index")
if not success:
self.log.error("failed to index records of type = %s", rtype)
I am using the python to connect to elastic search.
I got the answer from this link
Bulk index document from JSON file into ElasticSearch
I have to pass the header to the request as application/x-ndjson.
Though it is quite some time question is asked, but i want to give a solution that has worked for me in most case,
def insert_in_bulk(self, filee, rtype):
U = urljoin(self.args.host, "/_bulk")
body = []
f = open(filee)
for line in f:
action = {
'index' :{
'_index' : self.args.index,
'_type' : rtype,
}
}
item = {
'word' : line.strip()
}
body.append(json.dumps(action))
body.append(json.dumps(item))
f.close()
payload = ""
for l in body:
payload = payload + f"{l} \n"
data = payload.encode('utf-8')
r = requests.post(U, data=data, headers={"Content-Type": "application/x-ndjson"})
print(r.text)

Lambda Expressions: How to analyze how a delegate is stored as an Expression?

This code:
var lambda = Products.Where( p => p.name == "chair");
can be written like this code:
var propertyName = "name";
var value = "chair";
var arg = Expression.Parameter(typeof(Product), "p");
var property = typeof(Product).GetProperty(propertyName);
var comparison = Expression.Equal(
Expression.MakeMemberAccess(arg, property),
Expression.Constant(value));
var lambda = Expression.Lambda<Func<Product, bool>>(comparison, arg).Compile();
If I have any Lambda expression like this:
Products.Where( p => p.name.Contains("chair") );
How could I determine how to write the Expression like above? Is there a way to "debug" the expression tree so that I can program it manually?
EDIT:
I saw promising answers here but it didn't end up with working code. Here's their version (the error is the StartsWith method is given a non-string value):
ParameterExpression p = Expression.Parameter(typeof(Product), "p");
MethodInfo method = typeof(string).GetMethod("StartsWith",
new[] { typeof(string) });
var containsMethodExp = Expression.Call(p, method,
Expression.Constant("root"), p);
Just let the compiler do the work.
If you instead of
func<string,bool> MyLambda = p => p.name.Contains("chair");
write
Expression<func<string,bool>> MyExpression = p => p.name.Contains("chair");
Then you get a nice "MyExpression" that you can inspect in a debugger.

Resources