Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import discord
  3. import asyncio
  4. import ig_auth
  5. import urllib.request
  6. import os
  7. import sys
  8. import yaml.reader
  9. import re
  10. import textwrap
  11. import requests
  12. import json
  13.  
  14. from instagram_private_api import Client, ClientCompatPatch
  15.  
  16.  
  17.  
  18. # Variables
  19.  
  20. user_name = 'x'
  21. password = 'x'
  22. user_to_check = "milliebobbybrown"
  23. ig_client = ig_auth.login(user_name, password)
  24.  
  25. token = 'x'
  26.  
  27. user_res = ig_client.username_info(user_to_check)
  28. user_id = user_res['user']['pk']
  29.  
  30. posts_path = os.getcwd() + "/posts/"
  31. stories_path = os.getcwd() + "/stories/"
  32.  
  33. new_posts = []
  34. new_stories = []
  35.  
  36. discord_client = discord.Client()
  37. discord_channel_id = '311990116157620225'
  38.  
  39.  
  40. yaml.reader.Reader.NON_PRINTABLE = re.compile(
  41. u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD\U00010000-\U0010FFFF]')
  42.  
  43.  
  44. # Class to hold information for Instagram posts
  45. class media_post:
  46. def __init__(self, **kwargs):
  47. self.post_code = None
  48. self.post_caption = None
  49. self.post_content_images = []
  50. self.post_content_videos = []
  51.  
  52.  
  53. # We don't have a class for Instagram stories as these are added
  54. # to a single list unlike the media for different user posts
  55.  
  56.  
  57.  
  58. # post_embed=discord.Embed(title='Images (Uploading ...)', description="", color=0xff8040)
  59. # images_status = yield from discord_client.send_message(send_channel, embed=post_embed)
  60. # post_embed=discord.Embed(title='Images (Done)', description="", color=0xff8040)
  61. # yield from discord_client.edit_message(images_status, embed=post_embed)
  62.  
  63.  
  64.  
  65.  
  66. @discord_client.event
  67. @asyncio.coroutine
  68. def on_ready():
  69. print('Logged in as')
  70. print(discord_client.user.name)
  71. print(discord_client.user.id)
  72. print('------')
  73. yield from alert_discord()
  74.  
  75. @asyncio.coroutine
  76. def alert_discord():
  77. try:
  78. print("--------------------------------------------------\nNotify myself via PM first ...\n--------------------------------------------------")
  79. post_embed=discord.Embed(title="", description="Millie has posted on Instagram!", color=0xd9534f)
  80. user = yield from discord_client.get_user_info('140030005311700992')
  81. yield from discord_client.start_private_message(user)
  82. yield from discord_client.send_message(user, embed=post_embed)
  83. except Exception as e:
  84. pass
  85.  
  86. print("--------------------------------------------------\nStart Discord media dump ...\n--------------------------------------------------")
  87. send_channel = discord_client.get_channel(discord_channel_id)
  88. post_embed=discord.Embed(title='', description="Millie has posted new content on Instagram!", color=0xff8040)
  89. yield from discord_client.send_message(send_channel, embed=post_embed)
  90.  
  91. is_first_post = True
  92. is_first_story = True
  93. for new_post in new_posts:
  94. for image in new_post.post_content_images:
  95. if is_first_post:
  96. 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)
  97. yield from discord_client.send_message(send_channel, embed=post_embed)
  98. yield from discord_client.send_file(send_channel, posts_path + image.split('/')[-1])
  99. is_first_post = False
  100. else:
  101. post_embed=discord.Embed(title="Image", description="", color=0xff8040)
  102. yield from discord_client.send_message(send_channel, embed=post_embed)
  103. yield from discord_client.send_file(send_channel, posts_path + image.split('/')[-1])
  104. yield from asyncio.sleep(1)
  105. print("[I] Posted " + image.split('/')[-1])
  106.  
  107. for video in new_post.post_content_videos:
  108. file_name = video.split('/')[-1]
  109. yield from discord_client.send_file(send_channel, posts_path + file_name)
  110. yield from asyncio.sleep(1)
  111. print("[I] Posted " + file_name)
  112.  
  113. if len(new_stories) > 0:
  114. for story in new_stories:
  115. if story.endswith(".jpg"):
  116. post_embed=discord.Embed(title="Image", description="From Stories\n\n", color=0xff8040)
  117. yield from discord_client.send_message(send_channel, embed=post_embed)
  118. yield from discord_client.send_file(send_channel, stories_path + story.split('/')[-1])
  119. else:
  120. yield from discord_client.send_file(send_channel, stories_path + story.split('/')[-1])
  121. try:
  122. params = (
  123. ('url', story),
  124. )
  125. r = requests.get('https://api.streamable.com/import', params=params, auth=('raj.vhn@gmail.com', 'dirkving'))
  126. result = json.loads(r.text)
  127. post_embed=discord.Embed(title="Streamable link", description="https://streamable.com/" + result['shortcode'], color=0xff8040)
  128. yield from discord_client.send_message(send_channel, embed=post_embed)
  129. except Exception as e:
  130. pass
  131. yield from asyncio.sleep(1)
  132. print("[I] Posted " + story.split('/')[-1])
  133.  
  134. post_embed=discord.Embed(title="", description="End of dump.", color=0xff8040)
  135. yield from discord_client.send_message(send_channel, embed=post_embed)
  136. yield from asyncio.sleep(1)
  137. print("--------------------------------------------------\nEnding Discord media dump ...\n--------------------------------------------------")
  138. discord_client.logout()
  139. discord_client.close()
  140. exit(0)
  141.  
  142.  
  143.  
  144. # Get last 12 Instagram posts from feed
  145. def get_media_posts(user_id):
  146. print("--------------------------------------------------\nStarting posts retrieval ...\n--------------------------------------------------")
  147. feed_json = ig_client.user_feed(user_id)['items']
  148. feed_yaml_parsed = yaml.load(str(feed_json))
  149. has_new = False
  150.  
  151. for post in feed_yaml_parsed:
  152. post_iterate = media_post()
  153. post_iterate.post_code = post['code']
  154. try:
  155. post_iterate.post_caption = post['caption']['text']
  156. except:
  157. post_iterate.post_caption = "[No caption available]"
  158. # Images
  159. if 'carousel_media' in post:
  160. for media_carousel in post['carousel_media']:
  161. post_iterate.post_content_images.append(media_carousel['image_versions2']['candidates'][0]['url'].split('?')[0])
  162. if 'image_versions2' in post:
  163. post_iterate.post_content_images.append(post['image_versions2']['candidates'][0]['url'].split('?')[0])
  164.  
  165. # Videos
  166. if 'carousel_media' in post:
  167. for media_carousel in post['carousel_media']:
  168. if 'video_versions' in media_carousel:
  169. post_iterate.post_content_videos.append(media_carousel['video_versions'][0]['url'].split('?')[0])
  170. if 'video_versions' in post:
  171. post_iterate.post_content_videos.append(post['video_versions'][0]['url'].split('?')[0])
  172.  
  173. new_posts.append(post_iterate)
  174.  
  175. for new_post in new_posts:
  176. for image in list(new_post.post_content_images):
  177. file_name = image.split('/')[-1]
  178. if not os.path.exists(posts_path + file_name):
  179. has_new = True
  180. print("[I] [I] Downloading post item --- " + file_name + " ...")
  181. urllib.request.urlretrieve(image, posts_path + file_name)
  182. else:
  183. new_post.post_content_images.remove(image)
  184. print("[I] Post item " + file_name + " already exists !")
  185.  
  186. for video in list(new_post.post_content_videos):
  187. file_name = video.split('/')[-1]
  188. if not os.path.exists(posts_path + file_name):
  189. has_new = True
  190. print("[I] Downloading post item --- " + file_name + " ...")
  191. urllib.request.urlretrieve(video, posts_path + file_name)
  192. else:
  193. new_post.post_content_videos.remove(video)
  194. print("[I] Post item " + file_name + " already exists !")
  195. print("--------------------------------------------------\nEnded posts retrieval ...\n--------------------------------------------------")
  196. return has_new
  197.  
  198. def get_media_story(user_id):
  199. print("--------------------------------------------------\nStarting stories retrieval ...\n--------------------------------------------------")
  200. try:
  201. feed = ig_client.user_story_feed(user_id)
  202. feed_json = feed['reel']['items']
  203. for media in feed_json:
  204. if 'video_versions' in media:
  205. new_stories.append(media['video_versions'][0]['url'].split('?')[0])
  206. else:
  207. new_stories.append(media['image_versions2']['candidates'][0]['url'].split('?')[0])
  208.  
  209. for media_item in list(new_stories):
  210. file_name = media_item.split('/')[-1]
  211. if not os.path.exists(stories_path + file_name):
  212. print("[I] Downloading story item --- " + file_name + " ...")
  213. urllib.request.urlretrieve(media_item, stories_path + file_name)
  214. else:
  215. new_stories.remove(media_item)
  216. print("[I] Story item" + file_name + " already exists !")
  217.  
  218. print("--------------------------------------------------\nEnding stories retrieval ...\n--------------------------------------------------")
  219. if len(new_stories) > 0:
  220. return True
  221. else:
  222. return False
  223. except TypeError as e:
  224. print("[I] No new stories are available.")
  225. print("--------------------------------------------------\nEnding stories retrieval ...\n--------------------------------------------------")
  226. return False
  227.  
  228.  
  229. def main():
  230. if get_media_posts(user_id) or get_media_story(user_id):
  231. discord_client.run(token)
  232. else:
  233. discord_client.close()
  234. exit(0)
  235.  
  236. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement