Writing on Tablets Tech stuff from @yeltzland

Getting Daily Alerts for MLB Condensed Games

Keep up to date on the travails of the Mets without needing an MLB TV subscription

Citi Field

30 years ago I spent a couple of summers working in New Jersey, and got drawn into the crazy world of New York Mets baseball.

Back then, keeping in touch when back home meant scouring the International Herald Tribune for any baseball news, but for the last decade I’ve been a subscriber to MLB TV which has been fantastic.

When considering renewing this year’s subscription, I realised I hardly watch any live games any more. Most games are played in the evening US Eastern Time or later, so what I generally do is wait until the next morning and try to watch the condensed game - around 5 minutes of “Match Of The Day” style highlights - without knowing the score.

Accessing MLB Condensed Games for free

MLB are a lot less precious with their highlights packages than the Premier League (surprise, surprise!), and make a lot of content available for free on both YouTube and within the MLB TV app for free.

However within the MLB TV app you can’t get notifications for when new condensed games are available without a paid subscription (which is fair enough!), and it’s really hard to check when a the video is ready without finding out the score, or some information about the game via the other videos already published.

However, the excellent Baseball Theater had figured out that the configuration for the mobile apps is held in the open, so that gave me the idea of hacking together a similar solution to send alerts as soon as there is a new condensed game is ready for viewing.

My MLB Condensed Games alert system

It’s all a bit Heath-Robinson, but my system works as below. Note the code is all avaialble on GitHub, and the below is copied from the readme.md of the repo:

There are 2 main entry points - designed to be run from AWS Lambda:

  1. lambdaCondensedGame,js - Checks whether a condensed game stream is available for a given team on a given day
  2. lambdaMonitor.js - Will see if a condensed game has been added, and if so sends a Slack message saying a new game is ready, plus a link to the stream

Condensed Game function

This function reads from the POSTed JSON formatted like:

{
	gameDate: "2019-04-26",
	team: "nym"
}

You must also send a HTTP header of MLBAPIRequest with a value set as an environment variable of the same name.

This will then:

  1. Build URL like http://gd2.mlb.com/components/game/mlb/year_2018/month_06/day_26/master_scoreboard.xml (using the incoming date)
  2. Look for <game> node where home_file_code=”nym” or away_file_code=”nym” (using the incoming team)
  3. Pick up game_pk attribute to get the URL e.g. “https://statsapi.mlb.com/api/v1/game/530594/content?language=en”
  4. Find in the media.epgAlternate nodes the “Extended Highlights” section, and then find the item that is the condensed game video (if it exists)
  5. Pick the correct URL node - the video that has a value that ends in .mp4

Assuming that a condensed game is found, the function then returns JSON like:

{
	"opponent": "pit",
	"date": "2019-04-26",
	"url": "http://mediadownloads.mlb.com/mlbam/mp4/2018/06/27/2202032583/1530076464641/asset_1200K.mp4",
	"mediaType": "Extended Highlights" 
}

Note:

  • The opponent attribute can null if no game has been found
  • The url attribute can null if no condensed game stream has been found for the game
  • The mediaType attribute can null or “Extended Highlights” (I experimented with also getting the “Recap” for a while)

Configuration - Environment variables

  • MLBAPIRequest: The value to be sent in the header of any request

Monitoring function

This function saves the last found game data in an S3 bucket, and then is designed to run on a schedule to do the following:

  • Looks at date in saved game data JSON from the last successful run
  • If date is today, we’re done (we don’t cope with double-headers yet!)
  • Otherwise, call the condensed game function for either yesterday or today (if the last game was yesterday), and if the result has a url attribute
    • Save the latest game data JSON to S3
    • Call Slack sending the url attribute in a mesage
    • Call an IFTTT web hook that sends an iOS notification, clicking on which opens the viedo URL

You can setup a schedule in Cloudwatch to run every N minutes.

Configuration - Environment variables

  • S3ACCESSKEYID: Access Key for the S3 bucket to save game data
  • S3DATABUCKET: Name of the S3 bucket to save game data
  • S3DATAFILE: Name of the file to save game data in
  • S3SECRETACCESSKEY: Access Secret for the S3 bucket to save game data
  • SLACK_WEBHOOK_URL: URL of the webhook to send the Slack message to
  • TEAM: Team abbreviation to monitor e.g. nym
  • IFTTT_EVENT_NAME: Name of the IFTTT event to send the notification call to
  • IFTTT_MAKER_KEY: Name of the IFTTT maker key to enable IFTTT calls

Summary

Everything is working really well so far, and I’m getting exactly what I need. Very happy! I may still subscribe to MLB TV later in the season if I’m missing the weekend live games - or indeed if the Mets get in a playoff race - but for now I can keep wtaching the highlights without it.

The MLB “API” was a bit in flux for the first few weeks of the season. It’s not a public API so that’s to be expected, but hopefully it will be stable for a while now.

I also built an iOS Shortcut to call the API directly, so if for some reason the video URL changed, or I missed/deleted the notification by accident.

If you want to set something similar up for yourself and if the GitHub instructions aren’t clear, let me know and I’ll try to help you out!

MLB AWS Lambda

Using NFC and Launch Center Pro to launch Spotify

Probably saves me a couple of seconds every day :)

I’ve been thinking about playing about with NFC tags for a while, and when Launch Center Pro released a new version that both supported NFC and lets you buy a sticker pack, I thought it was a good time to jump in.

When I’m coding, I’ve got an almost Pavlovian need to have music playing. I think after spending years working in open-plan offices wearing headphones to isolate myself from distractions, I can’t actually work without music on anymore.

Now I spend most of the time working at home, I still have music on - and I’m a long time Spotify subscriber (the quality of their recommendations are unsurpassed!)

Writing a Siri Shortcut to start Spotify playing

Note this section was updated from the original post, as the simple way of just opening the URL spotify-action://press-play stopped working just after I originally wrote this 😠

The shortcut to resume Spotify playing basically uses the Spotify Web API to control playing in the app.

This API is still in Beta, so hopefully won’t change/break in the future, but in essence the shortcut does the following:

  1. Fetches an access token from https://accounts.spotify.com/api/token using a client ID and secret you’ve setup on the Spotify developer dashboard
  2. Open the Spotify app - you need to do this first because of an existing bug in the Spotify API
  3. Call https://api.spotify.com/v1/me/player/play?device_id=[Device ID] to resume playing, using a device ID you’ve found by previously calling the devices API at https://api.spotify.com/v1/me/player/devices

If anyone is interested, let me know and I’ll make the effort to make my script more reusable and shareable, as at present I’ve hard-coded the various IDs into my shortcut 😱

Setting up an NFC Trigger in Launch Center Pro

The LCP NFC integration is really nice. To add a new sticker, you just press “+” in the ‘NFC Triggers” section of “Settings”, and it lets you scan one of your tags and give it a logical name.

Then it’s simple to associate an LCP action with that trigger - obviously here I setup an action to run my Shortcut e.g. shortcuts://run-shortcut?name={{Play Spotify}} where “Play Spotify” is the name of the Shortcut.

Running the Shortcut

To run the Shortcut using the tag, I just move my iPhone XS Max over the NFC tag, which triggers a notification asking if I want to “Open in Launch”.

Tapping the notification then opens the LCP action, and after a couple of seconds Spotify starts playing.

It would be much nicer if I didn’t have to tap the notification to kick things off, and if you’re not shown all scripts running in LCP and Shortcuts.

The notification restriction is down to Apple’s security rules - so not sure we’ll ever get rid of that - but hopefully at some point soon Apple will better integrate Shortcuts so we don’t have to see the app running in the foreground. iOS13?

If none of that made much sense, here’s it all in action …

Other NFC ideas …

I’ve added a tag to the dashboard of my car, and written a Shortcut that can do one of “Play Spotify”, “Play Overcast” or “Open Google Maps with directions home” - the 3 possible things I usually do when I get in the car.

My next idea is to start and stop Toggl timers from another tag on my desk, but I’m waiting for the almost mythical forthcoming Timery app before I do, as I can’t face tackling the Toggl API myself.

NFC Launch Center Pro Spotify

Playing with Siri Intents

Yeltzland makes it to yet another platform

I’ve been enjoying playing with the new Siri Intents in iOS 12, and obviously didn’t need much of an excuse to get my Yeltzland app on yet another platform!

Shortcuts from NSUserActivity

It was pretty easy to get some basic integrations with Siri Shortcuts working. I was already using NSUserActivity on each view on the app to support Handoff between devices, so it was quite simple to extend that code for Siri shortcuts.

For example, on the fixture list page I can add the following:

    // activity is the current NSUserActivity object

    if #available(iOS 12.0, *) {
        activity.isEligibleForPrediction = true            
        activity.suggestedInvocationPhrase = "Fixture List"
        activity.persistentIdentifier = String(format: "%@.com.bravelocation.yeltzland.fixtures", Bundle.main.bundleIdentifier!)
    }

Making the activity eligible for Prediction means it can be used as a Siri Shortcut, and obviously the suggested invocation phrase is a hint for when you open the shortcut in Settings to be able to open the app directly on the Fixture List view from Siri.

Building a full custom Siri Intent

Probably the most useful app feature to expose via a full Siri Intent is the latest Halesowen score. By that I mean an intent that will speak the latest score, as well as showing a custom UI to nicely format the information.

There are plenty of good guides on how to build a custom Siri Intent out there, so I won’t add much detail on how I did this here.

However one strange issue I couldn’t find a work around for was that, when trying to put a number as a custom property in the Siri response, I couldn’t get the response to be spoken.

As you can see from the setup below, I got around this by passing the game score as a string rather than a number, but I wasted a long time trying to debug that issue. Still no idea why it doesn’t work as expected.

Custom response properties

Building a custom UI to show alongside the spoken text was also pretty easy. I’m quite happy with the results - you can see it all working in the video below

To make the shortcut discoverable, I added a “Add to Siri” button on the Latest Score view itself. This is really easy to hookup to the intent by simply passing it in the click handler of the button like this:

    if let shortcut = INShortcut(intent: intent) {
        let viewController = INUIAddVoiceShortcutViewController(shortcut: shortcut)
        viewController.modalPresentationStyle = .formSheet
        viewController.delegate = self
        self.present(viewController, animated: true, completion: nil)
    }

I’m sure you’ll agree the view itself looks pretty classy 🙂

Latest Score view

Summary

It was a lot of fun hooking everything up to Siri, and I’m really pleased with how it all turned out.

Overall I think opening up Siri to 3rd party apps could be game-changing for the platform. Previously Siri was so far behind both Google and Amazon it was almost unusable except for the most basic of tasks. However, now it can start working with those apps you use all the time, I can see it being a truly useful assistant.

Siri is still a way behind of course, but once custom parameterised queries are introduced - presumably in iOS 13 - and if the speech recognition can be improved, it is definitely going to be a contender in the voice assistant market.

I’m also looking forward to Google releasing their similar in-app Actions at some point soon.

Exciting times ahead!

Yeltzland Voice Siri

Xcode Debugging BT Homehub Fix

A settings change fixed this for me - YMMV

This definitely falls into the category of “it fixed it for me” - your mileage may vary.

I’ve been having assorted problems with my BT Infinity broadband service. For a while they insisted it was a router problem (it wasn’t) and sent me a new BT Homehub router to use. The router is actually fine and I’ll probably stick with it, but for some reason Xcode wireless debugging stopped working.

I could install an app from Xcode fine, but the debugger failed to start and attach to the app on my phone. My guess was it was a port issue, but had no real idea what was actually going on.

I didn’t find anything online, but playing about with the Homehub settings, I found I could turn “Enable SIP ALG” on (in Advanced Settings > Firewall > Configuration) which sounded suspicious. I turned it on, and now the wireless debugging works as before. Hurrah!

I’ve no real idea if this was actually the culprit, but thought I’d at least share this in case others hit the same issue, as it’s something to try.

Xcode BT Homehub

WWDC 2018 Wishlist

Things if announced next week would make me very happy

Following on from this week’s Connected, I thought I’d do my own wish list/happy list for WWDC 2018.

Here are my top 5 hopes for next week’s announcements …

1. Siri improvements

Siri is currently so far behind Alexa and Google Assistant it’s a joke - not just on quality of results but because of the very restricted set of domains developers respond to.

I’d love it if a much more flexible way of returning results was introduced. In particular, let developers develop their own intents and grammars to parse Siri queries - just like Alexa and Google do - and not have to wait for Apple to support individual problem domains at their glacial pace.

Bonus happiness if Apple Watch apps could hook into the potentially very useful but currently very limited Siri watch face.

2. Cross platform development

I don’t really believe the rumours of ‘Project Marzipan’, but it would be fantastic if a cross-platform iOS/Mac development platform is announced.

I definitely think some of my iOS apps could potentially work well on the Mac, but I’m not really going to consider porting them unless it was very little work.

Partial happiness if support for pointing devices/mice/trackpads is announced for iOS. Would sure make using my iPad Pro as a work machine a bit easier, and probably a requirement for any cross-platform support going forward.

3. Updated Mac Mini

I used to have a Mac Mini, but it was increasingly underpowered as a development machine so I switched to a 2015 MacBook Pro a couple of years ago.

I can’t really justify splashing out on an iMac right now (or even more an iMac Pro at those prices!), but I’d love a reasonably priced desktop I could use as a development machine - especially one that was always on that I could remote into as necessary.

4. Better WatchKit or full UIKit on Apple Watch

Apple Watch hardware is becoming increasing more powerful, and now mobile connectivity is supported. It has the potential to be really useful for some use cases.

However right now it’s really painful to make any sort of rich interface on Watch with the very limited WatchKit frameworks.

Clearly Apple’s own apps use another more powerful framework - UIKit? - and it would be great if developers are allowed access to this too.

5. Real-time Watch complications

Another missed opportunity on the Watch is being able to update complications on the watch faces in real-time.

The strength of the watch is having time-relevant information presented when you need it. However watch face complications can only be updated on a very restricted schedule, which makes lots of great ideas for real time info right on the Watch almost impossible.

I understand why the underpowered original watches were restricted to occasional complication updates, but we really need move past these restrictions if we want to move the platform forward.

If this means leaving Series 0 Watch owners behind - and I’m one of them at the moment - so be it.

Summary

To be honest I’m not really expecting any of these five things to be announced. I’d be pretty happy if any of them are, very happy if 2 or more are, and if all 5 were it would be a miracle!

iOS WatchOS WWDC