Last active
December 24, 2016 00:06
-
-
Save miticojo/0f760f4b7f1721e4e3e493c55ea29c74 to your computer and use it in GitHub Desktop.
Check Kimsufi availability
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
| #!/usr/bin/env /usr/bin/python3 | |
| import sys | |
| import requests | |
| # doc https://core.telegram.org/bots/api | |
| telegram_api="https://api.telegram.org" | |
| telegram_token="XXXXXXXXX:XXXXXXXXXXXXXXXXXXX" | |
| telegram_chatid="00000000" | |
| def check_avail(results, model_reference): | |
| avail = next(filter(lambda e: e['reference'] == model_reference, | |
| [elem for elem in results])) | |
| if avail == None: | |
| return False | |
| for zone in avail["zones"]: | |
| if zone['availability'] != 'unavailable': | |
| return True, zone['availability'] | |
| return False, None | |
| def telegram_send(api_uri, token, chat_id, message): | |
| r = requests.get("{}/bot{}/sendMessage?chat_id={}&text={}".format(api_uri, token, chat_id, message)) | |
| if r.status_code != 200: | |
| raise Exception("Error sending message through Telegram API") | |
| return True | |
| def main(args): | |
| r = requests.get('https://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2') | |
| if r.status_code != 200: | |
| raise Exception("Error getting response") | |
| results = r.json() | |
| model_ref = args[1] | |
| result, zone = check_avail(results['answer']['availability'], model_ref) | |
| if result: | |
| telegram_send(telegram_api, telegram_token, telegram_chatid, | |
| 'Yeah! Kimsufi server {} is available'.format(model_ref)) | |
| print('Yeah! Kimsufi server {} is available'.format(model_ref)) | |
| sys.exit(0) | |
| print('Sorry, Kimsufi server {} not available'.format(model_ref)) | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print("Run {} <model_reference>".format(sys.argv[0])) | |
| sys.exit(127) | |
| main(sys.argv[0:]) |
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
| #!/bin/bash | |
| # create a bot with botfather https://core.telegram.org/bots#6-botfather | |
| # doc https://core.telegram.org/bots/api | |
| telegram_api="https://api.telegram.org" | |
| telegram_token="XXXXXXXXX:XXXXXXXXXXXXXXXXXXX" | |
| telegram_chatid="00000000" | |
| function telegram_send { | |
| curl -s -X GET "$telegram_api/bot$telegram_token/sendMessage?chat_id=$telegram_chatid&text=$1" | |
| } | |
| code=$1 | |
| prod=$2 | |
| base_url="curl -s https://www.kimsufi.com/en/order/kimsufi.cgi?hard=" | |
| grep_error="<legend class=\"error\">1 Error<br>" | |
| err_found=$(curl -s $base_url$code | grep "$grep_error" | wc -l) | |
| if [ "$err_found" -eq "0" ]; then | |
| telegram_send "$prod available!" | |
| else | |
| echo "$prod not available, retry" | |
| exit 1 | |
| fi | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment