def test_popular(self): index = 'popular' resource_filepath = self.store_mapping[index] stored_response = json.loads(open(resource_filepath).read()) responses.add( responses.GET, '{}/{}{}'.format(API_ROOT, API_VERSION, stored_response.get('_url')), # _url contains only the short path like /popular?page=2&per_page=5 json=stored_response.get('body'), status=stored_response.get('status_code'), content_type='application/json', adding_headers=stored_response.get('headers'), match_querystring=True, ) py_pexels = PyPexels(api_key=api_key) popular_results_page = py_pexels.popular(page=2, per_page=5) # Page properties print(popular_results_page.page) print(popular_results_page.per_page) print(popular_results_page.has_next) print(popular_results_page.has_previous) print(popular_results_page.link_self) print(popular_results_page.link_first) print(popular_results_page.link_last) print(popular_results_page.link_next) print(popular_results_page.link_previous) # Entries for photo in popular_results_page.entries: print(photo.id, photo.photographer, photo.width, photo.height, photo.url) print(photo.src)
def getFromPexels(): api_key = 'useyourownapi' py_pexels = PyPexels(api_key=api_key) listback=[] popular_photos = py_pexels.popular(page=random.randrange(0,202,2)) for photo in popular_photos.entries: landsc=photo.src["landscape"] if landsc is not "": listback.append(photo.src["landscape"]) #print(listback) #else: # popular_photos=popular_photos.get_next_page() for i in range(len(listback)): try: print(listback[i]) opener = urllib.request.Request(listback[i]) opener.add_header('User-Agent','Mozilla/5.0 (X11; Linux x86_64; rv:65.0) Gecko/20100101 Firefox/65.0') fx = open('/home/devnull/Pictures/background/background.jpg','wb') fx.write(urllib.request.urlopen(opener).read()) fx.close() #listback.remove(listback[i]) time.sleep(660) except IndexError: print("List Index Out of Range") break
class PyPexelApi: pypexels = None def __init__(self, pypexels_api_key=None): self.pypexels = PyPexels(api_key=pypexels_api_key) def get_single_random_photo(self): random_photo = self.pypexels.random(per_page=1) for photo in random_photo.entries: photo_url = str(photo.src['large']) photo_photographer_name = photo.photographer.encode('utf-8') photo_photographer_url = str(photo.photographer_url) photo_id = str(photo.id) photo = Photo(photo_url, photo_photographer_name, photo_photographer_url, photo_id) return photo def get_photos_by_search(self, search_term=None, limit=None): list_photos = [] search_results_page = self.pypexels.search(query=search_term, per_page=limit) for pexel_photo in search_results_page.entries: photo_url = str(pexel_photo.src['large']) photo_photographer_name = str(pexel_photo.photographer) photo_photographer_url = str(pexel_photo.photographer_url) photo_id = str(pexel_photo.id) photo = Photo(photo_url, photo_photographer_name, photo_photographer_url, photo_id) list.append(photo) return list_photos def get_popular_photos(self, limit=None): list_photos = [] popular_photos_page = self.pypexels.popular(per_page=limit) for pexel_photo in popular_photos_page.entries: photo_url = str(pexel_photo.src['large']) photo_photographer_name = str(pexel_photo.photographer) photo_photographer_url = str(pexel_photo.photographer_url) photo_id = str(pexel_photo.id) photo = Photo(photo_url, photo_photographer_name, photo_photographer_url, photo_id) list_photos.append(photo) return list_photos
# Initialize app logging logger = logging.getLogger() logging.basicConfig(filename='app_popular.log', level=logging.DEBUG) # pypexels logger defaults to level logging.ERROR # If you need to change that, use getLogger/setLevel # on the module logger, like this: logging.getLogger(PyPexels.logger_name).setLevel(logging.DEBUG) # add a headers to the log logger.debug(80*'=') logging.debug('Testing PyPexels.popular()') logger.debug(80*'=') # instantiate PyPexels object py_pexel = PyPexels(api_key=api_key) # Start with the generic collection, maximize number of items per page # Note: this will run until all popular photos have been visited, # unless a connection error occurs. # Typically the API hourly limit gets hit during this # popular_photos_page = py_pexel.popular(per_page=40) while True: for photo in popular_photos_page.entries: print((photo.id, photo.photographer, photo.url)) if not popular_photos_page.has_next: break popular_photos_page = popular_photos_page.get_next_page()