salvoventura / pyunsplash Goto Github PK
View Code? Open in Web Editor NEWAn open source Python wrapper for the unsplash.com REST API
License: MIT License
An open source Python wrapper for the unsplash.com REST API
License: MIT License
import logging
from pyunsplash import PyUnsplash
api_key = 'MY_APP_ACCESS_KEY'
pu = PyUnsplash(api_key=api_key)
logging.getLogger("pyunsplash").setLevel(logging.DEBUG)
search = pu.search(type_='photos', query='red,car')
for photo in search.entries:
print(photo.id, photo.link_download)
Traceback (most recent call last):
File "unsplash_download.py", line 24, in
search = pu.search(type_='photos', query='red,car')
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/pyunsplash.py", line 58, in search
return Search(api_key=self.api_key, where=type, **kwargs)
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/src/search.py", line 29, in init
super(Search, self).init(url=url, api_key=api_key, valid_options=valid_options, **kwargs)
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/src/unpage.py", line 47, in init
r = self._agent.get(self.url, self.query_parameters)
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pyunsplash/src/rest.py", line 84, in get
self._body = _r.json()
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/site-packages/requests/models.py", line 858, in json
self.content.decode(encoding), **kwargs
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/json/init.py", line 348, in loads
return _default_decoder.decode(s)
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Users/appion/.pyenv/versions/3.7.0/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
"Attribution is appreciated and allows contributors to gain exposure"
As detailed in getsentry/responses#487, responses will now raise a RuntimeError when both the Content-Type
header and the content_type
keyword argument are provided.
Using the content_type
keyword argument is recommended in the error message.
Hello !
First of all, nice projet, I use your library for one of my personal projet, easy to setup and use, so thanks a lot for that !
Was just wondering, for User.photos() can you add the "urls" property in return ?
photo.link_download is not really usefull, it's simply https://unsplash.com/photos/{id} + '/download' which can be done quite easy without having to return a property.
However, returning the "urls" will be way more interesting because it will allow us the opportunity to literally download the picture and save them locally if necessary.
Thanks in advance :D
Evaluate whether having photos
as a property of collection
is viable, and makes sense.
This would allow to rewrite the example code loop as:
collections = py_un.collections(per_page=30)
while collections.has_next:
for collection in collections.entries:
for photo in collection.photos.entries:
print collection.title, photo.link_download
# no need to specify per_page: will take from original object
collections = collections.get_next_page()
while collections.has_next:
for collection in collections.entries:
photos = collection.photos
for photo in photos.entries:
print(collection.title, photo.link_download)
# no need to specify per_page: will take from original object
collections = collections.get_next_page()
Traceback (most recent call last):
File "<ipython-input-19-97290a89529d>", line 4, in <module>
for photo in photos.entries:
AttributeError: 'function' object has no attribute 'entries'
Hi,
Thanks a lot for this library. It would be nice to also support the retrieval of photos' raw URLs and author info as properties. (photo.url_raw
and photo.author_info
).
Regards,
Unsplash has made some breaking changes to their API in recent years:
Curated collections have been defunct since September 23, 2019
Curated photos have been defunct since October 22, 2019
Featured collections were deprecated on September 18, 2020, and have been defunct since April 30, 2021
These changes have yet to be reflected in pyunsplash.
In reference to #7 , there is a documentation issue that needs to be addressed regarding the different parameters for /photos/random handling.
This issue tracks that doc update.
Hey there! Nice Wrapper!
Is there a possible issue with the search class.
See following error:
search = py_un.search(type='photos', query='red')
TypeError: search() missing 1 required positional argument: 'type_'
Per your documentation/examples it says to use param "type" however, using "type_" works correctly, a possible typo?
At this time, the PyUnsplash.photo
class directly exposes link_download
which maps to a different property, in particular photo.links.download
. I can add the additional class property.
However, you can still work around this by something like:
import pyunsplash
pu = pyunsplash.PyUnsplash(api_key='<your Unsplash API key>')
photos = pu.photos(type='random', per_page=15)
for photo in photos.entries:
print photo.links.get('download_location', None)
Originally posted by @salvoventura in #3 (comment)
Hey there!
I need some help parsing a random photos object. I am unsure how to parse the object.
The following code I have tried:
py_un = PyUnsplash(api_key="**")
resp = py_un.photos(type_="random", per_page=4, order_by="popular")
for num, item in enumerate(resp.entries):
print(item)
I have tried looking at the object using however, I cannot access the body or entries.
dir(resp)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_agent', '_api_root', '_parse_navigation', '_ret_link', '_sanitized_query_parameters', '_sanitized_url', 'api_key', 'body', 'delete','entries', 'get', 'get_first_page', 'get_last_page', 'get_next_page', 'get_previous_page', 'has_next', 'has_previous', 'header', 'link_first', 'link_last', 'link_next', 'link_previous', 'link_self', 'navigation', 'post', 'put', 'query_parameters', 'requests', 'set_api_key', 'status_code', 'url', 'valid_options']
Could you please tell me what I am doing wrong?
Many Thanks.
This code fails to work, am I doing something wrong? This is the minimum code that triggers the issue. I'm trying to get next page in search results.
from pyunsplash import PyUnsplash
pu = PyUnsplash(api_key="api_key")
search = pu.search(type_="photos", query="bear")
if search.has_next:
search.get_next_page()
TypeError Traceback (most recent call last)
<ipython-input-16-b0e2b3485380> in <module>()
2 search = pu.search(type_="photos", query="bear")
3 if search.has_next:
----> 4 search.get_next_page()
/usr/local/lib/python3.6/dist-packages/pyunsplash/src/unpage.py in get_next_page(self)
118
119 def get_next_page(self):
--> 120 return self.__class__(url=self.link_next, api_key=self.api_key)
121
122 def get_previous_page(self):
TypeError: __init__() missing 1 required positional argument: 'where'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.