Add new serializer for the data send by Orpheo. We receive XML so we parse it to json. Then we parse some html to get the proper data of each attribute.
Add xmlToJson parser module to requirements.txt
--- a/server/ammico/serializers.py Wed Apr 08 00:31:45 2015 +0200
+++ b/server/ammico/serializers.py Wed Apr 08 00:36:37 2015 +0200
@@ -1,12 +1,13 @@
+from html.parser import HTMLParser
import json
import requests
from rest_framework import serializers
+import xmltodict
-from ammico.models import AmmicoUser, Book, Slide
+from ammico.models import Book, Slide
from ammico.utils import fetchJson
-from config import URL_EXALEAD
-from config import URL_JAMESPOT
+from config import URL_EXALEAD, URL_ORPHEO, URL_JAMESPOT
class BookSerializer(serializers.ModelSerializer):
@@ -23,7 +24,7 @@
if (slide.idInventory != ""):
return extractFromMIMO(slide)
else:
- return extractFromJameSpot(slide)
+ return extractFromOrpheo(slide)
def getOrder(self, slide):
return slide.book.get_slide_order().index(slide.id)
@@ -56,3 +57,71 @@
details['images'] = details.pop('firstImg')
details['description'] = details.pop('captionImg')
return details
+
+def extractFromOrpheo(slide):
+ details = {}
+ params = {'id': slide.idStop.replace('stop-', '')}
+ data = requests.get(URL_ORPHEO, params=params)
+ parsed_data = xmltodict.parse(data.content.decode('utf-8'))
+
+ if ('item' in parsed_data['result']):
+ details = {
+ 'title': parsed_data['result']['item']['title'],
+ 'idInventory': parsed_data['result']['item']['Numero_inventaire'],
+ }
+ parser = MyHTMLParser()
+
+ if (parsed_data['result']['item']['Description']):
+ parser.feed(parsed_data['result']['item']['Description'])
+ details['description']= parser.description
+
+ if (parsed_data['result']['item']['Audio']):
+ parser.feed(parsed_data['result']['item']['Audio'])
+ details['image']= parser.image
+ details['audio']= parser.audio
+ details['caption']= parser.caption
+
+ if (parsed_data['result']['item']['Video']):
+ parser.feed(parsed_data['result']['item']['Video'])
+ details['video']= parser.video
+ details['image']= parser.image
+
+ #if (parsed_data['result']['item']['Image']):
+ # parser.feed(parsed_data['result']['item']['Image'])
+ # details['image']= parser.caption
+
+ return details
+
+class MyHTMLParser(HTMLParser):
+ starttag=''
+ endtag=''
+ audio=''
+ video=''
+ image=''
+ caption=''
+ description=''
+
+ def handle_starttag(self, tag, attrs):
+ if (self.starttag == 'audio' and tag == 'source'):
+ for attr in attrs:
+ if 'src' in attr:
+ print(attr)
+ self.audio = attr[1]
+ elif (tag == 'img'):
+ for attr in attrs:
+ if 'src' in attr:
+ self.image = attr[1]
+ elif (tag == 'video'):
+ for attr in attrs:
+ if 'poster' in attr:
+ self.image = attr[1]
+ if (self.starttag == 'video' and tag == 'source'):
+ for attr in attrs:
+ if 'src' in attr:
+ print(attr)
+ self.video = attr[1]
+ self.starttag = tag
+ def handle_endtag(self, tag):
+ self.tag = tag
+ def handle_data(self, data):
+ self.description = data
--- a/server/requirements.txt Wed Apr 08 00:31:45 2015 +0200
+++ b/server/requirements.txt Wed Apr 08 00:36:37 2015 +0200
@@ -3,4 +3,5 @@
django-taggit==0.12.3
djangorestframework==3.1.0
psycopg2==2.6
-requests==2.5.3
\ No newline at end of file
+requests==2.5.3
+xmltodict==0.9.2
--- a/server/settings.py Wed Apr 08 00:31:45 2015 +0200
+++ b/server/settings.py Wed Apr 08 00:36:37 2015 +0200
@@ -37,7 +37,8 @@
'requests',
'taggit',
'ammico',
- 'authentication'
+ 'authentication',
+ 'xmltodict',
)
MIDDLEWARE_CLASSES = (