Switch to unified view

a b/OSSEval/IMDBMovieApp/models.py
1
# This Source Code Form of OSSEval is subject to the terms of the GNU AFFERO
2
# GENERAL PUBLIC LICENSE, v. 3.0. If a copy of the AGPL was not
3
# distributed with this file, You can obtain one at http://www.gnu.org/licenses/agpl.txt
4
#
5
# OSSeval is powered by the SOS Open Source AGPL edition.
6
#  The AGPL requires that you do not remove the SOS Open Source attribution and copyright 
7
#  notices from the user interface (see section 5.d below).
8
9
# OSSEval Copyright 2014 Bitergium SLL
10
# SOS Open Source Copyright 2012 Roberto Galoppini
11
# Author: Davide Galletti 
12
13
14
from django.db import models
15
from OSSEval.utils import xmlMinidom
16
from lxml import html
17
import urllib2
18
19
class IMDBMovie(models.Model):
20
    """
21
    """
22
    imdb_id = models.CharField(max_length=20)
23
    title = models.CharField(max_length=200)
24
    description = models.TextField(blank=True)
25
#     instance = models.OneToOneField('analysis.Instance', related_name='actual_instance', primary_key=True)
26
    instance = models.OneToOneField('analysis.Instance', related_name='imdb_movie', primary_key=True)
27
28
    def __str__(self):
29
        return self.title + " - " + self.instance.name
30
    
31
    def to_xml(self):
32
        str_xml = ""
33
        return '<' + self.__class__.__name__ + ' Title="' + self.title + '">' + str_xml + "</" + self.__class__.__name__ + ">"
34
    
35
    def from_xml(self, xmldoc, instance, insert = True):
36
        self.title = xmlMinidom.getStringAttribute(xmldoc, 'Title')
37
        self.instance = instance
38
        self.save()
39
40
    def getInstanceInfo(self):
41
        '''
42
        Part of the required interface
43
        '''
44
        i = {}
45
        # devo usare l'id tt2357377 di IMDBMovie per accedere alla pagina http://www.imdb.com/title/tt2357377/
46
        url_imdb = "http://www.imdb.com/title/" + self.imdb_id + "/"
47
        response = urllib2.urlopen(url_imdb)
48
        html_page = response.read()
49
        self.tree = html.fromstring(html_page)
50
        self.title = self.tree.xpath('//meta[@property="og:title"]')[0].get('content')
51
        self.description = self.tree.xpath('//meta[@property="og:description"]')[0].get('content')
52
        i['title'] = self.title
53
        i['description'] = self.description
54
        ### etc....
55
        return i
56