async def telegraph(media): """Gives telegraph link of a given media. No text.""" if not media.reply_to_msg_id: message = await media.edit( "Reply to a media to get its telegraph link.") time.sleep(3) return await message.delete() await media.edit("Starting...") sttime = datetime.now() Media = await media.get_reply_message() Downloaded = await media.client.download_media(Media) if not Media.media: message = await media.edit("That doesn't look like " "a **media** to me.") time.sleep(5) return await message.delete() telegraph = Telegraph() account = telegraph.create_account(short_name="Paperplane") auth_url = account["auth_url"] if Media.file.ext not in (([".jpg", ".jpeg", ".png", ".gif", ".mp4"])): await media.edit("I don't support this media.") return await media.edit("Downloaded media.") time.sleep(0.5) await media.edit("Created telegraph account.") if BOTLOG: await media.client.send_message( BOTLOG_CHATID, "#TELEGRAPH\n" "Created an account in telegraph: \n" "[Account Link]" "(" + auth_url + ")" "š") try: tlg_url = upload.upload_file(Downloaded) except exceptions.TelegraphException as error: await media.edit("Oh no! I got an error.") time.sleep(1) await media.edit("**REPORT**\n" f"`{str(error)}`") return entime = datetime.now() time_passed = entime - sttime time_taken = time_passed.seconds await media.edit("ā¢ Your telegraph link is here: " "[link]" f"(https://telegra.ph{tlg_url[0]})" "\nā¢ Uploaded in " f"{time_taken} secs.")
def new_post(client, message): post = message.text link = re.findall(r'(https?://\S+)', post)[-1] parsed = requests.get(link).text page = BeautifulSoup(parsed, features="html.parser") try: title = page.find('div', class_='bdaia-post-title').find('h1').next except AttributeError: title = page.find('h1', class_='post-title') content = page.find('div', class_='bdaia-post-content') image_url = re.findall( r'(https?://\S+)', page.find('a', class_='bdaia-featured-img-cover')['style'])[-1].replace( ');', '') response = requests.get(image_url, stream=True) with open('img.png', 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) cover = tupload.upload_file('img.png')[0] cover_url = 'http://telegra.ph' + cover text = '<img src=\"{}\"></img>'.format(cover_url) for elem in content: if elem.name == 'div' or elem.name == 'style' or 'script' in str(elem): pass elif elem.name == 'blockquote': if '<a>' in str(elem): quote = '' for p_ in elem: quote += str(p_) twitter_link = elem.find_all('a')[-1]['href'] text += '<p><blockquote>' + quote + '</blockquote></p>' + '<p><a href=\"{}\">Link to tweet</a></p>'.format( twitter_link) else: text += str(elem) elif 'iframe' in str(elem): url = elem.find('iframe')['src'] text += '<p><a href=\"{}\">Link to video</a></p>'.format(url) elif elem.name == 'h2': text += '<p><b>{}</b></p>'.format(elem.next) else: text += str(elem) nodes = tutils.html_to_nodes(text) tg_page = telegraph.create_page(content=nodes, title=title, author_name='@Lor3m', author_url='https://t.me/Lor3m') created_page = tg_page['url'] bot.send_message( -1001347924999, '{}\n\n{}'.format(title, created_page), parse_mode=ParseMode.HTML, )
def image(image_text): img = Image.open('logo.jpg') draw = ImageDraw.Draw(img) original_width = 1100 original_height = 310 left = 50 if width(None, image_text, 'condensed', 100) <= original_width: more_font = 200 while width(None, image_text, 'condensed', more_font) > original_width: more_font -= 1 left += (original_width - width(None, image_text, 'condensed', more_font)) // 2 top = 200 - height_indent(image_text, 'condensed', more_font) top += (original_height - height(None, image_text, 'condensed', more_font)) // 2 draw.text((left, top), image_text, color, fonts('condensed', more_font)) else: layer = 1 drop_text = '' layer_array = [] full_height = 0 temp_text_array = re.sub(r'\s+', ' ', image_text.strip()).split(' ') for i in range(0, len(temp_text_array)): if width(None, temp_text_array[i], 'condensed', 100) <= original_width: if width(None, (drop_text + ' ' + temp_text_array[i]).strip(), 'condensed', 100) <= original_width: drop_text = (drop_text + ' ' + temp_text_array[i]).strip() else: if drop_text != '' and len(layer_array) < 3: layer_array.append(drop_text) full_height += height(None, drop_text, 'condensed', 100) drop_text = '' drop_text = (drop_text + ' ' + temp_text_array[i]).strip() if i == len(temp_text_array) - 1: if drop_text != '' and len(layer_array) < 3: layer_array.append(drop_text) full_height += height(None, drop_text, 'condensed', 100) additional_height = 0 indent_height = int(full_height / len(layer_array) + 0.15 * (full_height / len(layer_array))) mod = int((original_height - len(layer_array) * indent_height) / 2) for i in layer_array: text_position = ( left + (original_width - width(None, i, 'condensed', 100)) // 2, 200 + mod + additional_height) additional_height += indent_height draw.text(text_position, i, color, fonts('condensed', 100)) layer += 1 img.save('bot_edited.jpg') doc = open('bot_edited.jpg', 'rb') uploaded = upload.upload_file(doc) uploaded_link = '<a href="https://telegra.ph' + uploaded[0] + '">āā</a>ļø' doc.close() return uploaded_link
async def settingapic(spic): links = str(spic.pattern_match.group(1)) pics = links.split(" ") what_pic, var_name = await whatpic(spic, 4) await mclient.edit("Setting " + what_pic + "...") if links.replace(" ", "") == '': replied_msg = await spic.get_reply_message() if not replied_msg: return await mclient.edit( "I can't find any proper link whatsoever.", del_in=5) if replied_msg.media: await mclient.edit( "You have replied to a media. Lemme check if it's a pic/video.." ) download = await spic.client.download_media(replied_msg) if replied_msg.file.size < 5.0: try: print("Uploading to telegraph...") telegraph = Telegraph() account = telegraph.create_account( short_name="Maple Plane") url = upload.upload_file(download) pics = f"https://telegra.ph{url[0]}" except exceptions.TelegraphException: await mclient.edit("Couldn't upload it to Telegraph.", sleep=0.5) try: pics = str(open(download).read()).split(" ") except: pics = (replied_msg.text).split(" ") else: await mclient.edit( "Couldn't upload it to Telegraph. Lemme read the caption.") try: pics = str(open(download).read()).split(" ") except: pics = (replied_msg.text).split(" ") #if pics[0] == '': #return await mclient.edit("I can't find any proper link whatsoever.", del_in=5) num_pics = len(pics) num_ltr = "" if num_pics == 1 else "s" num_wd = "has" if num_pics == 1 else "have" await set_a_pic(var_name, pics) await mclient.edit(f"**Your {what_pic}{num_ltr} {num_wd} been set!**", del_in=5) await mclient.botlog(f"The {what_pic}{num_ltr} you have set:\n{pics}")
def create_telegraph_post(list_of_img, title, text): telegraph = Telegraph() telegraph.create_account(short_name='FB_test') links_to_img = upload.upload_file(list_of_img) img_links = '' for link in links_to_img: temp = f"""<img src={link}>""" img_links += temp html_content = f""" {img_links} {text} """ response = telegraph.create_page(title=title, html_content=html_content) return 'https://telegra.ph/{}'.format(response['path'])
def instagram_image(text_array, background_coefficient): background = Image.open('images/logo_instagram' + background_coefficient + '.png') img = Image.new('RGBA', (1080, 1080), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) original_height = 980 original_width = 980 color = (256, 256, 256) layer_array = [] more_font = 200 height_coefficient = False while height_coefficient is False: while len(layer_array) != len(text_array): for t in text_array: array = search_emoji(t) emoji_factor = 0 if array[0]: emoji_factor += int(more_font + more_font * 0.35) * len(array[0]) if width(array[0], array[1], 'regular', more_font) + emoji_factor <= original_width: layer_array.append(array) else: temp_text_array = re.sub(r'\s+', ' ', array[1]).split(' ') array = [array[0]] drop_text = '' for i in range(0, len(temp_text_array)): if width(array[0], (drop_text + ' ' + temp_text_array[i]).strip(), 'regular', more_font) \ + emoji_factor <= original_width: drop_text = (drop_text + ' ' + temp_text_array[i]).strip() else: if drop_text != '' and len(array) < 3: array.append(drop_text) else: break drop_text = temp_text_array[i] if i == len(temp_text_array) - 1: if drop_text != '' and len(array) < 3: array.append(drop_text) layer_array.append(array) else: break if len(layer_array) != len(text_array): more_font -= 1 layer_array.clear() layer_count = 0 additional_height = 0 for i in layer_array: layer_count += len(i) - 1 indent_height = int(more_font + 0.15 * more_font) if layer_count * indent_height <= original_height: mod = 50 + int((original_height - layer_count * indent_height) / 2) pic = mod + int(0.1 * more_font) previous_arrow_array = None for array in layer_array: for i in array: if array.index(i) != 0: left = 0 arrow_split = False family = fonts('regular', more_font) if array[0]: for f in array[0]: if f in ['š»', 'šø', 'š', 'š']: family = fonts('bold', more_font) if f in ['š»', 'š', 'š ', 'š', 'šø', 'š', 'š', 'š¼', 'š']: left += int(more_font + more_font * 0.35) if array.index(i) == 1: foreground = emoji[f].resize((more_font, more_font), Image.ANTIALIAS) background.paste(foreground, (50, pic + additional_height), foreground) if f == 'ā”': arrow_split = True if arrow_split is False: text_position = (50 + left, mod + additional_height) draw.text(text_position, i, color, family) else: text = i arrow_indent = 50 + left arrow_array = i.split('ā”') foreground = emoji['ā”'].resize((more_font, more_font), Image.ANTIALIAS) if len(arrow_array) > 1: text = arrow_array[1] previous_arrow_array = arrow_array text_position = (arrow_indent, mod + additional_height) arrow_indent += width(array[0], arrow_array[0], 'regular', more_font) draw.text(text_position, arrow_array[0], color, family) if text != '': background.paste(foreground, (arrow_indent, pic + additional_height), foreground) if len(arrow_array) == 1 and previous_arrow_array: if previous_arrow_array[0] == '' and previous_arrow_array[1] == '': background.paste(foreground, (arrow_indent, pic + additional_height), foreground) previous_arrow_array = None elif previous_arrow_array[1] == '': background.paste(foreground, (arrow_indent, pic + additional_height), foreground) arrow_indent += int(more_font * 0.35) previous_arrow_array = None else: arrow_indent -= more_font text_position = (arrow_indent + more_font, mod + additional_height) draw.text(text_position, text, color, family) additional_height += indent_height height_coefficient = True else: more_font -= 1 layer_array.clear() background.paste(img, (0, 0), img) background.save('images/bot_edited.png') doc = open('images/bot_edited.png', 'rb') uploaded = upload.upload_file(doc) uploaded_link = '<a href="https://telegra.ph' + uploaded[0] + '">āā</a>ļø' return uploaded_link
def image(image_text, image_text_price, background_coefficient): left = 100 left_price = 60 original_width = 550 original_height = 280 more_font_price = 200 color = (256, 256, 256) original_width_price = 450 original_height_price = 75 img = Image.open('images/logo' + background_coefficient + '.png') draw = ImageDraw.Draw(img) while width(None, image_text_price, 'bold', more_font_price) > original_width_price: more_font_price -= 1 left_price += (original_width_price - width(None, image_text_price, 'bold', more_font_price)) // 2 top = 430 - height_indent(image_text_price, 'bold', more_font_price) top += (original_height_price - height(None, image_text_price, 'bold', more_font_price)) // 2 draw.text((left_price, top), image_text_price, color, fonts('bold', more_font_price)) if width(None, image_text, 'bold', 70) <= original_width: more_font = 200 while width(None, image_text, 'bold', more_font) > original_width: more_font -= 1 left += (original_width - width(None, image_text, 'bold', more_font)) // 2 top = 120 - height_indent(image_text, 'bold', more_font) top += (original_height - height(None, image_text, 'bold', more_font)) // 2 draw.text((left, top), image_text, color, fonts('bold', more_font)) else: layer = 1 drop_text = '' layer_array = [] full_height = 0 temp_text_array = re.sub(r'\s+', ' ', image_text.strip()).split(' ') for i in range(0, len(temp_text_array)): if width(None, temp_text_array[i], 'bold', 70) <= original_width: if width(None, (drop_text + ' ' + temp_text_array[i]).strip(), 'bold', 70) <= original_width: drop_text = (drop_text + ' ' + temp_text_array[i]).strip() else: if drop_text != '' and len(layer_array) < 3: layer_array.append(drop_text) full_height += height(None, drop_text, 'bold', 70) drop_text = '' drop_text = (drop_text + ' ' + temp_text_array[i]).strip() if i == len(temp_text_array) - 1: if drop_text != '' and len(layer_array) < 3: layer_array.append(drop_text) full_height += height(None, drop_text, 'bold', 70) additional_height = 0 if len(layer_array) > 0: indent_height = int(full_height / len(layer_array) + 0.15 * (full_height / len(layer_array))) else: indent_height = int(full_height + 0.15 * full_height) mod = int((original_height - len(layer_array) * indent_height) / 2) for i in layer_array: text_position = (left + (original_width - width(None, i, 'bold', 70)) // 2, 120 + mod + additional_height) additional_height += indent_height draw.text(text_position, i, color, fonts('bold', 70)) layer += 1 img.save('images/bot_edited.png') doc = open('images/bot_edited.png', 'rb') uploaded = upload.upload_file(doc) uploaded_link = '<a href="https://telegra.ph' + uploaded[0] + '">āā</a>ļø' return uploaded_link
def telegram_publish(item): group = [] text = None caption = None if item.get('text'): text = re.sub('<', '<', item.get('text')) text = re.sub('youtu.be/', 'www.youtube.com/watch?v=', text) if item.get('attachments'): if len(item.get('attachments')) > 0: number = 0 for media in item.get('attachments'): number += 1 headers = Headers(headers=True).generate() if media['type'] in ['photo', 'video', 'doc']: if media['type'] == 'photo': file_name = str(number) + '.jpg' sizes = media['photo']['sizes'] photo = sizes[len(sizes) - 1] request = requests.get(photo['url'], headers=headers) with open(file_name, 'wb') as file: file.write(request.content) group.append({'type': 'photo', 'content': open(file_name, 'rb')}) elif media['type'] == 'video': video = media['video'] owner_id = str(video['owner_id']) link = owner_id + '_' + str(video['id']) + '_' + video['access_key'] vk_session = vk_api.VkApi(os.environ['VK_PHONE'], os.environ['VK_PASS']) vk = vk_session.get_api() vk_session.auth() video = vk.video.get(owner_id=owner_id, videos=link)['items'][0] if video.get('platform') in [None, 'YouTube']: if video.get('platform') == 'YouTube': link = re.sub(r'\?.*', '', video['player']) link = re.sub('www.youtube.com/embed', 'youtu.be', link) group.append({'type': 'YouTube', 'content': link}) else: response = requests.get(video['player'], headers=headers) search = list( reversed(re.findall(r'"cache\d+":"(.*?)"', response.text))) if search: file_name = str(number) + '.mp4' request = requests.get(re.sub(r'\\', '', search[0])) with open(file_name, 'wb') as file: file.write(request.content) group.append( {'type': 'video', 'content': open(file_name, 'rb')}) else: file_name = str(number) + '.gif' if media['doc']['ext'] == 'gif': request = requests.get(media['doc']['url'], headers=headers) with open(file_name, 'wb') as file: file.write(request.content) group.append({'type': 'gif', 'content': open(file_name, 'rb')}) if len(group) > 1: media_group = [] for media in group: if media['type'] != 'YouTube': caption = None if media['type'] == 'photo': func = types.InputMediaPhoto else: func = types.InputMediaVideo if len(media_group) == 0 and text: caption = text[:1024] media_group.append(func(media['content'], caption=caption, parse_mode='HTML')) else: video_id = re.sub('https://youtu.be/', '', media['content']) if text.startswith('<a'): if video_id not in text: text += '\nhttps://www.youtube.com/watch?v=' + video_id else: text = '<a href="' + media['content'] + '"> </a>' + text if media_group: bot.send_media_group(idMainChannel, media_group) else: group.clear() if len(group) == 1: media = group[0] file_name = media['content'] if type(media['content']) != str: file_name = media['content'].name if text: if len(text) >= 1024: caption = None try: uploaded = upload.upload_file(open(file_name, 'rb')) text = '<a href="https://telegra.ph' + uploaded[0] + '"></a>' + text[:4090] except IndexError and Exception: caption = text[:1024] else: caption = text[:1024] if media['type'] != 'YouTube': if text and caption is None: group.clear() else: if media['type'] == 'photo': func = bot.send_photo elif media['type'] == 'video': func = bot.send_video else: func = bot.send_document func(idMainChannel, open(file_name, 'rb'), caption=caption, parse_mode='HTML') else: text = objects.html_link(file_name, ' ') + text[:4090] group.clear() if len(group) == 0: bot.send_message(idMainChannel, text[:4096], parse_mode='HTML')
def telegraph_link_media(media): return 'https://telegra.ph/{}'.format(upload_file(media)[0])
def image(): uploaded = upload.upload_file(open('screen.png', 'rb')) return '<a href="https://telegra.ph' + uploaded[0] + '">āā</a>ļø'