[488] | 1 | #!/usr/bin/python
|
---|
| 2 | # -*- coding: utf-8 -*-
|
---|
| 3 |
|
---|
| 4 | from appscript import *
|
---|
[489] | 5 | from aem import AEEnum
|
---|
[491] | 6 | import sys
|
---|
[489] | 7 | import time
|
---|
[488] | 8 |
|
---|
[489] | 9 | def iTunes_main_pane():
|
---|
[630] | 10 | return app(u'System Events').application_processes[u'iTunes'].windows[u'iTunes'].scroll_areas[1]
|
---|
[488] | 11 |
|
---|
[489] | 12 | def wait_for_podcast_update(iTunes):
|
---|
| 13 | # show 'Podcasts'
|
---|
| 14 | iTunes.playlists[its.special_kind == AEEnum('kSpP')].reveal()
|
---|
| 15 | podcast_status = iTunes_main_pane().outlines[1].rows.static_texts[1].value
|
---|
[639] | 16 | # XXX this can fail if iTunes is hidden; fix it
|
---|
[491] | 17 | while any(status in (u'downloading', u'queued for download')
|
---|
| 18 | for status in podcast_status.get()):
|
---|
[489] | 19 | time.sleep(0.5)
|
---|
[488] | 20 |
|
---|
[639] | 21 | def sync_devices(iTunes):
|
---|
[628] | 22 | try:
|
---|
| 23 | iTunes.update()
|
---|
| 24 | except CommandError:
|
---|
[639] | 25 | print >> sys.stderr, 'No iOS devices detected. Waiting for an iOS device...'
|
---|
[628] | 26 | while True:
|
---|
| 27 | time.sleep(0.5)
|
---|
| 28 | try:
|
---|
| 29 | iTunes.update()
|
---|
[629] | 30 | return
|
---|
[628] | 31 | except CommandError:
|
---|
| 32 | pass
|
---|
| 33 |
|
---|
[639] | 34 | # show first iOS device
|
---|
[489] | 35 | iTunes.sources[its.kind == AEEnum('kPod')].sources[1].library_playlists[1].reveal()
|
---|
[628] | 36 |
|
---|
| 37 | # wait for update to complete
|
---|
[630] | 38 | def sync_enabled():
|
---|
[546] | 39 | try:
|
---|
[630] | 40 | iTunes_main_pane().buttons[u'Apply'].click()
|
---|
[546] | 41 | except CommandError:
|
---|
| 42 | pass
|
---|
[630] | 43 | try:
|
---|
| 44 | return iTunes_main_pane().buttons[u'Sync'].enabled.get()
|
---|
| 45 | except CommandError:
|
---|
| 46 | pass
|
---|
[489] | 47 |
|
---|
[630] | 48 | while not sync_enabled():
|
---|
| 49 | time.sleep(0.5)
|
---|
| 50 |
|
---|
[488] | 51 | if __name__ == '__main__':
|
---|
[489] | 52 | iTunes = app('iTunes')
|
---|
[609] | 53 | print >> sys.stderr, 'Starting podcast update...'
|
---|
| 54 | iTunes.updateAllPodcasts()
|
---|
[639] | 55 | print >> sys.stderr, 'Synchronizing iOS devices...'
|
---|
| 56 | sync_devices(iTunes)
|
---|
[491] | 57 | print >> sys.stderr, 'Updating podcasts...'
|
---|
[489] | 58 | iTunes.updateAllPodcasts()
|
---|
| 59 | wait_for_podcast_update(iTunes)
|
---|
[639] | 60 | print >> sys.stderr, 'Synchronizing iOS devices...'
|
---|
| 61 | sync_devices(iTunes)
|
---|
[574] | 62 | app(id='net.sabi.UpdatePodcasts').quit()
|
---|