def aime(key): client_id = 'c3e767d450a401e' client_secret = 'cdf5fb70e82bc00e65c0d1d1a4eed318ae82024c' client = ImgurClient(client_id, client_secret) if key == 'Aime' or key == 'aime': album = ['hLZwL', 'Qt8En'] i = random.randint(0, len(album) - 1) images = client.get_album_images(album[i]) index = random.sample(range(0, len(images)), 5) else: album = 'hoBxs' #i = random.randint(0, len(album) - 1) images = client.get_album_images(album) index = random.sample(range(0, len(images)), 5) imgurResult = [] for i in index: imageDict = dict() #imageDict['imageLink'] = images[i].link.replace('http', 'https') imageDict['imageLink'] = images[i].link description = images[i].description.split('http')[0].strip('\n') imageDict['title&price'] = description #imageDict['title'] = description.split('$')[0].strip() #imageDict['price'] = '$'+ description.split('$')[1].strip() imageDict['shopeeLink'] = images[i].description.split( '$')[1][3:].strip() imgurResult.append(imageDict) return imgurResult
def download_albums(album_list, output_directory, num_threads, config_path): """ Download albums from album_list to output_directory. album_list: List containing album urls to download. output_directory: Directory in which to save the downloaded albums. num_threads: Number of concurrent downloads to perform. config_path: Path to an alternate config file. """ # Load the configuration from specified path if set. if config_path: config = load_config(config_path) else: config = load_config() logger.debug("Connecting to Imgur") imgur_client = ImgurClient(config['client_id'], config['client_secret']) pool = ThreadPool(num_threads) for album_url in album_list: logger.debug('Downloading images from %s' % (album_url)) album_id = re.match(r'.*\/a\/(?P<id>.*)', album_url).group('id') out_dir = os.path.join(output_directory, album_id) if not os.path.isdir(out_dir): os.makedirs(out_dir) logger.info('Downloading album %s' % (album_id)) images = imgur_client.get_album_images(album_id) for index, image in enumerate(images): pool.add_work({'index':index, 'url':image.link, 'directory':out_dir}) # Start the thread pool. Will block until all jobs are complete. pool.start()
def get_img(path): global pin client = ImgurClient(client_id, client_secret,'61c2a536adcd40eeedbd8e6fba18999f413bf5dc', 'aee3d76d7956080282f5d272f76ef12f19509a47') ## if pin == None: ## authorization_url = client.get_auth_url('pin') ## print("Go to the following URL: {0}".format(authorization_url)) ## pin = input("Enter pin code: ") ## else: ## pass ## credentials = client.authorize(pin, 'pin') ## client.set_user_auth(credentials['access_token'], credentials['refresh_token']) # Example request album_id = '2hzdSFn' config = { 'album': album_id, 'name': 'test-name!', 'title': 'test-title', 'description': 'test-description' } print(client.get_album(album_id)) client.upload_from_path(path,config=config,anon=False) images = client.get_album_images(album_id) url = images[len(images)-1].link return url
def main(): global config global args args = parse_args() config = load_config(args.config) # Initialise Imgur client client = ImgurClient(config['imgur_client_id'], config['imgur_client_secret']) for album in args.album: # Get our album images try: images = client.get_album_images(album) except ImgurClientError as e: print('ERROR: {}'.format(e.error_message)) print('Status code {}'.format(e.status_code)) print("Downloading album {} ({!s} images)".format(album, len(images))) # Download each image for image in images: # Turn our link HTTPs link = image.link.replace("http://", "https://") # Get our file name that we'll save to disk file_name = link.replace("https://i.imgur.com/", "") download_path = os.path.join(args.directory, file_name) if os.path.isfile(download_path): print( "File exists for image {}, skipping...".format(file_name)) else: download_image(link, download_path)
async def meme(self, ctx): image_link = [] config = configparser.ConfigParser() config.read('../auth.ini') client_id = config.get('credentials', 'client_id') client_secret = config.get('credentials', 'client_secret') clienta = ImgurClient(client_id, client_secret) albums = configparser.ConfigParser() albums.read('./Imgur/Memes/meme.ini') albums_dark = albums.get('meme', 'albums').split("\n") album_len = len(albums_dark) - 1 num = randint(0, 5) num = randint(0, album_len) images = clienta.get_album_images(str(albums_dark[num])) for image in images: image_link.append(image.link) i = 0 link_len = len(image_link) - 1 num = randint(0, link_len) embed = discord.Embed() embed.set_author(name="Feed_Ekko - Meme", icon_url=self.client.user.avatar_url) embed.set_image(url=image_link[num]) await ctx.send(embed=embed)
def get_img(): client_id = 'b9ae77105014a61' client_secret = '720fc83b0d748eb6131f987949280e15bf3a6e66' client = ImgurClient(client_id, client_secret) authorization_url = client.get_auth_url('pin') print("Go to the following URL: {0}".format(authorization_url)) if os.path.isfile('pin.txt'): with open('pin.txt', 'r') as f: pin = f.read() else: pin = input("Enter pin code: ") with open('pin.txt', 'w') as f: f.write(pin) credentials = client.authorize(pin, 'pin') client.set_user_auth(credentials['access_token'], credentials['refresh_token']) # Example request album_id = 'ARm1Dtq' config = { 'album': album_id, 'name': 'test-name!', 'title': 'test-title', 'description': 'test-description' } print(client.get_album('ARm1Dtq')) client.upload_from_path('D:/python/linebot/linebot/in.jpg', config=config, anon=False) images = client.get_album_images(album_id) url = images[len(images) - 1].link return url
def imgur_search(search=""): try: client = ImgurClient(imgur_id, imgur_secret) except ImgurClientError as e: if e.status_code == 403: return u'can i haz valid api keys?' else: return u'sorry i could not reach imgur :/ E_MSG: {0} E_CODE: {1}'.format(e.error_message, e.status_code) try: search_results = client.gallery_search(search, advanced=None, sort='time', window='all', page=0) except ImgurClientError as e: return u'derp, something bad happened: {0}'.format(e.error_message) if len(search_results) > 0: item = random.choice(search_results) if item.is_album: try: search_results = client.get_album_images(item.id) item = search_results[0] except ImgurClientError as e: return u'derp, something bad happened: {0}'.format(e.error_message) # gifs over 10mb get returned with an h appended to their id # shave it off to get the full animated gif if len(item.link) > 7 and item.link[-5] == 'h': gif_link = item.link[0:-5]+item.link[-4:] if DEBUG: print ("""[dankBot] [DEBUG] search="{0}" link="{1}" Large gif link found, modifying link.""").format(search, item.link) else: gif_link = item.link else: gif_link = None if DEBUG: print ("""[dankBot] [DEBUG] search="{0}" resource="{1}" No results found.""").format(search, "imgur") return gif_link
def imgur_search(search=""): try: client2 = ImgurClient(imgur_id, imgur_secret) except ImgurClientError as e: if e.status_code == 403: return u'can i haz valid api keys?' else: return u'sorry i could not reach imgur :/ E_MSG: {0} E_CODE: {1}'.format(e.error_message, e.status_code) try: search_results = client2.gallery_search(search, advanced=None, sort='time', window='all', page=0) except ImgurClientError as e: return u'derp, something bad happened: {0}'.format(e.error_message) if len(search_results) > 0: item = random.choice(search_results) if item.is_album: try: search_results = client2.get_album_images(item.id) item = search_results[0] except ImgurClientError as e: return u'derp, something bad happened: {0}'.format(e.error_message) # gifs over 10mb get returned with an h appended to their id # shave it off to get the full animated gif if len(item.link) > 7 and item.link[-5] == 'h': gif_link = item.link[0:-5]+item.link[-4:] if DEBUG: print ("""[dankBot] [DEBUG] search="{0}" link="{1}" Large gif link found, modifying link.""").format(search, item.link) else: gif_link = item.link else: gif_link = None if DEBUG: print ("""[dankBot] [DEBUG] search="{0}" resource="{1}" No results found.""").format(search, "imgur") return gif_link
def corgi(): client = ImgurClient(client_id, client_secret) album = client.get_account_albums(album_id) images = client.get_album_images(album[0].id) index = random.randint(0, len(images) - 1) url = images[index].link return url
def download_album_images(aid, filename, postTitle): client = ImgurClient(im_client, im_secret) imgs = None try: imgs = client.get_album_images(aid) except Exception, e: return
def download_album_images(aid, filename, postTitle): client = ImgurClient(im_client, im_secret) imgs = None try: imgs = client.get_album_images(aid) except Exception, e: return
def parse(self, response): filas = response.css('tr') for fila in filas: campos = fila.css('td') enlaces = campos.css('a::attr(href)').extract() url_imagen = enlaces[0] if enlaces else None url_video = enlaces[1] if len(enlaces) > 1 else None textos = campos.css('::text').extract() nombre = textos[0] if textos else None if textos: while textos[-1] == 'Eng' or textos[-1] == ', ' or textos[ -1] == 'Raw': textos.pop() print nombre, url_imagen, textos if 'imgur.com/a/' in response.url: nombre = response.css('div.post-header').css( 'h1::text').extract()[0] nombre_rom = nombre.split('(')[0] if nombre_rom[-1] == ' ': nombre_rom = nombre_rom[:-1] album_id = response.url.split('/')[-1] client = ImgurClient(API_CLIENT, API_SECRET) imagenes = [img.link for img in client.get_album_images(album_id)] yield ImgData(image_urls=imagenes, idol_name=nombre_rom)
def handsome(): client_id = 'c3e767d450a401e' client_secret = 'cdf5fb70e82bc00e65c0d1d1a4eed318ae82024c' client = ImgurClient(client_id, client_secret) images = client.get_album_images('hjCtM') index = random.randint(0, len(images) - 1) return images[index].link
def getLink(data): image_link = [] config = configparser.ConfigParser() config.read('../auth.ini') client_id = config.get('credentials', 'client_id') client_secret = config.get('credentials', 'client_secret') clienta = ImgurClient(client_id, client_secret) albums = configparser.ConfigParser() albums.read('./Imgur/Anime/' + data + '.ini') albums_dark = albums.get('anime', 'albums').split("\n") album_len = len(albums_dark) - 1 num = randint(0, 5) num = randint(0, album_len) images = clienta.get_album_images(str(albums_dark[num])) for image in images: image_link.append(image.link) i = 0 link_len = len(image_link) - 1 num = randint(0, link_len) return (image_link[num])
def main(): # Check if authentication information is correct. try: print("Successfully logged into Reddit with username ", reddit.user.me(), ".", sep = '') except Exception as e: print(e) return # Load JSON file for album id, frequency of posts, subreddit, and # frequency to repeat image posts. with open('input.json') as input_file: input = json.load(input_file) # Connect to imgur. try: client = ImgurClient(input['imgur_client_id'], input['imgur_client_secret']) except ImgurClientError as e: print(e.error_message) print(e.status_code) # Execute thread to clear cache. thread = threading.Thread(target = clear_cache, args = (input['repeat_frequency'],)) thread.start() while(True): # Get all images in the imgur album. try: images = client.get_album_images(input['album_id']) for image in images: print(image.link) except Exception as e: print(e) break # Pick a random image until we get one that isn't cached. while(True): index = random.randrange(0, len(images)) # Cache miss. Post this image. link = images[index].link if link not in cache: print("Caching image ", link, ".", sep = '') cache.append(link) post(link, input['subreddit']) break # Wait for a specific number of hours. This is provided in the # input.json file. hours = input['frequency'] print("sleeping for", hours, "hours...") # Convert hours to seconds. time.sleep(hours * 60 * 60)
def handle_image(event): print("hello") if isinstance(event.message, ImageMessage): ext = 'jpg' message_content = line_bot_api.get_message_content(event.message.id) with tempfile.NamedTemporaryFile(dir=static_tmp_path, prefix=ext + '-', delete=False) as tf: for chunk in message_content.iter_content(): tf.write(chunk) tempfile_path = tf.name dist_path = tempfile_path + '.' + ext dist_name = os.path.basename(dist_path) os.rename(tempfile_path, dist_path) try: client = ImgurClient(client_id, client_secret, access_token, refresh_token) config = { 'album': album_id, 'name': 'Catastrophe!', 'title': 'Catastrophe!', 'description': 'Cute kitten being cute on ' } path = os.path.join('static', 'tmp', dist_name) client.upload_from_path(path, config=config, anon=False) os.remove(path) print(path) line_bot_api.reply_message(event.reply_token, TextSendMessage(text='上傳成功')) except: line_bot_api.reply_message(event.reply_token, TextSendMessage(text='上傳失敗')) return 0 elif isinstance(event.message, VideoMessage): ext = 'mp4' elif isinstance(event.message, AudioMessage): ext = 'm4a' elif isinstance(event.message, TextMessage): if event.message.text == "看看大家都傳了什麼圖片": client = ImgurClient(client_id, client_secret) images = client.get_album_images(album_id) index = random.randint(0, len(images) - 1) url = images[index].link image_message = ImageSendMessage(original_content_url=url, preview_image_url=url) line_bot_api.reply_message(event.reply_token, image_message) return 0 else: line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='輸入無效'), buttons_template]) return 0 ##elif :大盤指數 ##elif : #純粹echo '''
def get_imgur_photo(self, album_id): client = ImgurClient(self.imgur_client_id, self.imgur_client_secret) images = client.get_album_images(album_id) index = random.randint(0, len(images) - 1) url = images[index].link image_message = ImageSendMessage(original_content_url=url, preview_image_url=url) return image_message
def check_imgur(url): def get_album_id(url): m = re.search(r'/(?:a|gallery)/([^/?#]+)', url) return m.group(1) if m else None client = ImgurClient(IMGUR_CLIENT_ID, IMGUR_CLIENT_SECRET) album_id = get_album_id(url) images = client.get_album_images(album_id) return [img.link for img in images]
def imgur_beauty(self): client = ImgurClient(client_id, client_secret) images = client.get_album_images(album_id) index = random.randint(0, len(images) - 1) url = images[index].link image_message = ImageSendMessage( original_content_url=url, preview_image_url=url ) self.reply_message(image_message)
class ImgurPostsProvider(RedditHandler, Generator): def __init__(self): super(ImgurPostsProvider, self).__init__(IMGUR) cm = ConfigManager() self.client = ImgurClient(cm.get('ImgrClientID'), cm.get('ImgrClientSecret')) self.toggled = set() def get_copies(self, url): search_request = "url:\'%s\'" % _get_post_id(url) return list(self.reddit.search(search_request)) def check(self, image): if not image.title or hash(normalize(image.title)) in self.toggled or \ image.height < 500 or image.width < 500: return False copies = self.get_copies(image.id) if len(copies) == 0: return True def process_title(self, title): #todo fix by re if isinstance(title,str): title.replace("my", "") title.replace("My", "") title.replace("MY", "") title.replace("me", "") title.replace("Me", "") title.replace("ME", "") return title def generate_data(self, subreddit, key_words): try:#todo fix rate limit #todo add posts statuses for page in xrange(MAX_PAGES): q = "tag:%s OR title:%s OR album:%s" % (subreddit, subreddit, subreddit) log.info("retrieve for %s at page %s" % (subreddit, page)) for entity in self.client.gallery_search(q=q, sort='time', page=page, window='week'): if entity.is_album: if entity.ups - entity.downs > 0 and entity.ups > MIN_UPS: images = [random.choice(self.client.get_album_images(entity.id))] else: images=[] else: images = [entity] for image in images: if self.check(image): self.toggled.add(hash(normalize(image.title))) yield PostSource(image.link, self.process_title(image.title)) except Exception as e: log.exception(e) return
def parse_link_in_comment(item_content): comment = Comment() comment.content = item_content if re.match(IMAGE_LINK_REGEX,item_content): comment.isimg = True data = re.search(IMAGE_LINK_REGEX,item_content).group(1) comment.data = data elif re.match(VIDEO_LINK_REGEX,item_content): comment.isvideo = True comment.data = item_content elif re.match(YOUTU_LINK_REGEX,item_content): comment.isembed_video = True data_id = re.search(YOUTU_LINK_REGEX,item_content).group(4) start_time = calculate_starttime_youtube(item_content) if start_time != 0: data_id = data_id+ '?start=' +str(start_time) comment.data = 'https://www.youtube.com/embed/' + data_id elif re.match(YOUTUBE_LINK_REGEX,item_content): comment.isembed_video=True data_id = re.search(YOUTUBE_LINK_REGEX,item_content).group(4) start_time = calculate_starttime_youtube(item_content) if start_time != 0: data_id = data_id+ '?start=' +str(start_time) comment.data = 'https://www.youtube.com/embed/' + data_id elif re.match(GFYCAT_LINK_REGEX,item_content): comment.isgfy = True comment.data = re.search(GFYCAT_LINK_REGEX,item_content).group(4) elif re.match(IMGUR_ALBUM_LINK_REGEX,item_content): comment.isalbumimg =True client = ImgurClient(settings.IMGUR_CLIENT_ID, settings.IMGUR_CLIENT_SECRET) albumid = re.search(IMGUR_ALBUM_LINK_REGEX,item_content).group(4) images= client.get_album_images(albumid) data ='' for image in images: data = data+ image.link +',' comment.data = data[:len(data)-1] elif re.match(IMGUR_LINK_REGEX,item_content): comment.isimg = True comment.data = item_content +".jpg" elif re.match(ODDSHOT_LINK_REGEX,item_content): comment.isembed_video = True comment.data = item_content+'/embed' elif re.match(LIVECAP_REGEX,item_content): comment.isembed_video = True videoid = re.search(LIVECAP_REGEX,item_content).group(4) comment.data = r'https://www.livecap.tv/s/embed/' + videoid elif re.match(GENERAL_LINK_REGEX,item_content): comment.islink = True comment.data = item_content elif re.search(LINK_INLINE_REGEX,item_content): link_inline = re.search(LINK_INLINE_REGEX,item_content).group(1) link_inline_with_html = '<a href="' + link_inline +'" target="_blank">' + link_inline + '</a>' comment.content = re.sub(LINK_INLINE_REGEX,link_inline_with_html,item_content) return comment
def language(): client = ImgurClient(client_id, client_secret) images = client.get_album_images(album_id) intro = '英文是我一直以來都有在持續學習的語言 對於聽說讀寫都有一定的信心\n' + \ '大二時考的多益成績是810分' + '\uDBC0\uDC8D' * 2 + '\n' \ '且因為自己的興趣 大學時曾修過基礎韓文 以下是我的多益成績單和韓文課證書' + '\uDBC0\uDC30' content = [TextSendMessage(intro), ImageSendMessage(original_content_url=images[0].link,preview_image_url=images[0].link), ImageSendMessage(original_content_url=images[1].link,preview_image_url=images[1].link), ] return content
def get_imgur_urls(self, url): c_id = '45b94b4d0013b7a' split = url.split('/') if self.has_extension(split[len(split)-1]): yield url return client = ImgurClient(client_id=c_id, client_secret=None) if 'a' == split[len(split)-2]: split = split[len(split)-1].split('#')[0] for img in client.get_album_images(split): yield img.link else: yield client.get_image(split[len(split)-1]).link
def albumToImage(album): client_id = 'ayy' client_secret = 'lmao' client = ImgurClient(client_id, client_secret) lolisAlbum = client.get_album_images(album) loliList = [] for loli in lolisAlbum: loliList.append(loli.link) chosenLoli = random.choice(loliList) return chosenLoli
def download_album_images(aid, filename, postTitle): client = ImgurClient(im_client, im_secret) imgs = None try: imgs = client.get_album_images(aid) except Exception: return #print("Downloading Album: " +postTitle) if not os.path.exists(filename+postTitle): os.mkdir(filename+postTitle) for image in imgs: fn = filename + postTitle + os.sep + ((image.link).split(".com/"))[1] downloadImage(image.link, fn, postTitle) return
def _get_image_from_album(self, album): if not isinstance(album, GalleryAlbum): raise ValueError("Album should be " "a GalleryAlbum instance!") # Download gallery data client = ImgurClient(self.client_id, None) album_id = album.id images = client.get_album_images(album_id) # Try to select an appropriate image from this album, return the # first one that fits our criteria. if images is not None: return self._select_image(images)
async def owlturd(self, ctx): client_id = os.getenv("IMGUR_CLIENT_ID") client_secret = os.getenv("IMGUR_CLIENT_SECRET") client = ImgurClient(client_id, client_secret) galleries = ["KQELY", "MJBPd"] images = client.get_album_images(random.choice(galleries)) random_idx = random.randrange(0, len(images)) random_comic = images[random_idx].link embed = discord.Embed(title="Owl Turd", url="https://www.gocomics.com/shen-comix") embed.set_image(url=random_comic) await ctx.send(embed=embed)
async def lovenstein(self, ctx): client_id = os.getenv("IMGUR_CLIENT_ID") client_secret = os.getenv("IMGUR_CLIENT_SECRET") client = ImgurClient(client_id, client_secret) galleries = ["6h7o9", "MhDJD", "Spqb6Oj", "Fm9cQ"] images = client.get_album_images(random.choice(galleries)) random_idx = random.randrange(0, len(images)) random_comic = images[random_idx].link embed = discord.Embed(title="MrLovenstein", url="https://www.mrlovenstein.com/") embed.set_image(url=random_comic) await ctx.send(embed=embed)
def _get_image_from_album(self, album): if not isinstance(album, GalleryAlbum): raise ValueError("Album should be " "a GalleryAlbum instance!") # Download gallery data client = ImgurClient(self.client_id, None) album_id = album.id images = client.get_album_images(album_id) # Try to select an appropriate image from this album, return the # first one that fits our criteria. if images is not None: return self._select_image(images)
def api_my(): config = configparser.ConfigParser() config.read('auth.ini') client_id = config.get('credentials', 'client_id') client_secret = config.get('credentials', 'client_secret') client = ImgurClient(client_id, client_secret) items = client.get_album_images('5pZ3ISJ') myresults = [] for picc in items: myresults.append(picc.link) return jsonify({'Uploaded': myresults})
def get_image(text): if len(text) < 4: return None else: tex = text[3:] t = fb.get('/pic', None) for key, value in t.items(): if value['describe'] == tex: client = ImgurClient(client_id, client_secret) images = client.get_album_images(album_id) img_id = int(value['id']) - 1 #從firebase取出來是字串 url = images[img_id].link image_message = ImageSendMessage(original_content_url=url, preview_image_url=url) return image_message
async def loading(self, ctx): client_id = os.getenv("IMGUR_CLIENT_ID") client_secret = os.getenv("IMGUR_CLIENT_SECRET") client = ImgurClient(client_id, client_secret) galleries = ["eqog8N3", "V4983", "nk7dK", "J5hdR"] images = client.get_album_images(random.choice(galleries)) random_idx = random.randrange(0, len(images)) random_comic = images[random_idx].link embed = discord.Embed(title="Loading Artist", url="https://loadingartist.com/") embed.set_image(url=random_comic) await ctx.send(embed=embed)
def main(): args.directory = fix_path(args.directory) args.blacklisted = fix_path(args.blacklisted) directories = [args.directory, args.blacklisted] for dir in directories: dir = fix_path(dir) if not os.path.exists(dir): print(dir, " is not a valid directory on the system") exit() if args.verbose >= 1 and args.force_aspect_ratio: print("Finding pictures with aspect ratio of", args.force_aspect_ratio) elif args.verbose >= 1 and not args.force_aspect_ratio and ( not args.force_height or not args.force_width): print("Finding pictures with minimum resolution of", args.min_width, "x", args.min_height, "and maximum resolution of", args.max_width, "x", args.max_height) elif args.verbose >= 1 and not args.force_aspect_ratio and args.force_height or args.force_width: print("Finding pictures with exact resolution of", args.min_width, "x", args.min_height) if args.album: client_id = "6c478b24a403475" client_secret = "ebdf34952fad765ee2e0946de30933e4936c1f0d" client = ImgurClient(client_id, client_secret) for album in args.album: items = client.get_album_images(album) for picture in items: first_stage(picture.link) else: reddit = praw.Reddit(client_id='P31W4FjRB6gJ0Q', client_secret='mulKi79w35XYQPMngRoQKn_L_yk', user_agent='Wallpapers by /u/454Casull') subreddit = reddit.subreddit(args.subreddit) if args.search_term: for submission in subreddit.search(args.search_term): if not submission.over_18: first_stage(submission.url) else: for submission in subreddit.top('day'): if not submission.over_18: first_stage(submission.url) if not args.no_delete_old_files: delete_old_files(args.directory, 1) delete_old_files(args.blacklisted, 2)
def certification(self): # get album from imgur with 4 random certification client = ImgurClient(IMGUR_CLIENT_ID, IMGUR_CLIENT_SECRET) images = client.get_album_images(IMGUR_ALBUM_ID) num = len(images) image_list=[] for i in range(0,num): image_list.append(i) # get 4 random certification image_list = random.sample(image_list, 4) print(image_list) image_carousel_template_message = TemplateSendMessage( alt_text='SAM的Certification', template=ImageCarouselTemplate( columns=[ ImageCarouselColumn( image_url=images[image_list[0]].link, action=URIAction( uri=images[image_list[0]].link ) ), ImageCarouselColumn( image_url=images[image_list[1]].link, action=URIAction( uri=images[image_list[1]].link ) ), ImageCarouselColumn( image_url=images[image_list[2]].link, action=URIAction( uri=images[image_list[2]].link ) ), ImageCarouselColumn( image_url=images[image_list[3]].link, action=URIAction( uri=images[image_list[3]].link ) ), ]) ) return image_carousel_template_message
def handle_message(event): client_id = 'de5ecd12cacaf0a' client_secret = '3556bb208ceda936f8c53033b861041cb878d878 ' client = ImgurClient(client_id, client_secret) a = client.get_account_albums("kevin86117") images = client.get_album_images(a[0].id) index = random.randint(0, len(images) - 1) url = images[index].link if event.message.text == "corgi" or event.message.text == "柯基": message = ImageSendMessage(original_content_url=url, preview_image_url=url) elif event.message.text == "news": content = technews() line_bot_api.reply_message(event.reply_token, TextSendMessage(text=content)) else: message = TextSendMessage(text=event.message.text) line_bot_api.reply_message(event.reply_token, message)
async def cyanide(self, ctx): client_id = os.getenv("IMGUR_CLIENT_ID") client_secret = os.getenv("IMGUR_CLIENT_SECRET") client = ImgurClient(client_id, client_secret) galleries = [ "Q28iX", "F3MUq", "Hs78vjZ", "4irAcqH", "XyPBv", "ytSSEEo" ] images = client.get_album_images(random.choice(galleries)) random_idx = random.randrange(0, len(images)) random_comic = images[random_idx].link embed = discord.Embed(title="Cyanide & Happiness", url="http://explosm.net/") embed.set_image(url=random_comic) await ctx.send(embed=embed)
def getFromImgur(**kwargs): if client == None: client = ImgurClient(client_id, client_secret) items = client.gallery_search("loli", advanced=None, sort='time', window='all', page=0) randNum = randint(0, len(items) - 1) if items[randNum]: try: image = client.get_album_images(items[randNum].id)[0] except Exception as e: print(e) return getFromImgur() if image.animated: return {"link": image.mp4, "gif": True} else: return {"link": image.link, "gif": False}
def imgur(request, section='user', sort='rising', page=0): if request.method == "POST": form = GrabberForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.time_added = datetime.now() post.downloaded = False post.save() return redirect('post_detail', pk=post.pk) else: form = GrabberForm() page = int(page) client_id = getattr(settings, 'IMGUR_CLIENT_ID', '') client_secret = getattr(settings, 'IMGUR_CLIENT_TOKEN', '') client = ImgurClient(client_id, client_secret) current_limits = client.credits items = client.gallery(section=section, sort=sort, page=page, show_viral=True) album_images = {} form = GrabberForm() # images = client.get_album_images(items[1].id) # Grab all the images in album for item in items: if item.is_album is True: id = item.id images = client.get_album_images(item.id) album_images[id] = images # album_images[id] = "test" return render( request, 'webapp/dashboard.html', { 'items': items, 'current_limits': current_limits, 'album_images': album_images, 'section': section, 'sort': sort, 'page': page, 'form': form, })
def scrapeAlbum(setting, albumURL): from imgurpython import ImgurClient from image_scraping import saveImage from database_tools import getImgurAlbums, addImgurAlbum start = albumURL.find('''/a/''') if (start == -1): return "This was not an album." if ('#' in albumURL): end = albumURL.find('#') else: end = len(albumURL) album_id = albumURL[start+3:end] if (album_id in getImgurAlbums()): return "That album was scraped already." client_id, client_secret = settings.getImgurToken().split(',') client = ImgurClient(client_id, client_secret) try: images = client.get_album_images(album_id) except: return "That album does not exist." addImgurAlbum(album_id) count = 0 for image in images: lol = saveImage(setting, image.link) count += 1 return count
'positive_album_id': '0XfM8', 'negative_album_id': 'NX3p8' }, { 'album_id': '1EN5T', 'positive_album_id': 'OthOo', 'negative_album_id': 'jQIwZ' }] for album in albums: positive_images = [] negative_images = [] album_id = album['album_id'] positive_album_id = album['positive_album_id'] negative_album_id = album['negative_album_id'] for image in client.get_album_images(album_id): webbrowser.open(image.link) answer = raw_input('Activity on picture? (y/n/s): ') if answer == 's': break elif answer == 'y': positive_images.append(image.id) else: negative_images.append(image.id) client.album_add_images(positive_album_id, positive_images) client.album_add_images(negative_album_id, negative_images) client.album_remove_images(album_id, positive_images) client.album_remove_images(album_id, negative_images)
class ImgurStorage(Storage): """ A storage class providing access to resources in a Dropbox Public folder. """ def __init__(self, location='/'): self.client = ImgurClient( CONSUMER_ID, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_REFRESH) logger.info("Logged in Imgur storage") self.account_info = self.client.get_account(USERNAME) self.albums = self.client.get_account_albums(USERNAME) self.location = location self.base_url = 'https://api.imgur.com/3/account/{url}/'.format(url=self.account_info.url) def _get_abs_path(self, name): return os.path.join(self.location, name) def _open(self, name, mode='rb'): remote_file = self.client.get_image(name, self, mode=mode) return remote_file def _save(self, name, content): name = self._get_abs_path(name) directory = os.path.dirname(name) logger.info([a.title for a in self.albums]) logger.info(name) logger.info(directory) if not self.exists(directory) and directory: album = self.client.create_album({"title": directory}) self.albums = self.client.get_account_albums(USERNAME) album = [a for a in self.albums if a.title == directory][0] #if not response['is_dir']: # raise IOError("%s exists and is not a directory." % directory) response = self._client_upload_from_fd(content, {"album": album.id, "name": name, "title": name}, False) return response["name"] def _client_upload_from_fd(self, fd, config=None, anon=True): """ use a file descriptor to perform a make_request """ if not config: config = dict() contents = fd.read() b64 = base64.b64encode(contents) data = { 'image': b64, 'type': 'base64', } data.update({meta: config[meta] for meta in set(self.client.allowed_image_fields).intersection(config.keys())}) return self.client.make_request('POST', 'upload', data, anon) def delete(self, name): name = self._get_abs_path(name) self.client.delete_image(name) def exists(self, name): name = self._get_abs_path(name) if len([a for a in self.albums if a.title == name]) > 0: return True try: album = [a for a in self.albums if a.title == os.path.dirname(name)][0] images = self.client.get_album_images(album.id) metadata = self.client.get_image(name) if len([im for im in images if im.name == name]) > 0: logger.info(dir(metadata)) return True except ImgurClientError as e: if e.status_code == 404: # not found return False raise e except IndexError as e: return False else: return True return False def listdir(self, path): path = self._get_abs_path(path) response = self.client.get_image(path) directories = [] files = [] for entry in response.get('contents', []): if entry['is_dir']: directories.append(os.path.basename(entry['path'])) else: files.append(os.path.basename(entry['path'])) return directories, files def size(self, path): cache_key = 'django-imgur-size:%s' % filepath_to_uri(path) size = cache.get(cache_key) if not size: directory = os.path.dirname(path) name = os.path.basename(path) album = [a for a in self.albums if a.title == directory][0] images = self.client.get_album_images(album.id) image = [im for im in images if im.name == path][0] size = self.client.get_image(image.id).size cache.set(cache_key, size) return size def url(self, path): cache_key = 'django-imgur-url:%s' % filepath_to_uri(path) url = cache.get(cache_key) if not url: directory = os.path.dirname(path) name = os.path.basename(path) album = [a for a in self.albums if a.title == directory][0] images = self.client.get_album_images(album.id) image = [im for im in images if im.name == path][0] url = self.client.get_image(image.id).link cache.set(cache_key, url) return url def get_available_name(self, name, max_length=None): """ Returns a filename that's free on the target storage system, and available for new content to be written to. """ #name = self._get_abs_path(name) #dir_name, file_name = os.path.split(name) #file_root, file_ext = os.path.splitext(file_name) ## If the filename already exists, add an underscore and a number (before ## the file extension, if one exists) to the filename until the generated ## filename doesn't exist. #count = itertools.count(1) #while self.exists(name): # # file_ext includes the dot. # name = os.path.join(dir_name, "%s_%s%s" % (file_root, count.next(), file_ext)) return name
class Getter(threading.Thread): def __init__(self,dwQ,errQ,stopped,filePath,initFile,cfreader): threading.Thread.__init__(self,daemon=False) self.errorQueue=errQ self.downloadQueue=dwQ self.stopped=stopped self.imgurClient=ImgurClient("4ff2bb9d9c640f2", "8b036ffa680a1304814f48eff9e93206c096727f") self.paramReader=cfreader self.downloadPath=PathHolder() self.downloadPath = self.paramReader.readParam(filePath+ "\\" + initFile,self.downloadPath) logger = logging.getLogger('Image_Downloader') logger.setLevel(logging.DEBUG) fh = logging.FileHandler('logs/downloads.log') fh.setLevel(logging.DEBUG) # create console handler with a higher log level ch = logging.StreamHandler() ch.setLevel(logging.ERROR) # create formatter and add it to the handlers formatter = logging.Formatter('[%(filename)s:%(lineno)s - %(funcName)20s() ] %(asctime)s %(levelname)s:%(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # add the handlers to the logger logger.addHandler(fh) logger.addHandler(ch) logger.debug("-----------------------------") logger.debug("Init complete") def run(self): logger= logging.getLogger('Image_Downloader') logger.debug("Thread started") while not self.stopped.is_set(): self.getURL() def getURL(self): logger= logging.getLogger('Image_Downloader') while not self.downloadQueue.empty(): downloadList=None try: downloadList=self.downloadQueue.get() except: continue self.downloadQueue.task_done() logger.debug("Dequeued and marked done") for downloadObject in downloadList: if "imgur" in downloadObject.domain: logger.debug("Imgur URL found") fileName, fileExtension = os.path.splitext(downloadObject.url) if "/a/" in fileName: albumPart=downloadObject.url.split("/a/")[1] albumName=albumPart.split('/')[0].split('#')[0] self.handleAlbum(albumName,downloadObject.subreddit,downloadObject.id,downloadObject.title) elif fileExtension is not None and len(fileExtension) > 0: self.handleImage(downloadObject.url,downloadObject.subreddit,downloadObject.id,downloadObject.title,fileExtension) else: fileId=downloadObject.url.split("/")[-1] for f in fileId.split(","): self.handleURL(f,downloadObject.subreddit,downloadObject.id,downloadObject.title) else: logger.debug("Non imgur URL") self.errorQueue.put(ErrorLog(subredditName=downloadObject.subreddit,failedItemName=downloadObject.id,failReason="Domain not suppported")) logger.debug("Returning from fuction") def handleImage(self,url,redditName,id,title,fileExtension): logger= logging.getLogger('Image_Downloader') directory=self.downloadPath.pathToHold + redditName valid_chars = "-_.%s%s" % (string.ascii_letters, string.digits) name=''.join(c for c in title if c in valid_chars) fname = (name[:30]) if len(name) > 30 else name fileExtension=fileExtension.split("?")[0] file=directory + "\\" + fname +"_" + id + fileExtension logger.debug("From "+ url + "to " + file) try: if not os.path.exists(directory): os.mkdir(directory) except OSError as e: logger.debug(e.strerror) self.errorQueue.put(ErrorLog(subredditName=redditName,failedItemName=id,failReason=e.strerror,retries=0)) if not os.path.exists(file) and not os.path.exists(directory + "\\" + name +"_" + id + fileExtension): try: r = requests.get(url, stream=True) if r.status_code == 200: with open(file, 'wb') as f: for chunk in r.iter_content(1024): f.write(chunk) except (requests.exceptions.RequestException,requests.exceptions.ConnectionError,requests.exceptions.HTTPError,requests.exceptions.URLRequired,requests.exceptions.TooManyRedirects,requests.exceptions.ConnectTimeout,requests.exceptions.ReadTimeout,requests.exceptions.Timeout) as e: logger.debug(e.__class__.__name__) self.errorQueue.put(ErrorLog(subredditName=redditName,failedItemName=id,failReason=e.__class__.__name__,retries=0)) except OSError as e: logger.debug(e.strerror +" " + file ) self.errorQueue.put(ErrorLog(subredditName=redditName,failedItemName=id,failReason=e.strerror,retries=0)) def handleAlbum(self,albumName,redditName,id,title): logger= logging.getLogger('Image_Downloader') logger.debug("Found an Album to download" + albumName) for imageObject in self.imgurClient.get_album_images(albumName): logger.debug("Next item in the album") self.handleImageObject(imageObject,redditName,title) def handleURL(self,fileId,subreddit,id,title): logger= logging.getLogger('Image_Downloader') logger.debug("Found a wrapped image: "+ fileId) try: image=self.imgurClient.get_image(fileId) logger.debug("Got the image back " ) self.handleImageObject(image,subreddit,title) logger.debug("Done with this wrap") except ImgurClientError as e: logger.debug(e.error_message) logger.debug(e.status_code) except Exception as e: logger.debug(type(e)) def handleImageObject(self,imageObject,reddit,title): logger= logging.getLogger('Image_Downloader') logger.debug("Got the an image to download " ) fileName, fileExtension = os.path.splitext(imageObject.link) if fileExtension is not None and len(fileExtension) > 0: self.handleImage(imageObject.link,reddit,imageObject.id,title,fileExtension)
class Engraver: def __init__(self, bot): self.bot = bot self.client = ImgurClient(imgur_client_id,imgur_client_secret) @commands.command() async def imgur (self, *topic): """ Performs an Imgur search on the given topic. Arguments: - topic: Topic to search for. """ if self.client.credits["ClientRemaining"] < 10: message = "**Error:** Imgur API daily limit exceeded." else: topic = " ".join(topic) search = self.client.gallery_search(topic) if len(search) > 0: item = search.pop() message = item.link else: message = "**Error:** no Imgur search results for {}.".format(topic) await(self.bot.say(message)) @commands.group(name="album", pass_context=True) async def album (self, ctx, name): """ Pulls a random image from a known album. Arguments: - name: Name of album to pull from. """ if ctx.invoked_subcommand is None: if self.client.credits["ClientRemaining"] < 10: message = "**Error:** Imgur API daily limit exceeded." else: sid = ctx.message.server.id if sid == None: message = "**Error:** Cannot use albums by PM." aid = get_album_id(sid,name) if aid == None: message = "**Error:** No known album {}.".format(name) else: album = self.client.get_album_images(aid) if album == None: message = "**Error:** invalid album ID {} for name {}. Contact the administrator.".format(aid,name) message = item.link await(self.bot.say(message)) @album.command(pass_context=True) async def create (self, ctx, name, image=None): """ Creates an album, containing the given image. If the album already exists, Arguments: - name: Name of album to be created to. - image: Link to the image to be added. If this is omitted, will check message for attached file. """ sid = ctx.message.server.id if image == None: if len(ctx.message.attachments) > 0: image = ctx.message.attachments[0].link else: image = "**Error:** No image provided." await(self.bot.say(image))
if test == "exit": sys.exit() try: test_id = test[test.index('a/')+2:] album = client.get_album(test_id) id = test_id except Exception,e: print '[-] Error: Invalid imgur album link' # test path: C:/Users/st/Documents/GitHub/Auto-Scripts/ dest = '' while dest == '': tet_dest = raw_input('[*] Please enter a destination path: ') if os.path.exists(test_dest): dest = test_dest else: print '[-] Error: Invalid destination path' print '[+] Downloading imgur album...' images = client.get_album_images(id) count = 0 for i in images: link = i.link print '[*] Downloading ', link, '...' ext = link[link.rindex('.')+1:] filename = dest + "/" + str(count) + "." + ext urllib.urlretrieve(link, filename) count += 1 raw_input('[+] Finished downloading album')
parser.add_option("-r", "--reddit", dest="subreddit") parser.add_option("-p", "--page", dest="page", default=0) (options, args) = parser.parse_args() path = options.path client_id = '' client_secret = '' if(options.album_id != None): album_id = options.album_id print("Download dell'album con id = " + album_id) print("Destinazione = " + path) cl = ImgurClient(client_id, client_secret) items = cl.get_album_images(album_id) image_n = str(len(items)) i = 1 for item in items: image_url = item.link print("Immagine " + str(i) + " su " + image_n + " " + image_url) i+=1 urllib.request.urlretrieve(image_url, path + item.id + image_url[image_url.rfind('/')+1:]) if(options.subreddit != None): subreddit = options.subreddit print("Download della reddit = " + subreddit) print("Destinazione = " + path) cl = ImgurClient(client_id, client_secret)
def main(): if not os.path.isfile('credentials.config'): # if credentials file does not exist, start the first run function first_run() # Authenticate and generate the credentials file. # command line switches function args = read_command_args() use_evernote = args.e debug_mode = args.debug delete_files = args.t if use_evernote is True else False path = args.p info_mode = args.i if debug_mode: # print("Warning - Debug mode active. Files will be downloaded, but not added to index") logger = create_logger(log_to_console=True) logger.setLevel(logging.DEBUG) logger.info('Warning - Debug mode active. Files will be downloaded, but not added to index') elif info_mode: warnings.warn("Suppressed Resource warning", ResourceWarning) # suppresses sll unclosed socket warnings. logger = create_logger(log_to_console=True) else: warnings.warn("Suppressed Resource warning", ResourceWarning) # suppresses sll unclosed socket warnings. logger = create_logger() logger.info("\n###########\nStarting SR\n###########") try: with open('credentials.config', 'r') as json_file: credentials = json.load(json_file) # get various OAuth tokens except OSError: logger.error('Unable to open credentials file') raise SystemExit # Create the downloads folder on the specified path, or in the dir where file is stored. if path is not "": path = path[0] else: path = os.getcwd() path += "/SRDownloads" if not os.path.exists(path): os.makedirs(path) # Authenticate with Reddit logger.info('Authenticating with Reddit') client_id = credentials['reddit']['client_id'] client_secret = credentials['reddit']['client_secret'] redirect_uri = credentials['reddit']['redirect_uri'] refresh_token = credentials['reddit']['refresh_token'] user_agent = "SavedRetriever 0.9 by /u/fuzzycut" try: r = praw.Reddit(user_agent=user_agent, oauth_client_id=client_id, oauth_client_secret=client_secret, oauth_redirect_uri=redirect_uri) access_information = r.refresh_access_information(refresh_token) r.set_access_credentials(**access_information) except Exception as e: logger.error(e) raise SystemExit time_since_accesstoken = time.time() index = set() if os.path.isfile('index.txt'): # checking for index file, which contains index of downloaded files. try: with open('index.txt', 'r') as ind: for line in ind: index.add(line[:-1]) # -1 truncates the newline in the index file. except OSError: logger.error("Unable to open index file for reading") raise SystemExit if use_evernote is True: enclient = evernoteWrapper.Client(credentials['evernote']['dev_token'], 'Saved from Reddit') html_index_file = None if delete_files is False: # only create index if we're going to use it. html_index_file = html_index.index(r.get_me().name, path) try: ind = open('index.txt', 'a') # open index file for appending except OSError: logger.error("Unable to open index file for writing") raise SystemExit logger.info("Beginning to save files...") for i in r.get_me().get_saved(limit=None): if (time.time() - time_since_accesstoken) / 60 > 55: # Refresh the access token before it runs out. logger.debug('Refreshing Reddit token') r.refresh_access_information(access_information['refresh_token']) time_since_accesstoken = time.time() name = i.name file_name = name # to stop ide complaining. note = None evernote_tags = ('Reddit', 'SavedRetriever', '/r/' + i.subreddit.display_name) # add config for this later # logger.info('Saving post - {}'.format(name)) if name not in index: # file has not been downloaded permalink = i.permalink author = i.author title = i.link_title if hasattr(i, 'link_title') else i.title # ========== # # IS COMMENT # # ========== # if hasattr(i, 'body_html'): logger.debug("{} is comment".format(name)) body = i.body_html # html output body = subreddit_linker(body) output = html_output_string(permalink, author, body, title) if delete_files is False: file_name = html_writer(path, name, output) # en api section if use_evernote is True: enclient.new_note(title) enclient.add_html(output) enclient.add_tag(*evernote_tags) # the * is very important. It unpacks the tags tuple properly note = enclient.create_note() # ============ # # IS SELF-POST # # ============ # elif hasattr(i, 'is_self') and i.is_self is True: logger.debug('{} is self-post'.format(name)) text = i.selftext_html if i.selftext_html is not None else "" # html output text = subreddit_linker(text) output = html_output_string(permalink, author, text, title) if delete_files is False: file_name = html_writer(path, name, output) # en api section if use_evernote is True: enclient.new_note(title) enclient.add_tag(*evernote_tags) enclient.add_html(output) note = enclient.create_note() # ====================== # # IS DIRECT LINKED IMAGE # # ====================== # elif hasattr(i, 'url') and re.sub("([^A-z0-9])\w+", "", i.url.split('.')[-1]) in ['jpg', 'png', 'gif', 'gifv', 'pdf']: """ Need to check file types and test pdf. How does this handle gfycat and webm? Can EN display that inline? The regex in the if is to strip out non-valid filetype chars. """ logger.debug('{} is direct linked image'.format(name)) url = i.url base_filename = "{}_image.{}".format(name, re.sub("([^A-z0-9])\w+", "", url.split('.')[ -1])) # filename for image. regex same as above. filename = path + "/" + base_filename # image downloader section if os.path.exists(filename) and (os.path.getsize(filename) > 0): # If image exists and is valid image_downloaded = True logger.info("Image already exists - {}".format(base_filename)) else: image_downloaded = image_saver(url, filename) logger.info('Downloaded image - {}'.format(base_filename)) if image_downloaded: # write image as <img> or link to local pdf downloaded in html file if filename.split('.')[-1] == 'pdf': img = '<a href="{}">Click here for link to downloaded pdf</a>'.format(base_filename) else: img = '<br><a href="{0}"><img src="{0}"></a>'.format( base_filename) # html for embedding in html file else: img = "Image failed to download - It may be temporarily or permanently unavailable" # Evernote api section if use_evernote is True: enclient.new_note(title) enclient.add_tag(*evernote_tags) enclient.add_html(html_output_string_image(permalink, author, "", title)) # should add body="" in the function if image_downloaded: enclient.add_resource(filename) note = enclient.create_note() if delete_files is False: file_name = html_writer(path, name, html_output_string_image(permalink, author, img, title)) else: os.remove(filename) # ============== # # IS IMGUR ALBUM # # ============== # elif hasattr(i, 'url') and 'imgur' in i.url: # Add option to download images to folder. logger.debug('{} is Imgur album'.format(name)) url = i.url body = "<h2>{}</h2>".format(title) # imgur api section client = ImgurClient(credentials['imgur']['client_id'], credentials['imgur']['client_secret']) pattern = '\/([A-z0-9]{5,7})' # matches any 5-7 long word that comes after a forward slash (/). match = re.findall(pattern, url) gallery_id = match[-1].replace('/', '') # removes any forward slashes for processing gallery = [] filename = None try: gallery = client.get_album_images(gallery_id) except imgurpython.helpers.error.ImgurClientError: # if 'gallery' is actually just a lone image try: gallery = [client.get_image(gallery_id)] except imgurpython.helpers.error.ImgurClientError as error: # if gallery does not exist. Is this the best way to do this? if debug_mode is True or error.status_code != 404: print("**{} - {}**".format(error.status_code, error.error_message)) # img_path = 'Downloads/{}'.format(gallery_id) img_path = path + "/" + gallery_id if not os.path.exists(img_path): os.makedirs(img_path) for image in gallery: # add if gallery > 10, then just add a link (would be too large for the note) image_name = image.title if image.title is not None else "" image_description = image.description if image.description is not None else "" image_filetype = image.type.split('/')[1] image_id = image.id image_link = image.link # sets up downloaded filename and html for embedding image base_filename = "{}_image.{}".format(image_id, image_filetype) img = '<p><h3>{0}</h3><a href="{1}/{2}"><img src="{1}/{2}"></a><br/>{3}</p>'.format(image_name, gallery_id, base_filename, image_description) filename = img_path + "/" + base_filename if os.path.exists(filename) and (os.path.getsize(filename) > 0): # only download if file doesn't already exist logger.info('Image already exists - {}'.format(base_filename)) else: image_saver(image_link, filename) logger.info('Image downloaded - {}'.format(base_filename)) body += img # Evernote api section if use_evernote is True: enclient.new_note(title) enclient.add_tag(*evernote_tags) if len(gallery) == 1 and filename is not None: enclient.add_html(html_output_string_image(permalink, author, "", title)) enclient.add_resource(filename) else: enclient.add_html(html_output_string_image(permalink, author, 'This album is too large to embed; please see <a href="{}">here</a> for the original link.'.format(url), title)) note = enclient.create_note() if delete_files is False: file_name = html_writer(path, name, html_output_string_image(permalink, author, body, title)) else: shutil.rmtree(img_path) # ========== # # IS ARTICLE # # ========== # elif hasattr(i, 'title') and i.is_self is False: # This section needs work. It is semi-complete. Ultimately, adding in the full article is the goal. logger.debug('{} is article/webpage'.format(name)) url = i.url # readability api section os.environ["READABILITY_PARSER_TOKEN"] = credentials['readability'][ 'parser_key'] # set the environment variable as the parser key logger.info('Initializing Readability Client') parse = ParserClient() # readability api doesn't take the token directly parse_response = parse.get_article(url) article = parse_response.json() if 'content' not in article: # if unable to parse document, manually set an error message article['content'] = 'Unable to parse page - See <a href="{}">here</a> for the original link'.format(url) article = article['content'] article = "<a href='{}'>{}</a><br/>{}<br/>".format(url, title, article) # source of article # html output section. output = html_output_string(permalink, author, article, title) if delete_files is False: file_name = html_writer(path, name, output) # Evernote section if use_evernote is True: enclient.new_note(title) enclient.add_tag(*evernote_tags) output = html_output_string(permalink, author, article, title) enclient.add_html(output) # Add html file to note # enclient.add_resource("Downloads/{}.html".format(name)) note = enclient.create_note() # end of checking for saved items # failed_upload = False if use_evernote is True: if note is not None: # print("Saved {:9} - GUID: {}".format(name, note.guid)) logger.info('Saved {:9} - GUID: {}'.format(name, note.guid)) else: # Upload failed # print("Saved {:9} - Note failed to upload".format(name)) logger.info('Saved {:9} - Note failed to upload'.format(name)) failed_upload = True elif use_evernote is False: # print("Saved " + name) logger.info('Saved ' + name) if not debug_mode and not failed_upload: ind.write(name + "\n") ind.flush() # this fixes python not writing the file if it terminates before .close() can be called if delete_files is False: html_index_file.add_link(title, file_name, permalink) # end of for loop ind.close() logger.info("All items downloaded") if delete_files is False: html_index_file.save_and_close() else: # try remove downloads if -t is set, but don't force it if directory has things in it already. try: os.rmdir('Downloads') except OSError: logger.error("Unable to remove files")
global gif_count if(type == "image/jpeg"): jpeg_count += 1 elif(type == "image/png"): png_count += 1 elif(type == "image/gif"): gif_count += 1 # Crawling Requests items = client.gallery(page=0) for item in items: print(item.link) if(not item.is_album): countImage(item.type) else: for image in client.get_album_images(item.id): countImage(image.type) total_count = jpeg_count + png_count + gif_count print("JPEG:" + str(jpeg_count) + " " + str(round(100 * (float(jpeg_count) / float(total_count)), 2)) + "%") print("PNG:" + str(png_count) + " " + str(round(100 * (float(png_count) / float(total_count)), 2)) + "%") print("GIF:" + str(gif_count) + " " + str(round(100 * (float(gif_count) / float(total_count)), 2)) + "%") print("Total:" + str(total_count)) new_rows = [] with open('imgur_count.csv', 'rb') as f: reader = csv.reader(f) for row in reader: new_rows.append(row)
<label class="grid-item" for="pic-3"><img id="thumb-img" src="http://i.imgur.com/9qmVznh.jpg" /></label> <label class="grid-item" for="pic-4"><img id="thumb-img" src="http://i.imgur.com/ayXoHBC.jpg" /></label> <label class="grid-item" for="pic-5"><img id="thumb-img" src="http://i.imgur.com/ckAOa0x.jpg" /></label> <label class="grid-item" for="pic-6"><img id="thumb-img" src="http://i.imgur.com/jmbpQqC.jpg" /></label> </div> <br /></div> </div> """ client_secret = "a739d0a7bcaccb1eb06250d794b9cfbd91d342a8" client_id = "be7f0181f9aa600" client = ImgurClient(client_id, client_secret) mainarr = [] thumbarr = [] # mainitem = (client.get_album_images(mainphotolink)) for i in client.get_album_images(mainphotolink): mainarr.append(i.link) for i in client.get_album_images(thumblink): thumbarr.append(i.link) # print (mainarr) i = -1 soup = BeautifulSoup(hml_doc, "html.parser") for x in soup.find_all(id="main-img"): i = i + 1 x["src"] = mainarr[i] i = -1 for x in soup.find_all(id="thumb-img"): i = i + 1
doc = SimpleDocTemplate(path,pagesize=letter, rightMargin=25,leftMargin=25, topMargin=25,bottomMargin=25) ParagraphStyle(name = 'Normal', fontName = "Verdana", fontSize = 11, leading = 15, alignment = TA_JUSTIFY, allowOrphans = 0, spaceBefore = 20, spaceAfter = 20, wordWrap = 1) Story=[] styles=getSampleStyleSheet() items = client.get_album_images(str(args.album)) p = 0 for item in items: p += 1 bar = pb.ProgressBar(maxval=p).start() p = 1 for item in items: response = requests.get(item.link, stream=True) name = str(item.id)+".jpg" with open(name, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) del response sc = PIL.Image.open(name) width, height = sc.size