Skip to content

Instantly share code, notes, and snippets.

@jarhill0
Created February 1, 2018 07:06
Show Gist options
  • Save jarhill0/fb12dbad986ab77b9f2d0be7d4393e66 to your computer and use it in GitHub Desktop.
Save jarhill0/fb12dbad986ab77b9f2d0be7d4393e66 to your computer and use it in GitHub Desktop.
Generating code (part 2)
import json
import os
with open('inline_query_results.json', 'r') as f:
types = json.load(f)
FORM = """
from .inline_query_result import InlineQueryResult
class {name}(InlineQueryResult):
\"\"\"{description}\"\"\"
def __init__(self, type_, id_, {args_and_kwargs}):
super().__init__(type_, id_, reply_markup)
{init_lines}
"""
INIT_LINE = ' self.{name} = {name}'
def gen(t):
params = t['params']
args = []
kwargs = []
for arg in params:
if arg['optional']:
kwargs.append(arg['name'])
else:
if arg['name'] in ('type', 'id'):
continue
args.append(arg['name'])
formatted_kwargs = [f'{a}=None' for a in kwargs]
formatted_args_and_kwargs = ', '.join(args + formatted_kwargs)
init_lines = []
for a in args + kwargs:
init_lines.append(INIT_LINE.format(name=a))
init_lines_formatted = '\n'.join(init_lines)
name = t['name']
description = t['description']
return FORM.format(name=name, description=description, args_and_kwargs=formatted_args_and_kwargs,
init_lines=init_lines_formatted)
def to_camel_case(name):
out = [name[0].lower()]
for letter in name[1:]:
if letter.isupper():
out.append('_')
out.append(letter.lower())
return ''.join(out)
for type_ in types:
with open(os.path.join('codegen', '{}.py'.format(to_camel_case(type_['name']))), 'w') as f:
f.write(gen(type_))
[
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "input_message_content",
"optional": false
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "url",
"optional": true
},
{
"name": "hide_url",
"optional": true
},
{
"name": "description",
"optional": true
},
{
"name": "thumb_url",
"optional": true
},
{
"name": "thumb_width",
"optional": true
},
{
"name": "thumb_height",
"optional": true
}
],
"name": "InlineQueryResultArticle",
"description": "Represents a link to an article or web page."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "photo_url",
"optional": false
},
{
"name": "thumb_url",
"optional": false
},
{
"name": "photo_width",
"optional": true
},
{
"name": "photo_height",
"optional": true
},
{
"name": "title",
"optional": true
},
{
"name": "description",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultPhoto",
"description": "Represents a link to a photo. By default, this photo will be sent by the user with optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the photo."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "gif_url",
"optional": false
},
{
"name": "gif_width",
"optional": true
},
{
"name": "gif_height",
"optional": true
},
{
"name": "gif_duration",
"optional": true
},
{
"name": "thumb_url",
"optional": false
},
{
"name": "title",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultGif",
"description": "Represents a link to an animated GIF file. By default, this animated GIF file will be sent by the user with optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the animation."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "mpeg4_url",
"optional": false
},
{
"name": "mpeg4_width",
"optional": true
},
{
"name": "mpeg4_height",
"optional": true
},
{
"name": "mpeg4_duration",
"optional": true
},
{
"name": "thumb_url",
"optional": false
},
{
"name": "title",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultMpeg4Gif",
"description": "Represents a link to a video animation (H.264/MPEG-4 AVC video without sound). By default, this animated MPEG-4 file will be sent by the user with optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the animation."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "video_url",
"optional": false
},
{
"name": "mime_type",
"optional": false
},
{
"name": "thumb_url",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "caption",
"optional": true
},
{
"name": "video_width",
"optional": true
},
{
"name": "video_height",
"optional": true
},
{
"name": "video_duration",
"optional": true
},
{
"name": "description",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultVideo",
"description": "Represents a link to a page containing an embedded video player or a video file. By default, this video file will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the video.If an InlineQueryResultVideo message contains an embedded video (e.g., YouTube), you must replace its content using input_message_content."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "audio_url",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "caption",
"optional": true
},
{
"name": "performer",
"optional": true
},
{
"name": "audio_duration",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultAudio",
"description": "Represents a link to an mp3 audio file. By default, this audio file will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the audio."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "voice_url",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "caption",
"optional": true
},
{
"name": "voice_duration",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultVoice",
"description": "Represents a link to a voice recording in an .ogg container encoded with OPUS. By default, this voice recording will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the the voice message."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "caption",
"optional": true
},
{
"name": "document_url",
"optional": false
},
{
"name": "mime_type",
"optional": false
},
{
"name": "description",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
},
{
"name": "thumb_url",
"optional": true
},
{
"name": "thumb_width",
"optional": true
},
{
"name": "thumb_height",
"optional": true
}
],
"name": "InlineQueryResultDocument",
"description": "Represents a link to a file. By default, this file will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the file. Currently, only .PDF and .ZIP files can be sent using this method."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "latitude",
"optional": false
},
{
"name": "longitude",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "live_period",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
},
{
"name": "thumb_url",
"optional": true
},
{
"name": "thumb_width",
"optional": true
},
{
"name": "thumb_height",
"optional": true
}
],
"name": "InlineQueryResultLocation",
"description": "Represents a location on a map. By default, the location will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the location."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "latitude",
"optional": false
},
{
"name": "longitude",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "address",
"optional": false
},
{
"name": "foursquare_id",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
},
{
"name": "thumb_url",
"optional": true
},
{
"name": "thumb_width",
"optional": true
},
{
"name": "thumb_height",
"optional": true
}
],
"name": "InlineQueryResultVenue",
"description": "Represents a venue. By default, the venue will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the venue."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "phone_number",
"optional": false
},
{
"name": "first_name",
"optional": false
},
{
"name": "last_name",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
},
{
"name": "thumb_url",
"optional": true
},
{
"name": "thumb_width",
"optional": true
},
{
"name": "thumb_height",
"optional": true
}
],
"name": "InlineQueryResultContact",
"description": "Represents a contact with a phone number. By default, this contact will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the contact."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "game_short_name",
"optional": false
},
{
"name": "reply_markup",
"optional": true
}
],
"name": "InlineQueryResultGame",
"description": "Represents a Game."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "photo_file_id",
"optional": false
},
{
"name": "title",
"optional": true
},
{
"name": "description",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedPhoto",
"description": "Represents a link to a photo stored on the Telegram servers. By default, this photo will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the photo."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "gif_file_id",
"optional": false
},
{
"name": "title",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedGif",
"description": "Represents a link to an animated GIF file stored on the Telegram servers. By default, this animated GIF file will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with specified content instead of the animation."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "mpeg4_file_id",
"optional": false
},
{
"name": "title",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedMpeg4Gif",
"description": "Represents a link to a video animation (H.264/MPEG-4 AVC video without sound) stored on the Telegram servers. By default, this animated MPEG-4 file will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the animation."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "sticker_file_id",
"optional": false
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedSticker",
"description": "Represents a link to a sticker stored on the Telegram servers. By default, this sticker will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the sticker."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "document_file_id",
"optional": false
},
{
"name": "description",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedDocument",
"description": "Represents a link to a file stored on the Telegram servers. By default, this file will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the file."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "video_file_id",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "description",
"optional": true
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedVideo",
"description": "Represents a link to a video file stored on the Telegram servers. By default, this video file will be sent by the user with an optional caption. Alternatively, you can use input_message_content to send a message with the specified content instead of the video."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "voice_file_id",
"optional": false
},
{
"name": "title",
"optional": false
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedVoice",
"description": "Represents a link to a voice message stored on the Telegram servers. By default, this voice message will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the voice message."
},
{
"params": [
{
"name": "type",
"optional": false
},
{
"name": "id",
"optional": false
},
{
"name": "audio_file_id",
"optional": false
},
{
"name": "caption",
"optional": true
},
{
"name": "reply_markup",
"optional": true
},
{
"name": "input_message_content",
"optional": true
}
],
"name": "InlineQueryResultCachedAudio",
"description": "Represents a link to an mp3 audio file stored on the Telegram servers. By default, this audio file will be sent by the user. Alternatively, you can use input_message_content to send a message with the specified content instead of the audio."
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment