Last active
April 5, 2018 23:56
-
-
Save dmattera/af157409e1e7df772a5ad6c361e7031f to your computer and use it in GitHub Desktop.
lullpy - pythonista script for bedtime
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
import os, sys, time | |
from objc_util import * | |
import webbrowser | |
### Tested with: | |
### Pythonista 3.2 | |
### iOS 11.3 | |
### The script performs various functions in order to prepare the device for bedtime: | |
### - lowers volume | |
### - opens Sleep Cycle app | |
### - waits a few seconds for you to start the alarm | |
### - begins playing a song of choice stored in the Music app (Sleep cycle pauses any music playing upon the alarm being started) | |
### - sets screen brightness all the way down | |
### credit to github.com/lukaskollmer for virtually all of the Objective-C here. The only Pythonista use of Music-related ObjC I managed to find. | |
class Song(object): | |
def __init__(self, title, albumTitle, playCount, persistentID, objcMediaItem): | |
self.title = title | |
self.albumTitle = albumTitle | |
self.playCount = playCount | |
self.persistentID = persistentID | |
self.objcMediaItem = objcMediaItem | |
def __str__(self): | |
return '{0} | {1} ({2})'.format(self.playCount, self.title, self.albumTitle) | |
def main(): | |
NSBundle.bundleWithPath_('/System/Library/Frameworks/MediaPlayer.framework').load() | |
set_volume(.2) # value between 0.0 and 1.0 | |
open_sleep_cycle_app() # change URL scheme to your liking if you have another Sleep app | |
time.sleep(5) # waits for user to start alarm before starting music | |
play_music_track("Weightless") # song title | |
set_bright(0) # value between 0.0 and 1.0 | |
os.abort() | |
sys.exit() | |
def set_bright(value): | |
UIScreen = ObjCClass('UIScreen') | |
screen = UIScreen.mainScreen() | |
screen.setBrightness_(value) | |
def set_volume(value): | |
MPVolumeView = ObjCClass('MPVolumeView') | |
volume_view = MPVolumeView.new().autorelease() | |
for subview in volume_view.subviews(): | |
if subview.isKindOfClass_(ObjCClass('UISlider')): | |
subview.value = value | |
break | |
def play_music_track(track_title): | |
MPMediaQuery = ObjCClass('MPMediaQuery') | |
MPMediaItemCollection = ObjCClass('MPMediaItemCollection') | |
MPMusicPlayerController = ObjCClass('MPMusicPlayerController') | |
songsQuery = MPMediaQuery.songsQuery() | |
player = MPMusicPlayerController.systemMusicPlayer() | |
songs = [] | |
for n, collections in enumerate(songsQuery.valueForKey_('collections')): | |
for i, mediaItem in enumerate(collections.valueForKey_('items')): | |
title = mediaItem.valueForKey_('title') | |
album = mediaItem.valueForKey_('albumTitle') | |
playCount = int(str(mediaItem.valueForKey_('playCount'))) | |
persistentID = mediaItem.valueForKey('persistentID') | |
song = Song(title, album, playCount, persistentID, mediaItem) | |
if track_title in str(title): | |
songs.append(song) | |
song_mediaItem = MPMediaItemCollection.collectionWithItems_(ns([songs[0].objcMediaItem])) | |
player.setQueueWithItemCollection_(song_mediaItem) | |
player.play() | |
def open_sleep_cycle_app(): | |
webbrowser.open('fb162575247235:') | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment