def get_acess_token(user_id, ig_code): """generate acess token from ig code""" files = { 'client_id': (None, '425368628431983'), 'client_secret': (None, '9c18abaf114db46610e7855a642dd6c9'), 'grant_type': (None, 'authorization_code'), 'redirect_uri': (None, 'https://0.0.0.0:5000/register'), 'code': (None, ig_code), } r = requests.post('https://api.instagram.com/oauth/access_token', files=files).json() short_access_token = r["access_token"] res = requests.get( "https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret=9c18abaf114db46610e7855a642dd6c9&access_token=" + short_access_token).json() long_access_token = res["access_token"] my_user = storage.get(User, user_id) my_user.update_attr("ig_access_token", long_access_token) URL = "https://graph.instagram.com/me/media?fields=id,caption,media_type,media_url,timestamp&access_token=" + long_access_token r_media = requests.get(URL) medias = r_media.json() post_dict = {} post_list = [] index = 0 for posts in medias["data"]: if index == 10: break new_post = {} new_post["ig_post_id"] = posts["id"] new_post["Post_id_CrossMe"] = str(uuid.uuid4()) if "caption" in posts.keys(): new_post["message"] = posts["caption"] else: new_post["message"] = "NULL" new_post["created_time"] = datetime.strptime(posts["timestamp"], '%Y-%m-%dT%H:%M:%S+%f') new_post["type"] = posts["media_type"] if posts["media_type"] != "CAROUSEL_ALBUM": new_post["image_url"] = posts["media_url"] else: continue new_post["image_url"] = [] URLCASS = "https://graph.instagram.com/" + new_post["ig_post_id"] + "?fields=children&access_token=" + long_access_token r_cass = requests.get(URLCASS) cass = r_cass.json() data_cass = cass["children"]["data"] for inside_data in data_cass: inside_id = inside_data["id"] URLINSIDE = "https://graph.instagram.com/" + inside_id + "?fields=media_url&access_token=" + long_access_token r_inside = requests.get(URLINSIDE) inside = r_inside.json() new_post["image_url"].append(inside["media_url"]) new_post["CrossMe_user_id"] = user_id new_post["source"] = "Instagram" post_list.append(new_post) index = index + 1 my_post = Post() my_post.user_id = new_post["CrossMe_user_id"] my_post.creation_date = new_post["created_time"] my_post.post_source = new_post["source"] my_post.post_type = posts["media_type"] my_post.post_text = new_post["message"] my_post.media_url = new_post["image_url"] my_post.save() post_dict["fb_last_post"] = post_list return make_response(jsonify(post_dict), 200)
def get_data_fb(user_id, access_token): """scrap data from facebook""" my_user = storage.get(User, user_id) my_user.update_attr("fb_access_token", access_token) r = requests.get('https://graph.facebook.com/me/feed?access_token=' + access_token) result = r.json() post_dict = {} post_list = [] index = 0 for posts in result["data"]: if index == 10: break new_post = {} new_post["CrossMe_user_id"] = user_id new_post["Post_id_CrossMe"] = str(uuid.uuid4()) if "message" in posts.keys(): new_post["message"] = posts["message"] else: new_post["message"] = "NULL" new_post["created_time"] = datetime.strptime(posts["created_time"], '%Y-%m-%dT%H:%M:%S+%f') new_post["source"] = "FACEBOOK" new_post["fb_post_id"] = posts["id"] URLPOST = 'https://graph.facebook.com/' + posts["id"] + '?fields=object_id&access_token=' + access_token post_data = requests.get(URLPOST).json() if "object_id" in post_data.keys(): URLIMAGE = 'https://graph.facebook.com/' + post_data["object_id"] + '?fields=images&access_token=' + access_token image_data = requests.get(URLIMAGE).json() if "images" not in image_data.keys(): continue all_images = image_data["images"] new_post["image_url"] = all_images[1]["source"] posts["media_type"] = "IMAGE" else: continue posts["media_type"] = "STATUS" new_post["image_url"] = "NULL" post_list.append(new_post) index = index + 1 my_post = Post() my_post.user_id = new_post["CrossMe_user_id"] my_post.creation_date = new_post["created_time"] my_post.post_source = new_post["source"] my_post.post_type = posts["media_type"] my_post.post_text = new_post["message"] my_post.media_url = new_post["image_url"] my_post.save() post_dict["fb_last_post"] = post_list return make_response(jsonify(post_dict), 200)
def make_new_post(user_id): """ make a new post Return: if not valid user_id or no user found: error code: 8 if not valid file format: error code 6 if success: return success code 200 else unknown error (code : 0) """ if request.method == 'POST': response_dict = {} response_dict["error"] = "invalid parameter" response_dict["usage"] = "/post/new" response_dict["error_code"] = "8" try: uuid_obj = UUID(user_id, version=4) except ValueError: return make_response(jsonify(response_dict), 202) user = storage.get(User, user_id) if not user: return make_response(jsonify(response_dict), 202) if not request.files and not request.form: invalid_dict = {} invalid_dict["error"] = "invalid request" invalid_dict["usage"] = "/post/<user_id>/new" invalid_dict["required_methods"] = "POST" invalid_dict["error_code"] = "7" return make_response(jsonify(invalid_dict), 202) else: new_post = Post() if request.form: from_data = request.form.to_dict() new_post.post_text = from_data["data"] else: new_post.post_text = "NULL" if request.files: new_post.post_type = "IMAGE" image = request.files['file'] if "." in image.filename: ext = image.filename.split(".")[-1] if ext.upper( ) not in app.config["ALLOWED_IMAGE_EXTENSIONS"]: invalid_dict = {} invalid_dict["error"] = "invalid file format" invalid_dict["usage"] = "/post/<user_id>/new" invalid_dict["required_methods"] = "POST" invalid_dict["error_code"] = "6" return make_response(jsonify(invalid_dict), 202) else: invalid_dict = {} invalid_dict["error"] = "invalid file format" invalid_dict["usage"] = "/post/<user_id>/new" invalid_dict["required_methods"] = "POST" invalid_dict["error_code"] = "6" return make_response(jsonify(invalid_dict), 202) new_filename = 'cm_' + str(new_post.id) + '.' + ext final_filename = secure_filename(new_filename) image.save( os.path.join(app.config["IMAGE_UPLOADS"], final_filename)) final_path = '/static/images/' + final_filename new_post.media_url = final_path new_post.user_id = user_id new_post.creation_date = datetime.utcnow() new_post.post_source = "CROSSME" new_post.save() storage.reload() my_user = user.to_dict() my_post = new_post.to_dict() my_post["user_full_name"] = my_user["full_name"] my_post["user_avatar"] = my_user["user_avatar"] if not my_post["post_text"] or my_post["post_text"] == "NULL": my_post["post_text"] = "" if "media_url" not in my_post.keys(): my_post["media_url"] = "nomedia" storage.reload() return make_response(jsonify(my_post), 200) unknown_dict = {} unknown_dict["error"] = "unknown error" unknown_dict["error_code"] = "0" return make_response(jsonify(unknown_dict), 404)