Returns a list of results from Flickr for the given query. Retrieving the URL of a result (i.e. image) requires an additional query. - type : SEARCH, IMAGE, - start: maximum undefined, - count: maximum 500, - sort : RELEVANCY, LATEST or IN
(self, query, type=IMAGE, start=1, count=10, sort=RELEVANCY, size=None, cached=True, **kwargs)
| 2160 | SearchEngine.__init__(self, license or FLICKR_LICENSE, throttle, language) |
| 2161 | |
| 2162 | def search(self, query, type=IMAGE, start=1, count=10, sort=RELEVANCY, size=None, cached=True, **kwargs): |
| 2163 | """ Returns a list of results from Flickr for the given query. |
| 2164 | Retrieving the URL of a result (i.e. image) requires an additional query. |
| 2165 | - type : SEARCH, IMAGE, |
| 2166 | - start: maximum undefined, |
| 2167 | - count: maximum 500, |
| 2168 | - sort : RELEVANCY, LATEST or INTERESTING. |
| 2169 | There is no daily limit. |
| 2170 | """ |
| 2171 | if type not in (SEARCH, IMAGE): |
| 2172 | raise SearchEngineTypeError |
| 2173 | if not query or count < 1 or start < 1 or start > 500/count: |
| 2174 | return Results(FLICKR, query, IMAGE) |
| 2175 | # 1) Construct request URL. |
| 2176 | url = FLICKR+"?" |
| 2177 | url = URL(url, method=GET, query={ |
| 2178 | "api_key": self.license or "", |
| 2179 | "method": "flickr.photos.search", |
| 2180 | "text": query.replace(" ", "_"), |
| 2181 | "page": start, |
| 2182 | "per_page": min(count, 500), |
| 2183 | "sort": { RELEVANCY: "relevance", |
| 2184 | LATEST: "date-posted-desc", |
| 2185 | INTERESTING: "interestingness-desc" }.get(sort) |
| 2186 | }) |
| 2187 | if kwargs.get("copyright", True) is False: |
| 2188 | # With copyright=False, only returns Public Domain and Creative Commons images. |
| 2189 | # http://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html |
| 2190 | # 5: "Attribution-ShareAlike License" |
| 2191 | # 7: "No known copyright restriction" |
| 2192 | url.query["license"] = "5,7" |
| 2193 | # 2) Parse XML response. |
| 2194 | kwargs.setdefault("unicode", True) |
| 2195 | kwargs.setdefault("throttle", self.throttle) |
| 2196 | data = url.download(cached=cached, **kwargs) |
| 2197 | data = xml.dom.minidom.parseString(bytestring(data)) |
| 2198 | results = Results(FLICKR, query, IMAGE) |
| 2199 | results.total = int(data.getElementsByTagName("photos")[0].getAttribute("total")) |
| 2200 | for x in data.getElementsByTagName("photo"): |
| 2201 | r = FlickrResult(url=None) |
| 2202 | r.__dict__["_id"] = x.getAttribute("id") |
| 2203 | r.__dict__["_size"] = size |
| 2204 | r.__dict__["_license"] = self.license |
| 2205 | r.__dict__["_throttle"] = self.throttle |
| 2206 | r.text = self.format(x.getAttribute("title")) |
| 2207 | r.author = self.format(x.getAttribute("owner")) |
| 2208 | results.append(r) |
| 2209 | return results |
| 2210 | |
| 2211 | class FlickrResult(Result): |
| 2212 |
nothing calls this directly
no test coverage detected