Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopo-j committed May 9, 2019
1 parent 4a692bf commit ffc041f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# TestFlight watcher

This script checks for free TestFlight slots for a given list of apps. It calls a callback function when new slots become available and (optionally) when they run out.

This repo includes, as an example, a Telegram bot which uses *TestFlight watcher* and sends notification messages.

## Example usage

The `telegram_bot.py` script takes a comma-separated list of app IDs as an argument.

`./telegram_bot.py ID1,ID2,ID3`

where `ID1`, `ID2`, `ID3` are the 8-character
app IDs you can find at the end of the TestFlight URL. If your URL is `https://testflight.apple.com/join/ABCDEFGH`, your `ID` is `ABCDEFGH`.
28 changes: 28 additions & 0 deletions telegram_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import testflight_watcher
import requests
import sys


CHAT_ID = ""
BOT_TOKEN = ""
BOT_URL = "https://api.telegram.org/bot{}/sendMessage".format(BOT_TOKEN)
MSG_NO_FULL = "TestFlight slots for <b>{}</b> beta are now available! \
<a href='{}'>Download now</a>"
MSG_FULL = "<b>{}</b> beta program on TestFlight is now full"


def send_notification(tf_id, free_slots, title):
dl_url = testflight_watcher.TESTFLIGHT_URL.format(tf_id)
if free_slots:
message = MSG_NO_FULL.format(title, dl_url)
else:
message = MSG_FULL.format(title)
requests.get(BOT_URL, params={"chat_id": CHAT_ID,
"text": message,
"parse_mode": "html",
"disable_web_page_preview": "true"})

testflight_watcher.watch(sys.argv[-1].split(","), send_notification)
36 changes: 36 additions & 0 deletions testflight_watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
from lxml import html
import re
from time import sleep


XPATH_STATUS = '//*[@class="beta-status"]/span/text()'
XPATH_TITLE = '/html/head/title/text()'
TITLE_REGEX = r'Join the (.+) beta - TestFlight - Apple'
TESTFLIGHT_URL = 'https://testflight.apple.com/join/{}'
FULL_TEXT = 'This beta is full.'


def watch(watch_ids, callback, notify_full=True, sleep_time=900):
data = {}
while True:
for tf_id in watch_ids:
req = requests.get(
TESTFLIGHT_URL.format(tf_id),
headers={"Accept-Language": "en-us"})
page = html.fromstring(req.text)
free_slots = (page.xpath(XPATH_STATUS)[0] != FULL_TEXT)
if tf_id not in data:
data[tf_id] = free_slots
else:
if data[tf_id] != free_slots:
if free_slots or notify_full:
title = re.findall(
TITLE_REGEX,
page.xpath(XPATH_TITLE)[0])[0]
callback(tf_id, free_slots, title)
data[tf_id] = free_slots
sleep(sleep_time)

0 comments on commit ffc041f

Please sign in to comment.