Flask instagram followers users compare

The program use instagrapi library to compare users followers from two instagram profile do they follow each others.Writen in Flask.
mail@pastecode.io avatar
unknown
python
3 years ago
8.3 kB
4
Indexable
Never
from flask import Flask, render_template, request, redirect, url_for, session, send_file
from instagrapi import Client
import time
import csv
import os

ACCOUNT_USERNAME = ""
ACCOUNT_PASSWORD = ''
global count
count = 0
users_list_compare = list()
following_list = list()

app = Flask(__name__)
cl = Client()

@app.route('/', methods=['GET', 'POST'])
def home():
	error = None
	if request.method == 'POST':
		if request.form['username'] != ACCOUNT_USERNAME or request.form['pass'] != ACCOUNT_PASSWORD:
			error = 'Invalid Credentials. Please try again.'
		else:
			cl.login(ACCOUNT_USERNAME, ACCOUNT_PASSWORD)
			cl.request_timeout = 12
			return redirect(url_for('mainpage'))

	return render_template('index.html', error=error)


@app.route('/home', methods=['GET', 'POST'])
def mainpage():
	users_list_main_followers = list()
	users_list_main_following = list()
	users_list_grand_followers = list()
	users_list_b_following = list()
	users_list_a_following = list()
	users_a_list = list()
	users_b_list = list()
	users_b_only = list()
	userr_id_compare = ''
	if(request.method == 'POST'):
		if request.form['user_compare'] is not None:
			value_check_box = request.form.getlist('check')
			print(len(value_check_box))
			if len(value_check_box) == 0:
				pass
			else:
				if value_check_box[0] == 'followers' and request.form['user_compare'] is not None and request.form['submitt'] == 'Scrape List A' :
					userr_id_compare = cl.user_id_from_username(request.form['user_compare'])		
					user_followers = cl.user_followers(userr_id_compare)
					user_keys_lista = user_followers.keys()

					for each in user_keys_lista:
						users_list_main_followers.append(user_followers[each].username)

					zlist_followers = zip(users_list_main_followers)

					with open('outputs/list_a.csv', 'w') as file:
						writer = csv.writer(file)
						writer.writerows(zlist_followers)
						print('file writed')
					return send_file('outputs/list_a.csv',
						             mimetype='text/csv',
						             attachment_filename='list_a.csv',
						             as_attachment=True)

				elif value_check_box[0] == 'following' and request.form['user_compare'] is not None and request.form['mainUsername'] is None and request.form['submitt'] == 'Scrape List A':
					userr_id_compare = cl.user_id_from_username(request.form['user_compare'])		
					user_following = cl.user_following(userr_id_compare)
					user_keys_following = user_following.keys()

					for each in user_keys_following:
						users_list_a_following.append(user_following[each].username)

					zlist_b_following = zip(users_list_a_following)
					
					with open('outputs/list_a.csv', 'w') as file:
						writer = csv.writer(file)
						writer.writerows(zlist_b_following)
					return send_file('outputs/list_a.csv',
							             mimetype='text/csv',
							             attachment_filename='list_a.csv',
							             as_attachment=True)	

			
			if request.form['mainUsername'] is not None and request.form['submitt'] == 'Scrape list B':
				userr_id_main = cl.user_id_from_username(request.form['mainUsername'])
				user_followers = cl.user_followers(userr_id_main)
				user_keys_main = user_followers.keys()
				for each in user_keys_main:
					users_list_grand_followers.append(user_followers[each].username)
				print(users_list_grand_followers)

				zlist_main_zip = zip(users_list_grand_followers)
				with open('outputs/list_b_followers.csv', 'w') as file:
					writer = csv.writer(file)
					writer.writerows(zlist_main_zip)

				return send_file('outputs/list_b_followers.csv',
								mimetype='text/csv',
								attachment_filename='list_b_followers.csv',
								as_attachment=True)

			if request.form['submitt'] == 'Compare':
				if value_check_box[0] == 'followers':
					userr_id_compare = cl.user_id_from_username(request.form['user_compare'])		
					user_followers = cl.user_followers(userr_id_compare)
					user_keys_lista = user_followers.keys()
					for each in user_keys_lista:
						users_list_a_following.append(user_followers[each].username)

				elif value_check_box[0] == 'following':
					users_list_a_following.clear()
					userr_id_compare = cl.user_id_from_username(request.form['user_compare'])		
					user_following = cl.user_following(userr_id_compare)
					user_keys_following = user_following.keys()

					for each in user_keys_following:
						users_list_a_following.append(user_following[each].username)


				userr_id_main = cl.user_id_from_username(request.form['mainUsername'])
				user_followers = cl.user_followers(userr_id_main)
				user_keys_main = user_followers.keys()
				for each in user_keys_main:
					users_list_grand_followers.append(user_followers[each].username)	


				if request.form['user_compare'] is not None and request.form['mainUsername'] is not None:
					for each in users_list_a_following:
						if each not in users_list_grand_followers:
							users_a_list.append(each)
					print(users_a_list)
					users_a_list_zip = zip(users_a_list)
					with open('outputs/users_a_list.csv', 'w') as file:
						writer = csv.writer(file)
						writer.writerows(users_a_list_zip)
					

					for each in users_list_a_following:
						if each in users_list_grand_followers:
							users_b_list.append(each)
					print(users_b_list)
					users_b_list_zip = zip(users_b_list)
					with open('outputs/users_b_list.csv', 'w') as file:
						writer = csv.writer(file)
						writer.writerows(users_b_list_zip)

					print(users_b_list)
					for each in users_list_grand_followers:
						if each not in users_list_a_following:
							users_b_only.append(each)
						
					users_b_only_zip = zip(users_b_only)
					with open('outputs/users_b_only.csv', 'w') as file:
						writer = csv.writer(file)
						writer.writerows(users_b_only_zip)

				
	return render_template('home.html', users_a_list=users_a_list, users_b_list=users_b_list, not_in_community=users_b_only, count1=len(users_b_list), count2=len(users_b_only), count=len(users_a_list))

@app.route('/compare', methods=['GET', 'POST'])
def comapare_two():
	if request.form['user_compare'] is not None and request.form['mainUsername'] is not None:
				for each in users_list_main_following:
					if each not in users_list_grand_followers:
						users_a_list.append(each)
				print(users_a_list)
				users_a_list_zip = zip(users_a_list)
				with open('outputs/users_a_list.csv', 'w') as file:
					writer = csv.writer(file)
					writer.writerows(users_a_list_zip)
				

				for each in users_list_main_following:
					if each in users_list_grand_followers:
						users_b_list.append(each)
				print(users_b_list)
				users_b_list_zip = zip(users_b_list)
				with open('outputs/users_b_list.csv', 'w') as file:
					writer = csv.writer(file)
					writer.writerows(users_b_list_zip)

				print(users_b_list)
				for each in users_list_grand_followers:
					if each not in users_list_main_following:
						users_b_only.append(each)
					
				users_b_only_zip = zip(users_b_only)
				with open('outputs/users_b_only.csv', 'w') as file:
					writer = csv.writer(file)
					writer.writerows(users_b_only_zip)



@app.route('/scrape_list_a') 
def plot_csv0():
		
	return send_file('outputs/list_a.csv',
	             mimetype='text/csv',
	             attachment_filename='list_a.csv',
	             as_attachment=True)



@app.route('/scrape_list_b') 
def plot_csv5():
		
	return send_file('outputs/list_b_followers.csv',
	             mimetype='text/csv',
	             attachment_filename='list_b_followers.csv',
	             as_attachment=True)


@app.route('/getPlotCSV2') 
def plot_csv2():
		
	return send_file('outputs/users_a_list.csv',
	             mimetype='text/csv',
	             attachment_filename='users_a_list.csv',
	             as_attachment=True)


@app.route('/getPlotCSV3') 
def plot_csv3():
		
    return send_file('outputs/users_b_list.csv',
                     mimetype='text/csv',
                     attachment_filename='users_b_list.csv',
                     as_attachment=True)



@app.route('/getPlotCSV4') 
		
    return send_file('outputs/users_b_only.csv',
                     mimetype='text/csv',
                     attachment_filename='users_b_only.csv',
                     as_attachment=True)



if __name__ == "__main__":
	app.run(debug=True)