Last active
February 4, 2025 21:39
-
-
Save farhadmpr/bb0dc8f0da6b317759908975b5b672e8 to your computer and use it in GitHub Desktop.
instagram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import instaloader | |
import sqlite3 | |
L = instaloader.Instaloader() | |
L.login('', '') | |
profile = instaloader.Profile.from_username(L.context, '') | |
def create_connection(db_file): | |
conn = None | |
try: | |
conn = sqlite3.connect(db_file) | |
return conn | |
except sqlite3.Error as e: | |
print(e) | |
return conn | |
def create_table(conn, create_table_sql): | |
try: | |
c = conn.cursor() | |
c.execute(create_table_sql) | |
except sqlite3.Error as e: | |
print(e) | |
def insert_page(conn, page): | |
try: | |
c = conn.cursor() | |
c.execute('SELECT * FROM pages WHERE (userid=? AND username=?)', | |
(page['userid'], page['username'])) | |
page_in_db = c.fetchone() | |
if page_in_db is None: | |
c.execute('''INSERT INTO pages( | |
username, | |
userid, | |
full_name, | |
is_private, | |
media_count, | |
followers_count, | |
biography, | |
profile_pic_url) | |
VALUES(?, ?, ?, ?, ?, ?, ?, ?)''', | |
( | |
page['username'], | |
page['userid'], | |
page['full_name'], | |
page['is_private'], | |
page['media_count'], | |
page['followers_count'], | |
page['biography'], | |
page['profile_pic_url'], | |
)) | |
conn.commit() | |
print(f'"{page["username"]}"\t added to db') | |
else: | |
print(f'"{page["username"]}"\t already exist') | |
except sqlite3.Error as e: | |
print(e) | |
def get_followers(conn): | |
count = 0 | |
for follower in profile.get_followers(): | |
insert_page(conn, | |
{ | |
'username': follower.username, | |
'userid': follower.userid, | |
'is_private': follower.is_private, | |
'media_count': follower.mediacount, | |
'followers_count': follower.followers, | |
'biography': follower.biography, | |
'full_name': follower.full_name, | |
'profile_pic_url': follower.profile_pic_url, | |
} | |
) | |
count += 1 | |
print(f'{count} pages completed') | |
def main(): | |
database = "pages.db" | |
sql_create_pages_table = """CREATE TABLE IF NOT EXISTS pages ( | |
id integer PRIMARY KEY, | |
username text NOT NULL, | |
userid integer, | |
full_name text, | |
is_private integer, | |
media_count integer, | |
followers_count integer, | |
biography text, | |
profile_pic_url text | |
);""" | |
conn = create_connection(database) | |
create_table(conn, sql_create_pages_table) | |
print('db created...') | |
get_followers(conn) | |
conn.close() | |
if __name__ == '__main__': | |
print('start...') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use it??