|
| 1 | +from enum import Enum |
| 2 | +from my_feed.modules.post import PostModel |
| 3 | +from my_feed.modules.types import PostType |
| 4 | +from my_feed.platforms import PlatformInterface, PlatformsId |
| 5 | +from my_feed.platforms.instagram.credentials import credentials_manager |
| 6 | +from my_feed.platforms.instagram.login import InstagramLogin |
| 7 | + |
| 8 | + |
| 9 | +class MediaType(Enum): |
| 10 | + video = 'video' |
| 11 | + image = 'image' |
| 12 | + |
| 13 | + |
| 14 | +class Instagram(PlatformInterface): |
| 15 | + |
| 16 | + def __init__(self): |
| 17 | + super().__init__() |
| 18 | + |
| 19 | + self.api: InstagramLogin |
| 20 | + |
| 21 | + def __repr__(self): |
| 22 | + return PlatformsId.INSTAGRAM.value |
| 23 | + |
| 24 | + def _login(self): |
| 25 | + user = credentials_manager.credentials[0] |
| 26 | + il = InstagramLogin() |
| 27 | + self.api = il.login(user.username, user.password, 'configs/{}_creds.json'.format(user.username)) |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def get_media_url(media_data): |
| 31 | + media = media_data.get('standard_resolution', {}) |
| 32 | + return media.get('url') |
| 33 | + |
| 34 | + def story(self, user_id, last_update_id) -> [PostModel]: |
| 35 | + """ |
| 36 | + story can be get only if you are logged in |
| 37 | + Load all the stories from the user loaded |
| 38 | + :return: InstagramStory list |
| 39 | + """ |
| 40 | + out = [] # list of all the items in the story |
| 41 | + |
| 42 | + stories = self.api.user_story_feed(user_id) |
| 43 | + |
| 44 | + reels = stories.get('reel') |
| 45 | + if not reels: |
| 46 | + return [] |
| 47 | + |
| 48 | + reels = reels.get('items') |
| 49 | + for reel in reels: |
| 50 | + |
| 51 | + post_id = reel.get('pk') |
| 52 | + if post_id == last_update_id: |
| 53 | + break |
| 54 | + |
| 55 | + post = PostModel( |
| 56 | + post_id=post_id, |
| 57 | + title='', |
| 58 | + created_at=reel.get('taken_at'), |
| 59 | + url=reel.get('link') |
| 60 | + ) |
| 61 | + |
| 62 | + media_type = MediaType(reel.get('type')) |
| 63 | + post.type = PostType.VIDEO if media_type == MediaType.video else PostType.IMAGE |
| 64 | + media = reel.get('videos' if media_type == MediaType.video else 'images') |
| 65 | + post.add_media(media_id=None, media_url=self.get_media_url(media)) |
| 66 | + |
| 67 | + out.append(post) |
| 68 | + |
| 69 | + return out |
| 70 | + |
| 71 | + def post(self, user_id, last_update_id) -> [PostModel]: |
| 72 | + """ |
| 73 | + Get all the post from the user loaded |
| 74 | + Store them as a list of InstagramPost obj |
| 75 | + :return: PostModel list |
| 76 | + """ |
| 77 | + out = [] |
| 78 | + |
| 79 | + media = self.api.user_feed(user_id) |
| 80 | + |
| 81 | + items = media.get('items') |
| 82 | + for item in items: |
| 83 | + |
| 84 | + post_id = item.get('code') |
| 85 | + if post_id == last_update_id: |
| 86 | + break |
| 87 | + |
| 88 | + # get description of the post |
| 89 | + caption = item.get('caption', {}) |
| 90 | + text = '' |
| 91 | + if caption: |
| 92 | + text = caption.get('text', '') |
| 93 | + |
| 94 | + post = PostModel( |
| 95 | + post_id=post_id, |
| 96 | + title=text, |
| 97 | + created_at=item.get('created_time'), |
| 98 | + url=item.get('link') |
| 99 | + ) |
| 100 | + |
| 101 | + """ |
| 102 | + Check if is a post with multiple elements |
| 103 | + - multi element post has media list inside carousel_media |
| 104 | + - if not the object media is no encapsulated |
| 105 | + """ |
| 106 | + carousel = item.get('carousel_media') |
| 107 | + media_type = MediaType(item.get('type')) |
| 108 | + post.type = PostType.VIDEO if media_type == MediaType.video else PostType.IMAGE |
| 109 | + if carousel: |
| 110 | + for c in carousel: |
| 111 | + media = c.get('videos' if media_type == MediaType.video else 'images') |
| 112 | + post.add_media(media_id=None, media_url=self.get_media_url(media)) |
| 113 | + |
| 114 | + else: |
| 115 | + media = item.get('videos' if media_type == MediaType.video else 'images') |
| 116 | + post.add_media(media_id=None, media_url=self.get_media_url(media)) |
| 117 | + |
| 118 | + out.append(post) |
| 119 | + |
| 120 | + return out |
| 121 | + |
| 122 | + def update(self, target, last_update_id): |
| 123 | + |
| 124 | + # decouple the last update id |
| 125 | + # this cause instagram have 2 feed sources, stories and posts |
| 126 | + last_update_id_story = None |
| 127 | + last_update_id_post = None |
| 128 | + if last_update_id: |
| 129 | + last_update_id_story, last_update_id_post = last_update_id |
| 130 | + |
| 131 | + # do login every time |
| 132 | + self._login() |
| 133 | + |
| 134 | + # get the data from the api |
| 135 | + story_feed = self.story(target, last_update_id_story) |
| 136 | + post_feed = self.post(target, last_update_id_post) |
| 137 | + |
| 138 | + # get the post id from the feed |
| 139 | + last_update_id_story = self._get_last_post_id(story_feed, last_update_id_story) |
| 140 | + last_update_id_post = self._get_last_post_id(post_feed, last_update_id_post) |
| 141 | + |
| 142 | + # the set feed will also revert it |
| 143 | + self._set_feed(post_feed + story_feed) |
| 144 | + |
| 145 | + # update the post id |
| 146 | + self._last_post_id = (last_update_id_story, last_update_id_post) |
| 147 | + return self._feed |
0 commit comments