Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import discord
- import asyncio
- import ig_auth
- import urllib.request
- import os
- import sys
- import yaml.reader
- import re
- import textwrap
- import requests
- import json
- from instagram_private_api import Client, ClientCompatPatch
- # Variables
- user_name = 'x'
- password = 'x'
- user_to_check = "milliebobbybrown"
- ig_client = ig_auth.login(user_name, password)
- token = 'x'
- user_res = ig_client.username_info(user_to_check)
- user_id = user_res['user']['pk']
- posts_path = os.getcwd() + "/posts/"
- stories_path = os.getcwd() + "/stories/"
- new_posts = []
- new_stories = []
- discord_client = discord.Client()
- discord_channel_id = '311990116157620225'
- yaml.reader.Reader.NON_PRINTABLE = re.compile(
- u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]')
- # Class to hold information for Instagram posts
- class media_post:
- def __init__(self, **kwargs):
- self.post_code = None
- self.post_caption = None
- self.post_content_images = []
- self.post_content_videos = []
- # We don't have a class for Instagram stories as these are added
- # to a single list unlike the media for different user posts
- # post_embed=discord.Embed(title='Images (Uploading ...)', description="", color=0xff8040)
- # images_status = yield from discord_client.send_message(send_channel, embed=post_embed)
- # post_embed=discord.Embed(title='Images (Done)', description="", color=0xff8040)
- # yield from discord_client.edit_message(images_status, embed=post_embed)
- @discord_client.event
- @asyncio.coroutine
- def on_ready():
- print('Logged in as')
- print(discord_client.user.name)
- print(discord_client.user.id)
- print('------')
- yield from alert_discord()
- @asyncio.coroutine
- def alert_discord():
- try:
- print("--------------------------------------------------\nNotify myself via PM first ...\n--------------------------------------------------")
- post_embed=discord.Embed(title="", description="Millie has posted on Instagram!", color=0xd9534f)
- user = yield from discord_client.get_user_info('140030005311700992')
- yield from discord_client.start_private_message(user)
- yield from discord_client.send_message(user, embed=post_embed)
- except Exception as e:
- pass
- print("--------------------------------------------------\nStart Discord media dump ...\n--------------------------------------------------")
- send_channel = discord_client.get_channel(discord_channel_id)
- post_embed=discord.Embed(title='', description="Millie has posted new content on Instagram!", color=0xff8040)
- yield from discord_client.send_message(send_channel, embed=post_embed)
- is_first_post = True
- is_first_story = True
- for new_post in new_posts:
- for image in new_post.post_content_images:
- if is_first_post:
- post_embed=discord.Embed(title="Image", description="[View on Instagram](https://instagram.com/p/{})\n\n".format(new_post.post_code) + new_post.post_caption + "\n", color=0xff8040)
- yield from discord_client.send_message(send_channel, embed=post_embed)
- yield from discord_client.send_file(send_channel, posts_path + image.split('/')[-1])
- is_first_post = False
- else:
- post_embed=discord.Embed(title="Image", description="", color=0xff8040)
- yield from discord_client.send_message(send_channel, embed=post_embed)
- yield from discord_client.send_file(send_channel, posts_path + image.split('/')[-1])
- yield from asyncio.sleep(1)
- print("[I] Posted " + image.split('/')[-1])
- for video in new_post.post_content_videos:
- file_name = video.split('/')[-1]
- yield from discord_client.send_file(send_channel, posts_path + file_name)
- yield from asyncio.sleep(1)
- print("[I] Posted " + file_name)
- if len(new_stories) > 0:
- for story in new_stories:
- if story.endswith(".jpg"):
- post_embed=discord.Embed(title="Image", description="From Stories\n\n", color=0xff8040)
- yield from discord_client.send_message(send_channel, embed=post_embed)
- yield from discord_client.send_file(send_channel, stories_path + story.split('/')[-1])
- else:
- yield from discord_client.send_file(send_channel, stories_path + story.split('/')[-1])
- try:
- params = (
- ('url', story),
- )
- r = requests.get('https://api.streamable.com/import', params=params, auth=('raj.vhn@gmail.com', 'dirkving'))
- result = json.loads(r.text)
- post_embed=discord.Embed(title="Streamable link", description="https://streamable.com/" + result['shortcode'], color=0xff8040)
- yield from discord_client.send_message(send_channel, embed=post_embed)
- except Exception as e:
- pass
- yield from asyncio.sleep(1)
- print("[I] Posted " + story.split('/')[-1])
- post_embed=discord.Embed(title="", description="End of dump.", color=0xff8040)
- yield from discord_client.send_message(send_channel, embed=post_embed)
- yield from asyncio.sleep(1)
- print("--------------------------------------------------\nEnding Discord media dump ...\n--------------------------------------------------")
- discord_client.logout()
- discord_client.close()
- exit(0)
- # Get last 12 Instagram posts from feed
- def get_media_posts(user_id):
- print("--------------------------------------------------\nStarting posts retrieval ...\n--------------------------------------------------")
- feed_json = ig_client.user_feed(user_id)['items']
- feed_yaml_parsed = yaml.load(str(feed_json))
- has_new = False
- for post in feed_yaml_parsed:
- post_iterate = media_post()
- post_iterate.post_code = post['code']
- try:
- post_iterate.post_caption = post['caption']['text']
- except:
- post_iterate.post_caption = "[No caption available]"
- # Images
- if 'carousel_media' in post:
- for media_carousel in post['carousel_media']:
- post_iterate.post_content_images.append(media_carousel['image_versions2']['candidates'][0]['url'].split('?')[0])
- if 'image_versions2' in post:
- post_iterate.post_content_images.append(post['image_versions2']['candidates'][0]['url'].split('?')[0])
- # Videos
- if 'carousel_media' in post:
- for media_carousel in post['carousel_media']:
- if 'video_versions' in media_carousel:
- post_iterate.post_content_videos.append(media_carousel['video_versions'][0]['url'].split('?')[0])
- if 'video_versions' in post:
- post_iterate.post_content_videos.append(post['video_versions'][0]['url'].split('?')[0])
- new_posts.append(post_iterate)
- for new_post in new_posts:
- for image in list(new_post.post_content_images):
- file_name = image.split('/')[-1]
- if not os.path.exists(posts_path + file_name):
- has_new = True
- print("[I] [I] Downloading post item --- " + file_name + " ...")
- urllib.request.urlretrieve(image, posts_path + file_name)
- else:
- new_post.post_content_images.remove(image)
- print("[I] Post item " + file_name + " already exists !")
- for video in list(new_post.post_content_videos):
- file_name = video.split('/')[-1]
- if not os.path.exists(posts_path + file_name):
- has_new = True
- print("[I] Downloading post item --- " + file_name + " ...")
- urllib.request.urlretrieve(video, posts_path + file_name)
- else:
- new_post.post_content_videos.remove(video)
- print("[I] Post item " + file_name + " already exists !")
- print("--------------------------------------------------\nEnded posts retrieval ...\n--------------------------------------------------")
- return has_new
- def get_media_story(user_id):
- print("--------------------------------------------------\nStarting stories retrieval ...\n--------------------------------------------------")
- try:
- feed = ig_client.user_story_feed(user_id)
- feed_json = feed['reel']['items']
- for media in feed_json:
- if 'video_versions' in media:
- new_stories.append(media['video_versions'][0]['url'].split('?')[0])
- else:
- new_stories.append(media['image_versions2']['candidates'][0]['url'].split('?')[0])
- for media_item in list(new_stories):
- file_name = media_item.split('/')[-1]
- if not os.path.exists(stories_path + file_name):
- print("[I] Downloading story item --- " + file_name + " ...")
- urllib.request.urlretrieve(media_item, stories_path + file_name)
- else:
- new_stories.remove(media_item)
- print("[I] Story item" + file_name + " already exists !")
- print("--------------------------------------------------\nEnding stories retrieval ...\n--------------------------------------------------")
- if len(new_stories) > 0:
- return True
- else:
- return False
- except TypeError as e:
- print("[I] No new stories are available.")
- print("--------------------------------------------------\nEnding stories retrieval ...\n--------------------------------------------------")
- return False
- def main():
- if get_media_posts(user_id) or get_media_story(user_id):
- discord_client.run(token)
- else:
- discord_client.close()
- exit(0)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement