[488] | 1 | #!/usr/bin/python
|
---|
| 2 | # -*- coding: utf-8 -*-
|
---|
| 3 |
|
---|
| 4 | from appscript import *
|
---|
[489] | 5 | from aem import AEEnum
|
---|
[488] | 6 | from itertools import izip
|
---|
| 7 | import htmlentitydefs
|
---|
| 8 | import re
|
---|
[491] | 9 | import sys
|
---|
[489] | 10 | import time
|
---|
[488] | 11 |
|
---|
| 12 | # based on <http://sebsauvage.net/python/snyppets/>
|
---|
| 13 | RE_ENTITY_CHR = re.compile(u'&(%s);' % u'|'.join(htmlentitydefs.name2codepoint))
|
---|
| 14 | RE_ENTITY_DEC = re.compile(u'&#(\d+);')
|
---|
| 15 | RE_ENTITY_HEX = re.compile(u'&#x(\w+);')
|
---|
| 16 | def decode_entities(s):
|
---|
| 17 | def entity2char(m):
|
---|
| 18 | entity = m.group(1)
|
---|
| 19 | if entity in htmlentitydefs.name2codepoint:
|
---|
| 20 | return unichr(htmlentitydefs.name2codepoint[entity])
|
---|
[489] | 21 | return u' ' # Unknown entity: We replace with a space.
|
---|
[488] | 22 | replaced = 0
|
---|
| 23 | s, n = RE_ENTITY_CHR.subn(entity2char, s)
|
---|
| 24 | replaced += n
|
---|
| 25 | s, n = RE_ENTITY_DEC.subn(lambda x: unichr(int(x.group(1))), s)
|
---|
| 26 | replaced += n
|
---|
| 27 | s, n = RE_ENTITY_HEX.subn(lambda x: unichr(int(x.group(1),16)), s)
|
---|
| 28 | replaced += n
|
---|
| 29 | return s, replaced
|
---|
| 30 |
|
---|
| 31 | OPEN_P = r'<[Pp][^>]*>'
|
---|
| 32 | RE_OPEN_P = re.compile(OPEN_P)
|
---|
| 33 | RE_MATCHED_P = re.compile(OPEN_P + r'(?!<[Pp])(.*)</[Pp]>')
|
---|
| 34 | RE_TOO_MANY_CR = re.compile(r'\s*\n\s*\n\s*\n+', re.U)
|
---|
| 35 | RE_TAG = re.compile(r'<[^>]*>')
|
---|
| 36 | RE_WHITESPACE = re.compile(r'\s+', re.U)
|
---|
| 37 | RE_LEADING_WHITESPACE = re.compile(r'^\s+(.*)', re.U)
|
---|
| 38 | RE_TRAILING_WHITESPACE = re.compile(r'(.*)\s+$', re.U)
|
---|
| 39 |
|
---|
| 40 | def html_to_text(s):
|
---|
| 41 | s, replaced = decode_entities(s)
|
---|
| 42 | s = s.replace('\r\n', '\n')
|
---|
| 43 | s = s.replace('\r', '\n')
|
---|
| 44 | if replaced > 0 or RE_TAG.search(s): # HTML
|
---|
| 45 | s = RE_WHITESPACE.sub(' ', s)
|
---|
| 46 | s = RE_MATCHED_P.sub(r'\1\n\n', s)
|
---|
| 47 | s = RE_OPEN_P.sub(r'\n\n', s)
|
---|
| 48 | s = RE_TAG.sub('', s)
|
---|
| 49 | s = RE_TOO_MANY_CR.sub(r'\n\n', s)
|
---|
| 50 | s = RE_LEADING_WHITESPACE.sub(r'\1', s)
|
---|
| 51 | s = RE_TRAILING_WHITESPACE.sub(r'\1', s)
|
---|
| 52 | return s
|
---|
| 53 |
|
---|
[489] | 54 | def podcasts_to_lyrics(iTunes):
|
---|
[488] | 55 | podcasts = iTunes.tracks[its.podcast == True]
|
---|
| 56 |
|
---|
| 57 | ids = podcasts.id()
|
---|
| 58 | descs = podcasts.description()
|
---|
| 59 | longdescs = podcasts.long_description()
|
---|
| 60 | lyricses = podcasts.lyrics()
|
---|
| 61 |
|
---|
| 62 | for id_, desc, longdesc, lyrics in izip(ids, descs, longdescs, lyricses):
|
---|
| 63 | if lyrics == k.missing_value: # video
|
---|
| 64 | continue
|
---|
| 65 |
|
---|
| 66 | if longdesc == k.missing_value:
|
---|
| 67 | if desc == k.missing_value:
|
---|
| 68 | continue
|
---|
| 69 | longdesc = desc
|
---|
| 70 | else:
|
---|
| 71 | longdesc = html_to_text(longdesc)
|
---|
| 72 |
|
---|
| 73 | if lyrics == longdesc:
|
---|
| 74 | continue
|
---|
| 75 |
|
---|
| 76 | iTunes.tracks[its.id == id_].lyrics.set(longdesc)
|
---|
| 77 |
|
---|
[489] | 78 | def iTunes_main_pane():
|
---|
| 79 | return app(u'System Events').application_processes[u'iTunes'].windows[u'iTunes'].splitter_groups[1].scroll_areas[1]
|
---|
[488] | 80 |
|
---|
[489] | 81 | def wait_for_podcast_update(iTunes):
|
---|
| 82 | # show 'Podcasts'
|
---|
| 83 | iTunes.playlists[its.special_kind == AEEnum('kSpP')].reveal()
|
---|
| 84 | podcast_status = iTunes_main_pane().outlines[1].rows.static_texts[1].value
|
---|
[491] | 85 | while any(status in (u'downloading', u'queued for download')
|
---|
| 86 | for status in podcast_status.get()):
|
---|
[489] | 87 | time.sleep(0.5)
|
---|
[488] | 88 |
|
---|
[489] | 89 | def wait_for_iPod_update(iTunes):
|
---|
| 90 | # show iPod
|
---|
| 91 | iTunes.sources[its.kind == AEEnum('kPod')].sources[1].library_playlists[1].reveal()
|
---|
| 92 | sync_enabled = iTunes_main_pane().buttons[u'Sync'].enabled
|
---|
| 93 | while sync_enabled.get() == False:
|
---|
| 94 | time.sleep(0.5)
|
---|
| 95 |
|
---|
[488] | 96 | if __name__ == '__main__':
|
---|
[489] | 97 | iTunes = app('iTunes')
|
---|
[491] | 98 | print >> sys.stderr, 'Synchronizing iPod...'
|
---|
| 99 | iTunes.update()
|
---|
[489] | 100 | wait_for_iPod_update(iTunes)
|
---|
[491] | 101 | print >> sys.stderr, 'Updating podcasts...'
|
---|
[489] | 102 | iTunes.updateAllPodcasts()
|
---|
| 103 | wait_for_podcast_update(iTunes)
|
---|
[491] | 104 | print >> sys.stderr, 'Copying podcast descriptions to lyrics...'
|
---|
[489] | 105 | podcasts_to_lyrics(iTunes)
|
---|
| 106 | iTunes.update()
|
---|