Created
September 21, 2017 19:40
-
-
Save iAbadia/2a00f27db90152a7f137994ef2d517c6 to your computer and use it in GitHub Desktop.
query function for telegram bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def query(self): | |
url = GIPHY_API + GIPHY_RAND_ENDP + '?' | |
# Check for API key | |
if not self.api_key: | |
logger.warn('GiphyRandomRequest<query()>: No API key provided') | |
return | |
# Add API key | |
url += 'api_key=' + self.api_key + '&' | |
# Add tag if provided | |
if self.tag: | |
url += 'tag=' + self.tag + '&' | |
# Add rating if provided | |
if self.rating: | |
url += 'rating=' + self.rating + '&' | |
# Indicate JSON | |
url += 'fmt=json' | |
# Do the query | |
try: | |
# Get GIF url | |
response = urllib.urlopen(url) | |
self.gif_json = json.loads(response.read()) | |
except: | |
# Some error happended, can's send GIF | |
logger.error('GiphyRandomRequest<query>: Error ocurred while requesting GIF.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment