File: morguefile.py

package info (click to toggle)
nodebox-web 1.9.2-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,724 kB
  • ctags: 1,254
  • sloc: python: 6,161; sh: 602; xml: 239; makefile: 33
file content (138 lines) | stat: -rw-r--r-- 4,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from urllib import quote_plus
from url import URLAccumulator
from xml.dom.minidom import parseString
import os
from cache import Cache

def clear_cache():
    Cache("morguefile").clear()

### MORGUEFILE IMAGE #################################################################################

class MorgueFileImage(URLAccumulator):
    
    def __init__(self):
        
        self.id        = 0
        self.category  = ""
        self.author    = ""
        self.name      = ""
        self.url       = ""
        self.date      = ""
        self.views     = 0
        self.downloads = 0
        
        self.path      = ""
        
    def __str__(self):
        
        return self.url
        
    def __cmp__(self, other):
        
        
        """ Images in a MorgueFile list can be sorted according to number of views.
        """
        
        if self.views > other.views: 
            return -1
        else: 
            return 1
        
    def download(self, thumbnail=False, wait=60, asynchronous=False):
        
        """ Downloads this image to cache.
        
        Calling the download() method instantiates an asynchronous URLAccumulator.
        Once it is done downloading, this image will have its path property
        set to an image file in the cache.
        
        """
        
        self._thumbnail = thumbnail
        url = self.url
        if self._thumbnail == False:
            url = self.url.replace("thumbnails", "lowrez")
        
        cache = "morguefile"
        extension = os.path.basename(self.url)[-4:]
        URLAccumulator.__init__(self, url, wait, asynchronous, cache, extension, 2)
        
        if not asynchronous:
            return self.path
        
    def load(self, data):
        
        url = self.url
        if self._thumbnail == False:
            url = self.url.replace("thumbnails", "lowrez")
        
        self.path = self._cache.hash(url)

### MORGUEFILE #######################################################################################

class MorgueFile(list):
    
    def __init__(self, xml):
        
        self._parse(xml)

    def _parse_data(self, e, tag):
        
        return e.getElementsByTagName(tag)[0].childNodes[0].data

    def _parse(self, xml):

        if xml == "": return
        dom = parseString(xml)
        for e in dom.getElementsByTagName("image"):
            img = MorgueFileImage()
            img.id        = self._parse_data(e, "unique_id")
            img.category  = self._parse_data(e, "category")
            img.author    = self._parse_data(e, "author")
            img.name      = self._parse_data(e, "title")
            img.url       = self._parse_data(e, "photo_path")
            img.date      = self._parse_data(e, "date_added")
            img.views     = int(self._parse_data(e, "views"))
            img.downloads = int(self._parse_data(e, "downloads"))
            self.append(img)

### MORGUEFILE SEARCH ################################################################################
            
class MorgueFileSearch(MorgueFile, URLAccumulator):
    
    def __init__(self, q, author=False, max=100, wait=10, asynchronous=False, cached=True):
        
        if cached: 
            cache = "morguefile"
        else:
            cache = None
    
        arg = "terms"
        if author == True: arg = "author"
        url = "http://morguefile.com/archive/archivexml.php"
        url += "?" + arg + "=" + quote_plus(q) + "&archive_max_image=" + str(max)
        URLAccumulator.__init__(self, url, wait, asynchronous, cache, ".xml", 1)

    def load(self, data):
        
        MorgueFile.__init__(self, data)

######################################################################################################
 
def search(q, max=100, wait=10, asynchronous=False, cached=True):
    
    return MorgueFileSearch(q, False, max, wait, asynchronous, cached)
    
def search_by_author(q, max=100, wait=10, asynchronous=False, cached=True):
    
    return MorgueFileSearch(q, True, max, wait, asynchronous, cached)
    
#images = search("apple")
#images.sort()
#for img in images:
#    print img.views, img.name

#img = images[0]
#img.download()
#image(img.path, 0, 0)