def haveibeenpwned(self, name : str):
'''Check if your account has been breached'''
url = "https://haveibeenpwned.com/api/v2/breachedaccount/{0}?truncateResponse=true".format(name)
async with clients.aiohttp_session.get(url) as resp:
status = resp.status
data = await resp.json()
if status in [404, 400]:
breachedaccounts = "None"
else:
breachedaccounts = ""
for breachedaccount in data:
breachedaccounts += breachedaccount["Name"] + ", "
breachedaccounts = breachedaccounts[:-2]
url = "https://haveibeenpwned.com/api/v2/pasteaccount/{0}".format(name)
async with clients.aiohttp_session.get(url) as resp:
status = resp.status
data = await resp.json()
if status in [404, 400]:
pastedaccounts = "None"
else:
pastedaccounts = ""
for pastedaccount in data:
pastedaccounts += pastedaccount["Source"] + " (" + pastedaccount["Id"] + "), "
pastedaccounts = pastedaccounts[:-2]
await self.bot.embed_reply("Breached accounts: {}\nPastes: {}".format(breachedaccounts, pastedaccounts))
python类name()的实例源码
def imagerecognition(self, image_url : str):
'''Image recognition'''
try:
response = clients.clarifai_general_model.predict_by_url(image_url)
except clarifai.rest.ApiError as e:
await self.bot.embed_reply(":no_entry: Error: `{}`".format(e.response.json()["outputs"][0]["status"]["details"]))
return
if response["status"]["description"] != "Ok":
await self.bot.embed_reply(":no_entry: Error")
return
names = {}
for concept in response["outputs"][0]["data"]["concepts"]:
names[concept["name"]] = concept["value"] * 100
output = ""
for name, value in sorted(names.items(), key = lambda i: i[1], reverse = True):
output += "**{}**: {:.2f}%, ".format(name, value)
output = output[:-2]
await self.bot.embed_reply(output)
def lichess_user_all(self, ctx, username : str):
'''WIP'''
data = self.lichess_user_data
embed = discord.Embed(title = data["username"], url = data["url"], color = clients.bot_color)
avatar = ctx.message.author.avatar_url or ctx.message.author.default_avatar_url
embed.set_author(name = ctx.message.author.display_name, icon_url = avatar)
embed.description = "Online: {}\n".format(data["online"])
embed.description += "Member since {}\n".format(datetime.datetime.utcfromtimestamp(data["createdAt"] / 1000.0).strftime("%b %#d, %Y")) #
embed.add_field(name = "Games", value = "Played: {0[all]}\nRated: {0[rated]}\nWins: {0[win]}\nLosses: {0[loss]}\nDraws: {0[draw]}\nBookmarks: {0[bookmark]}\nAI: {0[ai]}".format(data["count"]))
embed.add_field(name = "Follows", value = "Followers: {0[nbFollowers]}\nFollowing: {0[nbFollowing]}".format(data))
embed.add_field(name = "Time", value = "Spent playing: {}\nOn TV: {}".format(utilities.secs_to_letter_format(data["playTime"]["total"]), utilities.secs_to_letter_format(data["playTime"]["tv"])))
for mode, field_name in (("bullet", ":zap: Bullet"), ("blitz", ":fire: Blitz"), ("classical", ":hourglass: Classical"), ("correspondence", ":envelope: Correspondence"), ("crazyhouse", ":pisces: Crazyhouse"), ("chess960", ":game_die: Chess960"), ("kingOfTheHill", ":triangular_flag_on_post: King Of The Hill"), ("threeCheck", ":three: Three-Check"), ("antichess", ":arrows_clockwise: Antichess"), ("atomic", ":atom: Atomic"), ("horde", ":question: Horde"), ("racingKings", ":checkered_flag: Racing Kings"), ("puzzle", ":bow_and_arrow: Training")):
if data["perfs"].get(mode, {}).get("games", 0) == 0: continue
prov = '?' if data["perfs"][mode]["prov"] else ""
chart = ":chart_with_upwards_trend:" if data["perfs"][mode]["prog"] >= 0 else ":chart_with_downwards_trend:"
value = "Games: {0[games]}\nRating: {0[rating]}{1} ± {0[rd]}\n{2} {0[prog]}".format(data["perfs"][mode], prov, chart)
embed.add_field(name = field_name, value = value)
embed.set_footer(text = "Last seen")
embed.timestamp = datetime.datetime.utcfromtimestamp(data["seenAt"] / 1000.0)
await self.bot.say(embed = embed)
def rotmg_characters(self, ctx, player : str):
'''Realm of the Mad God player characters information'''
url = "https://nightfirec.at/realmeye-api/?player={}".format(player)
# http://webhost.ischool.uw.edu/~joatwood/realmeye_api/0.3/
async with clients.aiohttp_session.get(url) as resp:
data = await resp.json()
if "error" in data:
await self.bot.embed_reply("Error: " + data["error"])
return
embed = discord.Embed(title = "{}'s Characters".format(data["player"]), color = clients.bot_color)
avatar = ctx.message.author.avatar_url or ctx.message.author.default_avatar_url
embed.set_author(name = ctx.message.author.display_name, icon_url = avatar)
for character in data["characters"]:
value = "Fame: {0[fame]:,}, Exp: {0[exp]:,}, Rank: {0[place]:,}, Class Quests Completed: {0[cqc]}, Stats Maxed: {0[stats_maxed]}".format(character)
value += "\nHP: {0[hp]}, MP: {0[mp]}, Attack: {0[attack]}, Defense: {0[defense]}, Speed: {0[speed]}, Vitality: {0[vitality]}, Wisdom: {0[wisdom]}, Dexterity: {0[dexterity]}".format(character["stats"])
equips = []
for type, equip in character["equips"].items():
equips.append("{}: {}".format(type.capitalize(), equip))
value += '\n' + ", ".join(equips)
value += "\nPet: {0[pet]}, Clothing Dye: {0[character_dyes][clothing_dye]}, Accessory Dye: {0[character_dyes][accessory_dye]}, Backpack: {0[backpack]}".format(character)
value += "\nLast Seen: {0[last_seen]}, Last Server: {0[last_server]}".format(character)
embed.add_field(name = "Level {0[level]} {0[class]}".format(character), value = value, inline = False)
await self.bot.say(embed = embed)
def spotifyinfo(self, ctx, url : str):
'''Information about a Spotify track'''
path = urllib.parse.urlparse(url).path
if path[:7] != "/track/":
await self.bot.embed_reply(":no_entry: Syntax error")
return
trackid = path[7:]
api_url = "https://api.spotify.com/v1/tracks/" + trackid
async with clients.aiohttp_session.get(api_url) as resp:
data = await resp.json()
# tracknumber = str(data["track_number"])
description = "Artist: [{}]({})\n".format(data["artists"][0]["name"], data["artists"][0]["external_urls"]["spotify"])
description += "Album: [{}]({})\n".format(data["album"]["name"], data["album"]["external_urls"]["spotify"])
description += "Duration: {}\n".format(utilities.secs_to_colon_format(data["duration_ms"] / 1000))
description += "[Preview]({})".format(data["preview_url"])
await self.bot.embed_reply(description, title = data["name"], title_url = url, thumbnail_url = data["album"]["images"][0]["url"])
def XetexBody(self):
data = ''
prevcode = 0
for code in sorted(self.font.chars):
try:
uniname = unicodedata.name(unichr(code))
except ValueError:
uniname = ''
if code - prevcode > 1:
gaps = len([x for x in range(prevcode + 1, code)
if unicodedata.category(unichr(x))[0] != 'C'])
if gaps:
data += ('\\rowcolor{missing}\\multicolumn{3}{|c|}'
'{\\small %d visible characters not mapped to glyphs} \\\\\n') % (gaps)
prevcode = code
data += ('\\texttt{%04X} & {\\customfont\\symbol{%d}} &'
'{\\small %s}\\\\\n') % (code, code, uniname)
return data
def XetexBody(self):
data = ''
uni = {}
for code, name in self.font.chars.items():
if name not in uni:
uni[name] = []
uni[name].append(code)
for glyph in self.font.glyphs:
if glyph.class_name:
data += '\\rowcolor{%s}\n' % glyph.class_name
if glyph.name in uni:
chars = ', '.join('u%04X' % x for x in glyph.chars)
else:
chars = ''
data += '%d & %s & %s & %d & %d & %d & %s\\\\\n' % (
glyph.index, TexGlyph(glyph), TexEscape(glyph.name),
glyph.advance_width, glyph.lsb, glyph.class_def, chars)
return data
def charinfo(self, ctx, *, chars):
"""Get unicode character info."""
if not chars:
return
chars = unicodedata.normalize('NFC', chars)
if len(chars) > 25:
await ctx.send('Too many emoji.')
return
embed = discord.Embed()
for char in chars:
uc = hex(ord(char))[2:]
name = unicodedata.name(char, 'unknown')
if name in {'SPACE', 'EM QUAD', 'EN QUAD'} or ' SPACE' in name:
char = '" "'
short = len(uc) <= 4
code = f'`\\{"u" if short else "U"}{uc.lower().zfill(4 if short else 8)}`'
embed.add_field(name=name,
value=f'{char} [{code}](http://www.fileformat.info/info/unicode/char/{uc}/index.htm)')
await ctx.send(embed=embed)
def __init__(self, emoji):
self.raw = emoji
if isinstance(emoji, str):
self.id = 0
self.unicode = emoji
self.custom = False
self.managed = False
self.name = [unicodedata.name(ch) for ch in emoji]
self.category = [unicodedata.category(ch) for ch in emoji]
self.roles = []
self.guild = None
else:
self.id = emoji.id
self.unicode = ''
self.custom = True
self.managed = getattr(emoji, 'managed', None)
self.name = [emoji.name]
self.category = ['custom']
self.roles = getattr(emoji, 'roles', None)
self.guild = getattr(emoji, 'guild', None)
def charinfo(self, *, characters: str):
"""Shows you information about a number of characters in emojis.
Only up to 15 characters at a time.
"""
if len(characters) > 15:
await self.bot.say('Too many characters ({}/15)'.format(len(characters)))
return
fmt = '`\\U{0:>08}`: {1} - {2} \N{EM DASH} <http://www.fileformat.info/info/unicode/char/{0}>'
def to_string(c):
digit = format(ord(c), 'x')
name = unicodedata.name(c, 'Name not found.')
return fmt.format(digit, name, c)
await self.bot.say('\n'.join(map(to_string, characters)))
def build_index(self, chars=None):
if chars is None:
chars = (chr(i) for i in range(32, sys.maxunicode))
index = {}
for char in chars:
try:
name = unicodedata.name(char)
except ValueError:
continue
if name.startswith(CJK_UNI_PREFIX):
name = CJK_UNI_PREFIX
elif name.startswith(CJK_CMP_PREFIX):
name = CJK_CMP_PREFIX
for word in tokenize(name):
index.setdefault(word, set()).add(char)
self.index = index
def show_encodings():
print(end='\t\t')
for encoding in encodings:
print(encoding.ljust(widths[encoding] * 2), end='\t')
print()
for lineno, char in enumerate(chars):
codepoint = 'U+{:04X}'.format(ord(char))
print(char, codepoint, sep='\t', end='\t')
for encoding in encodings:
try:
bytes = char.encode(encoding)
dump = ' '.join('%02X' % byte for byte in bytes)
except UnicodeEncodeError:
dump = missing_mark
dump = dump.ljust(widths[encoding] * 2)
print(dump, end='\t')
# print(chr(callout1_code + lineno))
print(unicodedata.name(char))
# print()
#list_chars()
def build_index(self, chars=None):
if chars is None:
chars = (chr(i) for i in range(32, sys.maxunicode))
index = {}
for char in chars:
try:
name = unicodedata.name(char)
except ValueError:
continue
if name.startswith(CJK_UNI_PREFIX):
name = CJK_UNI_PREFIX
elif name.startswith(CJK_CMP_PREFIX):
name = CJK_CMP_PREFIX
for word in tokenize(name):
index.setdefault(word, set()).add(char)
self.index = index
def build_index(self, chars=None):
if chars is None:
chars = (chr(i) for i in range(32, sys.maxunicode))
index = {}
for char in chars:
try:
name = unicodedata.name(char)
except ValueError:
continue
if name.startswith(CJK_UNI_PREFIX):
name = CJK_UNI_PREFIX
elif name.startswith(CJK_CMP_PREFIX):
name = CJK_CMP_PREFIX
for word in tokenize(name):
index.setdefault(word, set()).add(char)
self.index = index
def show_usage(name):
usage = """
Usage:
%s script
where script is a number between 1-9
1 - devnag
2 - bengali / assamese
3 - punjabi
4 - gujarati
5 - oriya
6 - tamil
7 - telugu
8 - kannada
9 - malayalam
the program reads from stdin and writes to stdout
any msgs to the user (error msgs etc) are printed on stderr
""" % (name)
logging.info(sys.stderr, usage)
sys.exit(1)
def __init__(self, unicodeHexValue, block):
""" Set up a unicode character.
Arguments:
unicodeHexValue -- an integer that should correspond to a
Unicode code point.
block -- the CharacterBlock this character belongs to.
Raises:
ValueError -- if unicodeHexValue is not a valid code point.
"""
if unicodeHexValue < 0 or unicodeHexValue > 0x10FFFF:
raise (ValueError, "numeric value outside Unicode range")
self.unicodeHexValue = unicodeHexValue
""" Use name check to filter out unused characters.
unicodedata.name() raises ValueError for these
"""
self.chr = chr(self.unicodeHexValue)
self.name = unicodedata.name(self.chr)
self.equivalents = {}
self._block = block
def _equivalent(self, char, prev, next, implicitA):
""" Transliterate a Devanagari character to Latin.
Add implicit As unless overridden by VIRAMA.
"""
implicitA = False # Force it!
result = []
if char.chr != DevanagariCharacter._VIRAMA:
result.append(char.equivalents[self.name])
""" Append implicit A to consonants if the next character isn't a vowel. """
if implicitA and char.isConsonant \
and ((next is not None \
and next.chr != DevanagariCharacter._VIRAMA \
and not next.isVowel) \
or next is None):
result.append(characterBlocks['DEVANAGARI']\
[DevanagariCharacter._LETTER_A].equivalents[self.name])
return result
transliterator_tam.py 文件源码
项目:indic_transliteration
作者: sanskrit-coders
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def _equivalent(self, char, prev, next, implicitA):
""" Transliterate a Devanagari character to Latin.
Add implicit As unless overridden by VIRAMA.
"""
implicitA = False # Force it!
result = []
if char.chr != DevanagariCharacter._VIRAMA:
result.append(char.equivalents[self.name])
""" Append implicit A to consonants if the next character isn't a vowel. """
if implicitA and char.isConsonant \
and ((next is not None \
and next.chr != DevanagariCharacter._VIRAMA \
and not next.isVowel) \
or next is None):
result.append(characterBlocks['DEVANAGARI']\
[DevanagariCharacter._LETTER_A].equivalents[self.name])
return result
def member(self, ctx, member : discord.Member = None):
"""Retrieves information about a member of the guild."""
async with ctx.typing():
member = member or ctx.author
icon_url = member.avatar_url_as(static_format='png')
e = discord.Embed(type='rich', color=member.color)
e.set_thumbnail(url=icon_url)
e.add_field(name='Name', value=str(member))
e.add_field(name='ID', value=member.id)
e.add_field(name='Nickname', value=member.nick)
e.add_field(name='Bot Created' if member.bot else 'User Joined Discord', value=member.created_at.strftime(datetime_format))
e.add_field(name='Joined Guild', value=member.joined_at.strftime(datetime_format))
e.add_field(name='Color', value=str(member.color).upper())
e.add_field(name='Status and Game', value='%s, playing %s' % (str(member.status).title(), member.game), inline=False)
roles = sorted(member.roles)[1:] # Remove @everyone
roles.reverse()
e.add_field(name='Roles', value=', '.join(role.name for role in roles) or 'None', inline=False)
e.add_field(name='Icon URL', value=icon_url, inline=False)
await ctx.send(embed=e)
def guild(self, ctx):
"""Retrieves information about this guild."""
guild = ctx.guild
e = discord.Embed(type='rich', color=blurple)
e.set_thumbnail(url=guild.icon_url)
e.add_field(name='Name', value=guild.name)
e.add_field(name='ID', value=guild.id)
e.add_field(name='Created at', value=guild.created_at.strftime(datetime_format))
e.add_field(name='Owner', value=guild.owner)
e.add_field(name='Members', value=guild.member_count)
e.add_field(name='Channels', value=len(guild.channels))
e.add_field(name='Roles', value=len(guild.role_hierarchy)-1) # Remove @everyone
e.add_field(name='Emoji', value=len(guild.emojis))
e.add_field(name='Region', value=guild.region.name)
e.add_field(name='Icon URL', value=guild.icon_url or 'This guild has no icon.')
await ctx.send(embed=e)
def unicode_name_matches(self, text):
u"""Match Latex-like syntax for unicode characters base
on the name of the character.
This does ``\\GREEK SMALL LETTER ETA`` -> ``?``
Works only on valid python 3 identifier, or on combining characters that
will combine to form a valid identifier.
Used on Python 3 only.
"""
slashpos = text.rfind('\\')
if slashpos > -1:
s = text[slashpos+1:]
try :
unic = unicodedata.lookup(s)
# allow combining chars
if ('a'+unic).isidentifier():
return '\\'+s,[unic]
except KeyError:
pass
return u'', []
def test_longstrings(self):
# test long strings to check for memory overflow problems
errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
"backslashreplace"]
# register the handlers under different names,
# to prevent the codec from recognizing the name
for err in errors:
codecs.register_error("test." + err, codecs.lookup_error(err))
l = 1000
errors += [ "test." + err for err in errors ]
for uni in [ s*l for s in ("x", "\u3042", "a\xe4") ]:
for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15",
"utf-8", "utf-7", "utf-16", "utf-32"):
for err in errors:
try:
uni.encode(enc, err)
except UnicodeError:
pass
def test_hangul_syllables(self):
self.checkletter("HANGUL SYLLABLE GA", "\uac00")
self.checkletter("HANGUL SYLLABLE GGWEOSS", "\uafe8")
self.checkletter("HANGUL SYLLABLE DOLS", "\ub3d0")
self.checkletter("HANGUL SYLLABLE RYAN", "\ub7b8")
self.checkletter("HANGUL SYLLABLE MWIK", "\ubba0")
self.checkletter("HANGUL SYLLABLE BBWAEM", "\ubf88")
self.checkletter("HANGUL SYLLABLE SSEOL", "\uc370")
self.checkletter("HANGUL SYLLABLE YI", "\uc758")
self.checkletter("HANGUL SYLLABLE JJYOSS", "\ucb40")
self.checkletter("HANGUL SYLLABLE KYEOLS", "\ucf28")
self.checkletter("HANGUL SYLLABLE PAN", "\ud310")
self.checkletter("HANGUL SYLLABLE HWEOK", "\ud6f8")
self.checkletter("HANGUL SYLLABLE HIH", "\ud7a3")
import unicodedata
self.assertRaises(ValueError, unicodedata.name, "\ud7a4")
def test_strict_error_handling(self):
# bogus character name
self.assertRaises(
UnicodeError,
str, b"\\N{blah}", 'unicode-escape', 'strict'
)
# long bogus character name
self.assertRaises(
UnicodeError,
str, bytes("\\N{%s}" % ("x" * 100000), "ascii"), 'unicode-escape', 'strict'
)
# missing closing brace
self.assertRaises(
UnicodeError,
str, b"\\N{SPACE", 'unicode-escape', 'strict'
)
# missing opening brace
self.assertRaises(
UnicodeError,
str, b"\\NSPACE", 'unicode-escape', 'strict'
)
def test_longstrings(self):
# test long strings to check for memory overflow problems
errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
"backslashreplace"]
# register the handlers under different names,
# to prevent the codec from recognizing the name
for err in errors:
codecs.register_error("test." + err, codecs.lookup_error(err))
l = 1000
errors += [ "test." + err for err in errors ]
for uni in [ s*l for s in (u"x", u"\u3042", u"a\xe4") ]:
for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15",
"utf-8", "utf-7", "utf-16", "utf-32"):
for err in errors:
try:
uni.encode(enc, err)
except UnicodeError:
pass
def test_hangul_syllables(self):
self.checkletter("HANGUL SYLLABLE GA", u"\uac00")
self.checkletter("HANGUL SYLLABLE GGWEOSS", u"\uafe8")
self.checkletter("HANGUL SYLLABLE DOLS", u"\ub3d0")
self.checkletter("HANGUL SYLLABLE RYAN", u"\ub7b8")
self.checkletter("HANGUL SYLLABLE MWIK", u"\ubba0")
self.checkletter("HANGUL SYLLABLE BBWAEM", u"\ubf88")
self.checkletter("HANGUL SYLLABLE SSEOL", u"\uc370")
self.checkletter("HANGUL SYLLABLE YI", u"\uc758")
self.checkletter("HANGUL SYLLABLE JJYOSS", u"\ucb40")
self.checkletter("HANGUL SYLLABLE KYEOLS", u"\ucf28")
self.checkletter("HANGUL SYLLABLE PAN", u"\ud310")
self.checkletter("HANGUL SYLLABLE HWEOK", u"\ud6f8")
self.checkletter("HANGUL SYLLABLE HIH", u"\ud7a3")
import unicodedata
self.assertRaises(ValueError, unicodedata.name, u"\ud7a4")
def test_strict_eror_handling(self):
# bogus character name
self.assertRaises(
UnicodeError,
unicode, "\\N{blah}", 'unicode-escape', 'strict'
)
# long bogus character name
self.assertRaises(
UnicodeError,
unicode, "\\N{%s}" % ("x" * 100000), 'unicode-escape', 'strict'
)
# missing closing brace
self.assertRaises(
UnicodeError,
unicode, "\\N{SPACE", 'unicode-escape', 'strict'
)
# missing opening brace
self.assertRaises(
UnicodeError,
unicode, "\\NSPACE", 'unicode-escape', 'strict'
)
def test_longstrings(self):
# test long strings to check for memory overflow problems
errors = [ "strict", "ignore", "replace", "xmlcharrefreplace",
"backslashreplace"]
# register the handlers under different names,
# to prevent the codec from recognizing the name
for err in errors:
codecs.register_error("test." + err, codecs.lookup_error(err))
l = 1000
errors += [ "test." + err for err in errors ]
for uni in [ s*l for s in (u"x", u"\u3042", u"a\xe4") ]:
for enc in ("ascii", "latin-1", "iso-8859-1", "iso-8859-15",
"utf-8", "utf-7", "utf-16", "utf-32"):
for err in errors:
try:
uni.encode(enc, err)
except UnicodeError:
pass
def test_strict_eror_handling(self):
# bogus character name
self.assertRaises(
UnicodeError,
unicode, "\\N{blah}", 'unicode-escape', 'strict'
)
# long bogus character name
self.assertRaises(
UnicodeError,
unicode, "\\N{%s}" % ("x" * 100000), 'unicode-escape', 'strict'
)
# missing closing brace
self.assertRaises(
UnicodeError,
unicode, "\\N{SPACE", 'unicode-escape', 'strict'
)
# missing opening brace
self.assertRaises(
UnicodeError,
unicode, "\\NSPACE", 'unicode-escape', 'strict'
)
def _change_flowbox_font(self):
'''
Update the font and fontsize used in the current content
of the flowbox.
'''
for flowbox_child in self._flowbox.get_children():
label = flowbox_child.get_child().get_child()
text = label.get_label()
(emoji, name) = self._parse_emoji_and_name_from_text(text)
if emoji:
new_text = (
'<span font="%s %s" fallback="%s">'
%(self._font, self._fontsize, str(self._fallback).lower())
+ html.escape(emoji)
+ '</span>')
if name:
new_text += (
'<span fallback="false" font="%s">'
%(self._fontsize / 2)
+ html.escape(name)
+ '</span>')
label.set_text(new_text)
label.set_use_markup(True)
self.show_all()
self._busy_stop()