[EN] HOW GET URL FOR LATEST PICTURE BONJOURMADAME

bonjourmadame 06/06/2017

I create a Python script and API service for get the latest picture URL for BonjourMadame. It's pretty nice then I share it :)

Working with Python2 or Python3. If use Slack, set the slackuse to True and if use Telegram, set the telegramuse to True.

For get a latest URL picture, I launch a GET request from my personnal API service (infos here) and use a data['url'] value.

Telegram module

Install telegram module from pip:

pip install telegram
Slack module

Install slackclient module from pip:

pip install slackclient
Python script

Now, a Python script. Do not forget to modify the tokens for Telegram/Slack:

#!/usr/bin/env python

import json
import requests
import telegram
from slackclient import SlackClient

# Telegram settings
telegramuse     = False
telegram_chatid = ""
telegram_token  = ""

# Slack settings
slackuse        = False
slack_token     = ""
slack_channel   = ""

# Set your base URL
base            = "latest"
#base           = "random"
#base           = sys.argv[1]

# Function for get a latest url picture
def get_url(base):
  r = requests.get("https://bonjourmadame.xorhak.fr/api/" + base)
  jsondata  = json.loads(r.text)
  latesturl = jsondata['url']
  return latesturl  

# Function for sending message to Slack
def slacksend(message):
  slack_client = SlackClient(slack_token)
  slack_client.api_call(
    "chat.postMessage",
    channel=slack_channel,
    text=message,
    username="BonjourMadame",
  )

# Function for sending message to Telegram
def telegramsend(message):
  bot = telegram.Bot(telegram_token)
  bot.sendPhoto(
    chat_id=telegram_chatid,
    photo=message
  )

# Default main
if __name__ == "__main__":
  if slackuse == True:
    slacksend(get_url(base))
  if telegramuse == True:
    telegramsend(get_url(base))

Enjoy!
XorHak

Tags

🌱 DJΞRFY 🚀

👨🏻‍💻 Tech Lead SRE. Like #Linux, #Apple, #Kubernetes, #Docker, #Unraid, #Traefik, #Hacking, #Chia. Member of @OpenChia Team 🌱 ¯\_(ツ)_/¯