예제 #1
0
def hope(bot, update):
    global likes_no
    global post_links
    global likes
    a = update.message.text
    username = a[1:a.find(",")]
    range = int(a[a.find(",") + 1:])
    req = requests.get("{}{}".format(instagram_link, username))
    if '200' not in str(req.status_code):
        bot.send_message(chat_id=update.message.chat_id, text="There is no Username with @{} on Instagram".format(username))
    elif "is_private\":true" in str(req.content):
        bot.send_message(chat_id=update.message.chat_id, text="{} is private! @{}".format(msg['text'][1:], msg["from"]["username"]))
    elif not(range <= 9 and range >= 0):
        bot.send_message(chat_id=update.message.chat_id, text="Please use Numbers between 1 to 9")
    else:
        bot.send_message(chat_id=update.message.chat_id, text="Recevied @{} from @{}".format(username, update.message.from_user["username"]))
        API = InstagramAPI(USERNAME, PASSWORD)
        API.login()

        API.searchUsername(username)
        username_id = API.LastJson["user"]["pk"]
        l = API.getTotalUserFeed(username_id)

        ii = 0
        while ii < len(l):
            likes_no.update({str(l[ii]["like_count"]): int(ii)})
            likes.append(l[int(ii)]["like_count"])
            ii = ii + 1

        tt = 0
        while tt < range:
            max_like = max(likes)
            max_liked_no = likes_no[str(max_like)]
            post_links.update({"https://www.instagram.com/p/{}".format((l[max_liked_no])["code"]): max_like})
            likes.remove(max_like)
            tt = tt + 1

        for b in post_links:
            like = post_links[b]
            bot.sendMessage(chat_id=update.message.chat_id, text="Post Link: {}\nLikes: {}".format(b, like), disable_web_page_preview=True)

        API.logout()
        likes_no.clear()
        post_links.clear()
        likes.clear()
        print("Logout success!")
예제 #2
0
def top_post(bot, update, args):
    likes_no = {}
    post_links = {}
    likes = []

    # Google sheets for Storing the Chat ID of every new Telegram user who press start
    scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
    credentials = ServiceAccountCredentials.from_json_keyfile_name("toppostbot.json", scope)
    gc = gspread.authorize(credentials)
    wks = gc.open("test2").worksheet("Subscribed Users")

    chat_id = update.message.chat_id
    a = args

    if str(chat_id) in wks.col_values(1) or int(chat_id) in admins and len(args) == 2:
        username = (a[0].replace(',','')).replace('@',"")
        range = int(a[1].replace(',',''))
        print("{}{}".format(instagram_link, username))
        req = requests.get("{}{}".format(instagram_link, username))
        if '200' not in str(req.status_code):
            bot.send_message(chat_id=update.message.chat_id, text="There is no Username with @{} on Instagram".format(username))
        elif "is_private\":true" in str(req.content):
            bot.send_message(chat_id=update.message.chat_id, text="{} is private! @{}".format(username, update.message.from_user["username"]))
        elif not(range <= 25 and range >= 1):
            bot.send_message(chat_id=update.message.chat_id, text="Please use Numbers between 1 to 25")
        else:
            bot.send_message(chat_id=update.message.chat_id, text="Recevied @{} from @{}".format(username, update.message.from_user["username"]))
            API = InstagramAPI(USERNAME, PASSWORD)
            API.login()

            API.searchUsername(username)
            username_id = API.LastJson["user"]["pk"]
            l = API.getTotalUserFeed(username_id)

            ii = 0
            while ii < len(l):
                likes_no.update({str(l[ii]["like_count"]): int(ii)})
                likes.append(l[int(ii)]["like_count"])
                ii = ii + 1

            tt = 0
            while tt < range:
                max_like = max(likes)
                max_liked_no = likes_no[str(max_like)]
                post_links.update({"https://www.instagram.com/p/{}".format((l[max_liked_no])["code"]): max_like})
                likes.remove(max_like)
                tt = tt + 1

            for b in post_links:
                like = post_links[b]
                bot.send_message(chat_id=update.message.chat_id, text="Post Link: {}\nLikes: {}".format(b, like), disable_web_page_preview=True)

            API.logout()
            likes_no.clear()
            post_links.clear()
            likes.clear()
            print("Logout success!")
    elif str(chat_id) in wks.col_values(1) or int(chat_id) in admins and len(args) != 2:
        update.message.reply_text("Your format seems to be wrong use /help to know the right format")
    elif str(chat_id) not in wks.col_values(1) or int(chat_id) not in str(admins):
        update.message.reply_text("Use this bot to get the Top Posts by an Instagram user!!\nContact @mentalgain for more...")
예제 #3
0
def date_insta_top_posts(bot, update, args):
    likes_no = []
    post_links = []
    likes = []
    timestamps = [] #np.array([])
    level = {}

    # Google sheets for Storing the Chat ID of every new Telegram user who press start
    scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
    credentials = ServiceAccountCredentials.from_json_keyfile_name("toppostbot.json", scope)
    gc = gspread.authorize(credentials)
    wks = gc.open("test2").worksheet("Subscribed Users")

    chat_id = update.message.chat_id
    a = args

    if str(chat_id) in wks.col_values(1) or int(chat_id) in admins and len(a) == 4:
        username = (a[0].replace(',','')).replace('@',"")
        range = int(a[1].replace(',',''))
        date1 = (str(a[2]).replace(',','')).replace(" ","")
        date2 = (str(a[3]).replace(',','')).replace(" ","")
        A = int(time.mktime(datetime.datetime.strptime(date1, "%d/%m/%Y").timetuple()))
        B = int(time.mktime(datetime.datetime.strptime(date2, "%d/%m/%Y").timetuple()))
        # primary == smallest
        # secondary == biggest
        print("{}{}".format(instagram_link, username))
        req = requests.get("{}{}".format(instagram_link, username))
        # if "{} {}".format(a[2],a[3]).count("/") == 4:
        if date_validation(date1) == False:
            update.message.reply_text(text="Your First date isn't in Right Format or is not a Valid Date\nDate Format: `DD/MM/YYYY`", parse_mode='MARKDOWN')
        elif date_validation(date2) == False:
            update.message.reply_text(text="Your second date isn't in Right Format or is not a Valid Date\nDate Format: `DD/MM/YYYY`", parse_mode='MARKDOWN')
        elif A == B:
            update.message.reply_text(text="Both of your date are same.\nPlease put two different date")
        elif "{} {}".format(a[2],a[3]).count("/") != 4:
            update.message.reply_text(text="Your date format is wrong\nDate Format: `DD/MM/YYYY`", parse_mode='MARKDOWN')
        elif '200' not in str(req.status_code):
            bot.send_message(chat_id=update.message.chat_id, text="There is no Username with @{} on Instagram".format(username))
        elif "is_private\":true" in str(req.content):
            bot.send_message(chat_id=update.message.chat_id, text="{} is private! @{}".format(a, update.message.from_user["username"]))
        elif not(range <= 25 and range >= 1):
            bot.send_message(chat_id=update.message.chat_id, text="Please use Numbers between 1 to 9")
        else:
            if A > B:
                primary = B
                secondary = A
            elif A < B:
                primary = A
                secondary = B
            bot.send_message(chat_id=update.message.chat_id, text="Recevied @{} from @{}".format(username, update.message.from_user["username"]))
            API = InstagramAPI(USERNAME, PASSWORD)
            API.login()

            API.searchUsername(username)
            username_id = API.LastJson["user"]["pk"]
            l = API.getTotalUserFeed(username_id)

            ii = 0
            while ii < len(l):
                # np.append(timestamps, l[ii]["taken_at"])
                timestamps.append(l[ii]["taken_at"])
                ii = ii + 1

            secondy = timestamps.index((min(timestamps, key=lambda x:abs(x-primary))))
            first = timestamps.index((min(timestamps, key=lambda x:abs(x-secondary))))

            tt = int(first)
            while tt < secondy:
                # likes_no.update({str(l[tt]["like_count"]): int(tt)})
                likes_no.append([str(l[tt]["like_count"]), int(tt)])
                # print(str(l[tt]["like_count"]))
                likes.append(l[int(tt)]["like_count"])
                
                tt = tt + 1
            print(likes)

            bot.send_message(chat_id=update.message.chat_id, text="From {} to {} there are only {} posts out of {} posts".format(date1, date2, len(likes), len(timestamps)))

            if range > len(likes):
                range = len(likes)

            ss = 0
            while ss < range:
                max_like = str(max(likes))
                maxed_like = likes.index(int(max_like))
                max_liked_no = likes_no[int(maxed_like)][1]
                post_links.append(["https://www.instagram.com/p/{}".format((l[max_liked_no])["code"]), max_like])
                likes.pop(maxed_like)
                likes_no.pop(maxed_like)
                ss = ss + 1

            for b in post_links:
                like = b[1]
                post_linked = b[0]
                bot.sendMessage(chat_id=update.message.chat_id, text="Post Link: {}\nLikes: {}".format(post_linked, like), disable_web_page_preview=True)
                # print("Post Link: {}\nLikes: {}".format(b[0], like))

            API.logout()
            likes_no.clear
            post_links.clear
            likes.clear
            timestamps.clear #np.array([])
            level.clear
            print("Logout success!")
    elif str(chat_id) in wks.col_values(1) or int(chat_id) in admins and len(a) != 4:
        update.message.reply_text("Your format seems to be wrong use /help to know the right format")
    elif str(chat_id) not in wks.col_values(1) or int(chat_id) not in admins:
        update.message.reply_text("Use this bot to get the Top Posts by an Instagram user!!\nContact @mentalgain for more...")