server/ammico/utils.py
changeset 109 fda9006b0ec1
parent 15 a3b7c0823378
equal deleted inserted replaced
108:4a152f5f4a09 109:fda9006b0ec1
       
     1 from html.parser import HTMLParser
       
     2 import json
       
     3 
     1 from django.core.cache import cache
     4 from django.core.cache import cache
     2 import requests
     5 import requests
     3 import json
     6 
     4 
     7 
     5 def fetchJson(url):
     8 def fetchJson(url):
     6     cached = cache.get(url)
     9     cached = cache.get(url)
     7     content = ""
    10     content = ""
     8     if not cached:
    11     if not cached:
    16     else:
    19     else:
    17         # Return the cached content
    20         # Return the cached content
    18         content = cached
    21         content = cached
    19 
    22 
    20     return json.loads(content.decode('utf-8'))['VAL']
    23     return json.loads(content.decode('utf-8'))['VAL']
       
    24 
       
    25 #HTML parser to get the info from the Orpheo XML
       
    26 #Hopefully they'll update it because so far it's too slow
       
    27 #to parse the xml and then the html..
       
    28 #Better use extractFromJameSpot for now.
       
    29 class MyHTMLParser(HTMLParser):
       
    30     
       
    31     def __init__(self):
       
    32         self.starttag=''
       
    33         self.endtag=''
       
    34         self.audio=''
       
    35         self.video=''
       
    36         self.images=[]
       
    37         self.captions=[]
       
    38         self.description=''
       
    39         self.captionList=''
       
    40         HTMLParser.__init__(self)
       
    41     
       
    42     def handle_starttag(self, tag, attrs):
       
    43         if (self.starttag == 'audio' and tag == 'source'):
       
    44             for attr in attrs:
       
    45                 if 'src' in attr:
       
    46                     self.audio = attr[1]
       
    47         elif (tag == 'img'):
       
    48             for attr in attrs:
       
    49                 if 'src' in attr:
       
    50                     self.images.append(attr[1])
       
    51         elif (tag == 'video'):
       
    52             for attr in attrs:
       
    53                 if 'poster' in attr:
       
    54                     self.images.append(attr[1])
       
    55         if (self.starttag == 'video' and tag == 'source'):
       
    56             for attr in attrs:
       
    57                 if 'src' in attr:
       
    58                     self.video = attr[1]
       
    59         self.starttag = tag
       
    60     def handle_endtag(self, tag):
       
    61         self.tag = tag
       
    62     def handle_data(self, data):
       
    63         if ('Caption_image' in data):
       
    64             self.captionList = True
       
    65         elif (self.captionList == True):
       
    66             self.captions.append(data)
       
    67         else:
       
    68             self.description = data