def pexels_parse_resp(subject): """ From Pexels API resp, collect the top 4 images from results. :param subject: The subject to be used for the image search or type(None). If None, random photos are fetched. :rtype images: A list containing data on the fetched images. :except AttributeErrror: Occurs when resp fails to fetch images and enumerate cannot parse resp. """ py_pexel = PyPexels(api_key=PEXELS_API_KEY) if subject is not None: resp = py_pexel.search(query=subject, per_page=4) else: resp = py_pexel.random(per_page=4) images = [] try: for num, item in enumerate(resp.entries, 1): image_info = { "author_name": item.photographer, "full_image": item.src["original"], "image_id": item.id, "author_profile": item.photographer_url, } images.append(image_info) return images except AttributeError as err: handle_err( f"Failed to parse pexels resp object: {err}\nCheck that your API_KEYs are setup correctly." )
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
from pypexels import PyPexels from os import chdir import urllib.request as req import requests chdir("imgs") api_key = '563492ad6f9170000100000133e1d38b49754e5ebcf024313a5fdbc2' # instantiate PyPexels object py_pexels = PyPexels(api_key=api_key) for i in range(100): popular_photos = py_pexels.random(per_page=10) for photo in popular_photos.entries: print(photo.id, photo.photographer, photo.url) img_data = requests.get(photo.src['medium']).content with open('{0}.jpeg'.format(photo.id), 'wb') as handler: handler.write(img_data) break break
def do_stuff(infinite=True): db = DB(host=DB_HOST, port=DB_PORT, user=DB_USER, passwd=DB_PASS, name=DB_NAME) rmq = Rmq(host=RMQ_HOST, port=RMQ_PORT, user=RMQ_USER, passw=RMQ_PASS) pexel = PyPexels(api_key=API) log.info('started') banned = False while True: try: log.info('Attempting to get photos!') entries = set(pexel.random(per_page=PER_PAGE).entries) banned = False except PexelsError: word = "Still" if banned else "Got" log.warning(f'{word} banned on pexels, waiting 5 min.') banned = True for _ in range(6): rmq.connection.process_data_events() sleep(50) continue rejected_pics = db.seen_pictures for photo in entries: source = photo.src["original"] if source in rejected_pics: log.info(f'Already seen this({source}) picture!') continue db.add_seen_pic(source) pic = PictureValid(service="Pexels", download_url=photo.src["original"], preview_url=photo.src["large"], source_url=photo.url, height=int(photo.height), width=int(photo.width)) if photo.height > photo.width: log.info( 'not adding this pic because height > weight, for now') log.debug(f' height = {photo.height}\nwidth={photo.width}') continue log.info(f'Adding {pic}!') rmq.channel.basic_publish(exchange='', routing_key='check_out', body=pic.json(), properties=rmq.durable) if not infinite: return int_range = range(0, INTERVAL, 20) for _ in int_range: how_many = len(list(int_range)) rmq.connection.process_data_events() sleep(INTERVAL / how_many)
# Comment: What's new in revision 1 # ############################################################################### import logging import os from pypexels import PyPexels api_key = os.environ.get('API_KEY', None) or 'DUMMY_API_KEY' # Initialize app logging logger = logging.getLogger() logging.basicConfig(filename='app_random.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.random()') logger.debug(80*'=') # instantiate PyPexels object py_pexel = PyPexels(api_key=api_key) random_photos_page = py_pexel.random(per_page=7) for photo in random_photos_page.entries: print(photo.id, photo.photographer, photo.url)