Exemplo n.º 1
0
def Main():
    client_id = os.environ.get('IMGUR_CLIENT_ID')
    client_secret = os.environ.get('IMGUR_CLIENT_SECRET')
    client = ImgurClient(client_id, client_secret)

    imgur_df = pd.DataFrame(
        columns=['title', 'description', 'img_type', 'url', 'datetime'])
    page = 1
    force_stop = False
    error = None

    while imgur_df.shape[0] < COUNT and not force_stop:
        try:
            print 'Current Count', imgur_df.shape[0]
            tags = client.gallery_tag('photoshop_battles',
                                      sort='all',
                                      window='time',
                                      page=page)

            if tags.items is None:
                force_stop = True
                break

            for item in tags.items:
                if hasattr(item, 'type') and item.type == 'image/jpeg':
                    if passes_imgur_filter(item.title, item.description):
                        data = [
                            item.title, item.description, item.type, item.link,
                            item.datetime
                        ]
                        imgur_df.loc[item.id] = data

                if hasattr(item, 'images') and len(item.images) > 0:
                    for images in item.images:
                        if images['type'] == 'image/jpeg':
                            if passes_imgur_filter(images['title'],
                                                   images['description']):
                                data = [
                                    images['title'], images['description'],
                                    images['type'], images['link'],
                                    images['datetime']
                                ]
                                imgur_df.loc[images['id']] = data

            print 'page', page
            page += 1

        except ImgurClientError as e:
            force_stop = True
            error = {'message': e.error_message, 'code': e.status_code}
            print(e.error_message)
            print(e.status_code)

    imgur_df.to_csv(CSV_OUTPUT_PATH, encoding='utf-8', index=True)
    print "Total: ", imgur_df.shape[0]
    print "Output to ", CSV_OUTPUT_PATH
Exemplo n.º 2
0
def get_imgur_image(text):
    client_id = os.environ.get('IMGUR_CLIENT_ID')
    client_secret = os.environ.get('IMGUR_CLIENT_SECRET')

    if client_id and client_secret:

        client = ImgurClient(client_id, client_secret)
        tag = tag_from_text(text)

        if tag:
            try:
                gallery = client.gallery_tag(tag)
                gallery = gallery.items
            except ImgurClientError:
                return 'Tag {} not found'.format(tag)
        else:

            gallery = client.gallery()

        return random_link_from_gallery_list(gallery)
Exemplo n.º 3
0
class imgurAPI:

    def __init__(self):
        self.imgur = ImgurClient(client_id="d77a3439b35d2ea", client_secret="b5ae47b0c54923367978870f8a945f6e09e82dec")

    def getGallery(self, search):

        gallery = []
        pictureGallery = self.imgur.gallery_tag(search)

        for i in range(1, pictureGallery.total_items):
            if(len(gallery)>=3):
                break
            if pictureGallery.items[i].nsfw:
                continue
            l = Image(pictureGallery.items[i].id, pictureGallery.items[i].link)
            if not l.url[-4:] == ".jpg":
                continue
            gallery.append(l)

        return gallery
Exemplo n.º 4
0
def get_meme():
    client_id = 'c1c976ad7ab60e9'
    client_secret = '96fff59e1a078eb1d8080d838dba6f0892b17f3c'
    client = ImgurClient(client_id, client_secret)
    while True:
        page = randint(0, 1000)
        try:
            gallery = client.gallery_tag('memes',
                                         page=page,
                                         sort='time',
                                         window='all')
            break
        except:
            pass
    items = gallery.items
    while True:
        try:
            item_ind = randint(0, len(items))
            item = items[item_ind]
            break
        except IndexError:
            pass
    if hasattr(item, "images"):
        images = item.images
        while True:
            try:
                image_ind = randint(0, len(images))
                image = images[image_ind]
                break
            except IndexError:
                pass
        if not image["animated"]:
            return image["link"]
    else:
        if not item.animated:
            return item.link
Exemplo n.º 5
0
def memeFinder():

    client_id = "96fbe8f3e6c8e9f"
    client_secret = "9610a5de8341e22a6f4f8e3d08fad2ea5571bb30"

    client = ImgurClient(client_id, client_secret)

    lastImage = ""

    imgArr = []
    typeArr = []

    for tagIndex in range(len(tagMap)):
        tagName = tagMap[tagIndex]
        tag = client.gallery_tag(tagName)
        for item in tag.items:
            try:
                for image in item.images:
                    lastImage = image["link"]
            except:
                lastImage = item.link

            if "mp4" in lastImage:
                continue

            print(lastImage)
            response = requests.get(lastImage)
            img = Image.open(BytesIO(response.content))
            img = img.resize((32, 32))
            img = img.getdata()

            if (len(array(img).shape) < 2):
                continue
            if (array(img).shape[1] == 4):
                img = array(img)[:, :3]

            img = np.reshape(img, (32, 32, 3))
            img = np.transpose(img, (2, 0, 1))

            imgArr.append(img)

            typeArr.append(tagIndex)

    imgArr = array(imgArr)
    typeArr = array(typeArr)
    # arr = np.zeros(len(imgArr), len(typeArr), 3)

    newTypeArr = array([np.zeros(len(tagMap)) for i in range(len(typeArr))])

    for i in range(len(newTypeArr)):
        newTypeArr[i][typeArr[i]] = 1
    x = len(imgArr)
    y = len(typeArr)
    print(len(imgArr), len(imgArr[0]))
    # imageBatch = np.zeros((x,y,z))
    # for i in range(x):
    #     for j in range(y):
    #         print(i)
    #         imageBatch[x][y] = imgArr[x][y]

    return (array(imgArr).reshape(-1, 3, 32, 32),
            array(newTypeArr).reshape(-1, len(tagMap)))
Exemplo n.º 6
0
            await client.send_message(message.channel, random.choice(cats).link)
        except ImgurClientError:
            await client.send_message(message.channel, 'Imgur-Client spinnt. :(')
    # !capybara
    # Postet ein süßs Bild von einem Capybara
    elif message.content.startswith('!capybara'):
        try:
            baras = imgurclient.subreddit_gallery('capybara', sort='time', window='week', page=0)
            await client.send_message(message.channel, random.choice(baras).link)
        except ImgurClientError:
            await client.send_message(message.channel, 'Imgur-Client spinnt. :(')
    # !jews
    # Postet ein Bild von einem russischen Juden
    elif message.content.startswith('!jews'):
        try:
            jews = imgurclient.gallery_tag('ducklings', sort='viral', page=0, window='week')
            await client.send_message(message.channel, random.choice(jews).link)
        except ImgurClientError:
            await client.send_message(message.channel, 'Imgur-Client spinnt. :(')

    # !funnyAirplaneCrash
    # Postet ein Bild von einem lustigen Flugzeugabsturz
    elif message.content.startswith('!funnyairplanecrash'):
        try:
            funnyairplanecrash = imgurclient.subreddit_gallery('funnyairplanecrash', sort='time', window='week', page=0)
            await client.send_message(message.channel, random.choice(funnyairplanecrash).link)
        except ImgurClientError:
            await client.send_message(message.channel, 'Imgur-Client spinnt. :(')
    
    # !choose
    # Wählt ein Element aus einer Auswahl von Elementen aus
Exemplo n.º 7
0
class ImPI:
    """
    A class used to access the imgur API and download images based on user inputted tags.
    """

    def __init__(self):
        """
        Initializing the class.

        Parameters
        ----------
        None

        Returns
        ----------
        self.creds : dict
            Credentials used to access the imgur API.

        self.client : ImgurClient
            The Imgur API Client.
        """

        with open(os.environ['CREDS_PATH']) as file:
            self.creds = yaml.full_load(file)
        self.client = ImgurClient(self.creds['im_cid'], self.creds['im_cs'])

    def get_file_name(self,url):
        """
        Takes in a url and returns the last section in the path, assumably the
        source file name.

        Parameters
        ----------
        url : url or string
            The url pointing to the image location on imgur.

        Returns
        ----------
        file name : string
            The file name of the file the url is pointing to.
        """

        return(url.split("/")[-1])

    def download_image(self, link, destination=None):
        """
        Downloads an image from a given url and saves it either
        in the script's directory or in a user given location after
        ensuring the existence of the destination directory.

        Parameters
        ----------
        link : url or string
            The url to the image to be downloaded

        destination : os.path object or string
            The destination path for the file to be downloaded to.
            If not supplied, the file will be downloaded to the
            directory that the script sits in.

        Returns
        ----------
        Downloads the file to the specified location, does not retrun anything.
        """

        dest = ""
        if destination:
            os.makedirs(destination) if not os.path.isdir(destination) else ""
            dest = "-O {}/{} ".format(destination,self.get_file_name(link))

        cmd = "wget {}{}".format(dest,link)
        print(cmd)
        try:
            os.system(cmd)
        except Exception as err:
            print("Could not download file: {}\nERROR:{}\n".format(link, err))

    def get_some_links_by_tag(self, tag):
        """
        Takes in a tag to search imgur and returns a list of the top images with that tag.
        This is sure to sift through returned albums and galleries to get all images within.

        Parameters
        ----------
        tag : string
            A tag to search for.

        Returns
        ----------
        outs : array[string]
            An array of url strings pointing to images that match the tag.
        """

        galls_list = self.client.gallery_tag(tag)
        outs = [x.link for x in galls_list.items if not x.is_album and x.type == 'image/jpeg'] + [z[0]['link'] for z in [y.images for y in [x for x in galls_list.items if x.is_album]] if z[0]['type'] == 'image/jpeg']
        return(outs)

    def fetch_images_by_tag(self, tag, destination_path, threads=4):
        """
        Takes in a tag, a destination directory path, and number of threads.
        Searches imgur via API to get the top images with the specified
        tag, then proceeds to download each image to the specified destination
        using a threadpool of size `threads`.

        Parameters
        ----------
        tag : string
            A tag to search for.

        destination_path : os.path object or string
            The path to the destination directory for the image downloads.

        threads : integer
            The number of threads to spin up in the thread pool
            when downloading. If no value is passed, 4 threads
            will be assigned as default value.

        Returns
        ----------
        out_hash : dict{string:string}
            A dictionary of {image_link: download_full_path}.
        """

        out_hash = {}
        images_to_download = self.get_some_links_by_tag(tag)

        with cf.ThreadPoolExecutor(max_workers=threads) as executor:
            for image_link in images_to_download:
                executor.submit(self.download_image, image_link, destination_path)
                out_hash[image_link] = "{}/{}".format(destination_path,self.get_file_name(image_link))
        print("{} images have been downloaded to {}".format(len(images_to_download), destination_path))
        return(out_hash)