Skip to content

Commit

Permalink
Push changes to add initial language support.
Browse files Browse the repository at this point in the history
  • Loading branch information
dethrophes committed Jun 29, 2017
1 parent e5d36ed commit 5cd7153
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
87 changes: 52 additions & 35 deletions Contents/Code/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@

# URLs
VERSION_NO = '1.2017.03.05.1'
AUD_BASE_URL = 'http://www.audible.com/'
AUD_BOOK_INFO = AUD_BASE_URL + 'pd/%s'
AUD_ARTIST_SEARCH_URL = AUD_BASE_URL + 'search?&searchAuthor=%s'
AUD_ALBUM_SEARCH_URL = AUD_BASE_URL + 'search?&searchTitle=%s&x=41'
AUD_SEARCH_URL = AUD_BASE_URL + 'search?'
AUD_SEARCH_BOOK = '&searchTitle='
AUD_SEARCH_AUTHOR = '&searchAuthor='
AUD_SEARCH_TAIL = '&x=41'
AUD_INTERNATIONAL = 'http://www.audible.com/?ipRedirectOverride=true'

# International URLs
AUD_DE = 'http://www.audible.de/?ipRedirectOverride=true'
AUD_UK = 'http://www.audible.co.uk/?ipRedirectOverride=true'

REQUEST_DELAY = 0 # Delay used when requesting HTML, may be good to have to prevent being banned from the site

Expand All @@ -26,6 +13,43 @@

THREAD_MAX = 20

intl_sites={
'fr' : { 'url': 'www.audible.fr', 'rel_date' : 'Date de publication' , 'nar_by' : 'Narrateur(s)' , 'nar_by2':'Lu par'},
'de' : { 'url': 'www.audible.de', 'rel_date' : 'Erscheinungsdatum' , 'nar_by' : 'Gesprochen von', 'rel_date2':'Veröffentlicht'},
'it' : { 'url': 'www.audible.it', 'rel_date' : 'Data di Pubblicazione', 'nar_by' : 'Narratore' },
}

def SetupUrls(base, lang='en'):
ctx=dict()
if base is None:
base='www.audible.com'
if lang in intl_sites :
base=intl_sites[lang]['url']
ctx['REL_DATE']=intl_sites[lang]['rel_date']
ctx['NAR_BY' ]=intl_sites[lang]['nar_by']
if 'rel_date2' in intl_sites[lang]:
ctx['REL_DATE_INFO']=intl_sites[lang]['rel_date2']
else:
ctx['REL_DATE_INFO']=ctx['REL_DATE']
if 'nar_by2' in intl_sites[lang]:
ctx['NAR_BY_INFO' ]=intl_sites[lang]['nar_by2']
else:
ctx['NAR_BY_INFO' ]=ctx['NAR_BY' ]
else:
ctx['REL_DATE']='Release Date'
ctx['REL_DATE_INFO']=ctx['REL_DATE']
ctx['NAR_BY' ]='Narrated By'
ctx['NAR_BY_INFO' ]='Narrated by'


AUD_BASE_URL='http://' + base + '/'
ctx['AUD_BOOK_INFO' ]=AUD_BASE_URL + 'pd/%s?ipRedirectOverride=true'
ctx['AUD_ARTIST_SEARCH_URL']=AUD_BASE_URL + 'search?searchAuthor=%s&ipRedirectOverride=true'
ctx['AUD_ALBUM_SEARCH_URL' ]=AUD_BASE_URL + 'search?searchTitle=%s&x=41&ipRedirectOverride=true'
ctx['AUD_SEARCH_URL' ]=AUD_BASE_URL + 'search?searchTitle={0}&searchAuthor={1}&x=41&ipRedirectOverride=true'
return ctx


def Start():
#HTTP.ClearCache()
HTTP.CacheTime = CACHE_1WEEK
Expand All @@ -35,7 +59,7 @@ def Start():

class AudiobookArtist(Agent.Artist):
name = 'Audiobooks'
languages = [Locale.Language.NoLanguage]
languages = [Locale.Language.English, 'de', 'fr', 'it']
primary_provider = True
accepts_from = ['com.plexapp.agents.localmedia']

Expand Down Expand Up @@ -77,7 +101,7 @@ def findDateInTitle(self, title):
return Datetime.ParseDate(result.group(0)).date()
return None

def doSearch(self, url):
def doSearch(self, url, ctx):



Expand Down Expand Up @@ -142,7 +166,7 @@ def addTask(self, queue, func, *args, **kargs):

class AudiobookAlbum(Agent.Album):
name = 'Audiobooks'
languages = [Locale.Language.NoLanguage]
languages = [Locale.Language.English, 'de', 'fr', 'it']
primary_provider = True
accepts_from = ['com.plexapp.agents.localmedia']

Expand Down Expand Up @@ -184,31 +208,28 @@ def findDateInTitle(self, title):
return Datetime.ParseDate(result.group(0)).date()
return None

def doSearch(self, url):
if Prefs['international']:
self.Log('International Mode Enabled')
cookies = HTTP.CookiesForURL(AUD_INTERNATIONAL)
httpCookies=cookies

def doSearch(self, url, ctx):
html = HTML.ElementFromURL(url, sleep=REQUEST_DELAY)
found = []

for r in html.xpath('//div[contains (@class, "adbl-search-result")]'):
date = self.getDateFromString(self.getStringContentFromXPath(r, 'div/div/ul/li[contains (., "Release Date")]/span[2]//text()'))
date = self.getDateFromString(self.getStringContentFromXPath(r, 'div/div/ul/li[contains (., "{0}")]/span[2]//text()'.format(ctx['REL_DATE'])))
#title = self.getStringContentFromXPath(r, 'div[contains (@class,"adbl-prod-meta-data-cont")]/div[contains (@class,"adbl-prod-title")]/a[1]')
title = self.getStringContentFromXPath(r, 'div/div/div/div/a[1]')
#murl = self.getAnchorUrlFromXPath(r, 'div[contains (@class,"adbl-prod-meta-data-cont")]/div[contains (@class,"adbl-prod-title")]/a[1]')
murl = self.getAnchorUrlFromXPath(r, 'div/div/div/div/a[1]')
thumb = self.getImageUrlFromXPath(r, 'div[contains (@class,"adbl-prod-image-sample-cont")]/a/img')
author = self.getStringContentFromXPath(r, 'div/div/ul/li//a[contains (@class,"author-profile-link")][1]')
narrator = self.getStringContentFromXPath(r, 'div/div/ul/li[contains (., "Narrated By")]//a[1]')
narrator = self.getStringContentFromXPath(r, 'div/div/ul/li[contains (., "{0}")]//a[1]'.format(ctx['NAR_BY']))
self.Log('---------------------------------------XPATH SEARCH HIT-----------------------------------------------')

found.append({'url': murl, 'title': title, 'date': date, 'thumb': thumb, 'author': author, 'narrator': narrator})

return found

def search(self, results, media, lang, manual):
ctx=SetupUrls(Prefs['site'], lang)

self.Log('---------------------------------------ALBUM SEARCH-----------------------------------------------')
self.Log('* ID: %s', media.parent_metadata.id)
self.Log('* Title: %s', media.title)
Expand Down Expand Up @@ -251,10 +272,10 @@ def search(self, results, media, lang, manual):

# Make the URL
if media.artist is not None:
searchUrl = AUD_SEARCH_URL + AUD_SEARCH_BOOK + (String.Quote((normalizedName).encode('utf-8'), usePlus=True)) + AUD_SEARCH_AUTHOR + (String.Quote((media.artist).encode('utf-8'), usePlus=True)) + AUD_SEARCH_TAIL
searchUrl = ctx['AUD_SEARCH_URL'].format((String.Quote((normalizedName).encode('utf-8'), usePlus=True)), (String.Quote((media.artist).encode('utf-8'), usePlus=True)))
else:
searchUrl = AUD_ALBUM_SEARCH_URL % (String.Quote((normalizedName).encode('utf-8'), usePlus=True))
found = self.doSearch(searchUrl)
searchUrl = ctx['AUD_ALBUM_SEARCH_URL'] % (String.Quote((normalizedName).encode('utf-8'), usePlus=True))
found = self.doSearch(searchUrl, ctx)
#found2 = media.album.lstrip('0123456789')
#if normalizedName != found2:
# searchUrl = D18_SEARCH_URL % (String.Quote((found2).encode('utf-8'), usePlus=True))
Expand Down Expand Up @@ -356,25 +377,21 @@ def search(self, results, media, lang, manual):

def update(self, metadata, media, lang, force=False):
self.Log('***** UPDATING "%s" ID: %s - AUDIBLE v.%s *****', media.title, metadata.id, VERSION_NO)
ctx=SetupUrls(Prefs['site'], lang)

if Prefs['international']:
self.Log('International Mode Enabled')
cookies = HTTP.CookiesForURL(AUD_INTERNATIONAL)
httpCookies=cookies

# Make url
url = AUD_BOOK_INFO % metadata.id
url = ctx['AUD_BOOK_INFO'] % metadata.id

html = HTML.ElementFromURL(url, sleep=REQUEST_DELAY)

for r in html.xpath('//div[contains (@id, "adbl_page_content")]'):
date = self.getDateFromString(self.getStringContentFromXPath(r, '//li[contains (., "Release Date")]/span[2]//text()'))
date = self.getDateFromString(self.getStringContentFromXPath(r, '//li[contains (., "{0}")]/span[2]//text()'.format(url['REL_DATE_INFO'])))
#title = self.getStringContentFromXPath(r, 'div[contains (@class,"adbl-prod-meta-data-cont")]/div[contains (@class,"adbl-prod-title")]/a[1]')
title = self.getStringContentFromXPath(r, '//h1[contains (@class, "adbl-prod-h1-title")]/text()')
murl = self.getAnchorUrlFromXPath(r, 'div/div/div/div/a[1]')
thumb = self.getImageUrlFromXPath(r, 'div/div/div/div/div/img')
author = self.getStringContentFromXPath(r, '//li//a[contains (@class,"author-profile-link")][1]')
narrator = self.getStringContentFromXPath(r, '//li[contains (., "Narrated by")]//span[2]').strip()
narrator = self.getStringContentFromXPath(r, '//li[contains (., "{0}")]//span[2]'.format(url['NAR_BY_INFO'])).strip()
studio = self.getStringContentFromXPath(r, '//li//a[contains (@id,"PublisherSearchLink")][1]')
synopsis = self.getStringContentFromXPath(r, '//div[contains (@class, "disc-summary")]/div[*]').strip()
series = self.getStringContentFromXPath(r, '//div[contains (@class, "adbl-series-link")]//a[1]')
Expand Down
6 changes: 6 additions & 0 deletions Contents/DefaultPrefs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@



},{
"id" : "site",
"label" : "Select amazonsite to use",
"type" : "enum",
"values" : ["www.audible.com","www.audible.co.uk","www.audible.com.au","www.audible.de","www.audible.fr","www.audible.it","www.audible.co.jp"],
"default" : "www.audible.com"
},{ "id": "debug",
"label": "Ouput debugging info in logs",
"type": "bool",
Expand Down

0 comments on commit 5cd7153

Please sign in to comment.