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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def scrape_instagram(user_name):
|
| 5 |
+
|
| 6 |
+
url = "https://instagram-scraper-api2.p.rapidapi.com/v1/info"
|
| 7 |
+
|
| 8 |
+
querystring = {"username_or_id_or_url":f"{user_name}"}
|
| 9 |
+
|
| 10 |
+
headers = {
|
| 11 |
+
"x-rapidapi-key": f"{RAPIDAPI_API_KEY}",
|
| 12 |
+
"x-rapidapi-host": "instagram-scraper-api2.p.rapidapi.com"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
response = requests.get(url, headers=headers, params=querystring)
|
| 16 |
+
|
| 17 |
+
if response.status_code != 200:
|
| 18 |
+
print(f"Failed to fetch profile: {response.status_code}")
|
| 19 |
+
return {} # Return an empty dictionary if the request fails
|
| 20 |
+
|
| 21 |
+
response_json = response.json()
|
| 22 |
+
if 'data' not in response_json:
|
| 23 |
+
print("No data found in response")
|
| 24 |
+
return {} # Return an empty dictionary if there is no data in the response
|
| 25 |
+
|
| 26 |
+
response_data = response_json['data']
|
| 27 |
+
|
| 28 |
+
profile_info = {
|
| 29 |
+
'bio': response_data.get('biography', ''),
|
| 30 |
+
'follower_count': response_data.get('follower_count', 0),
|
| 31 |
+
'following_count': response_data.get('following_count', 0),
|
| 32 |
+
'bio_links': [item['url'] for item in response_data.get('bio_links', [])],
|
| 33 |
+
'full_name': response_data.get('full_name', ''),
|
| 34 |
+
'username': response_data.get('username', ''),
|
| 35 |
+
'num_posts': response_data.get('media_count', 0),
|
| 36 |
+
'profile_id': response_data.get('profile_pic_id', ''),
|
| 37 |
+
'email': response_data.get('biography_email', ''),
|
| 38 |
+
'badge': response_data.get('account_badges', []),
|
| 39 |
+
'category': response_data.get('category', ''),
|
| 40 |
+
'phone_number': response_data.get('contact_phone_number', ''),
|
| 41 |
+
'city_name': response_data.get('location_data', {}).get('city_name', ''),
|
| 42 |
+
'country': response_data.get('about', {}).get('country', ''),
|
| 43 |
+
'date_joined': response_data.get('about', {}).get('date_joined', '')
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
return profile_info
|
| 47 |
+
|
| 48 |
+
def get_insta_info(df):
|
| 49 |
+
# Add new columns to the DataFrame
|
| 50 |
+
df['Bio'] = ''
|
| 51 |
+
df['Follower Count'] = 0
|
| 52 |
+
df['Following Count'] = 0
|
| 53 |
+
df['Bio Links'] = ''
|
| 54 |
+
df['Full Name'] = ''
|
| 55 |
+
df['Username'] = ''
|
| 56 |
+
df['Num Posts'] = 0
|
| 57 |
+
df['Profile ID'] = ''
|
| 58 |
+
df['Email'] = ''
|
| 59 |
+
df['Badge'] = ''
|
| 60 |
+
df['Category'] = ''
|
| 61 |
+
df['Phone Number'] = ''
|
| 62 |
+
df['City Name'] = ''
|
| 63 |
+
df['Country'] = ''
|
| 64 |
+
df['Date Joined'] = ''
|
| 65 |
+
|
| 66 |
+
for index, row in df.iterrows():
|
| 67 |
+
profile_info = scrape_instagram(row['Username'])
|
| 68 |
+
|
| 69 |
+
if profile_info: # Only populate if profile_info is not empty
|
| 70 |
+
df.at[index, 'Bio'] = profile_info['bio']
|
| 71 |
+
df.at[index, 'Follower Count'] = profile_info['follower_count']
|
| 72 |
+
df.at[index, 'Following Count'] = profile_info['following_count']
|
| 73 |
+
df.at[index, 'Bio Links'] = ', '.join(profile_info['bio_links'])
|
| 74 |
+
df.at[index, 'Full Name'] = profile_info['full_name']
|
| 75 |
+
df.at[index, 'Username'] = profile_info['username']
|
| 76 |
+
df.at[index, 'Num Posts'] = profile_info['num_posts']
|
| 77 |
+
df.at[index, 'Profile ID'] = profile_info['profile_id']
|
| 78 |
+
df.at[index, 'Email'] = profile_info['email']
|
| 79 |
+
df.at[index, 'Badge'] = ', '.join(profile_info['badge'])
|
| 80 |
+
df.at[index, 'Category'] = profile_info['category']
|
| 81 |
+
df.at[index, 'Phone Number'] = profile_info['phone_number']
|
| 82 |
+
df.at[index, 'City Name'] = profile_info['city_name']
|
| 83 |
+
df.at[index, 'Country'] = profile_info['country']
|
| 84 |
+
df.at[index, 'Date Joined'] = profile_info['date_joined']
|
| 85 |
+
|
| 86 |
+
return df
|
| 87 |
+
|
| 88 |
+
# Function to scrape LinkedIn profiles
|
| 89 |
+
def scrape_linkedins(links):
|
| 90 |
+
url = "https://linkedin-bulk-data-scraper.p.rapidapi.com/profiles"
|
| 91 |
+
|
| 92 |
+
payload = {"links": links}
|
| 93 |
+
|
| 94 |
+
headers = {
|
| 95 |
+
"x-rapidapi-key": f"{RAPIDAPI_API_KEY}",
|
| 96 |
+
"x-rapidapi-host": "linkedin-bulk-data-scraper.p.rapidapi.com",
|
| 97 |
+
"Content-Type": "application/json",
|
| 98 |
+
"x-rapidapi-user": "usama"
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
# Initialize an empty list to store the dictionaries
|
| 102 |
+
profile_info_list = []
|
| 103 |
+
|
| 104 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 105 |
+
responses = response.json()['data']
|
| 106 |
+
for response_item in responses:
|
| 107 |
+
response_data = response_item.get('data', {})
|
| 108 |
+
|
| 109 |
+
# Use get() method with default empty strings for missing fields
|
| 110 |
+
profile_info = {
|
| 111 |
+
'full_name': response_data.get('fullName', ''),
|
| 112 |
+
'headline': response_data.get('headline', ''),
|
| 113 |
+
'connections': response_data.get('followers', ''), # or 'connections' based on availability
|
| 114 |
+
'country': response_data.get('addressCountryOnly', ''),
|
| 115 |
+
'address': response_data.get('addressWithoutCountry', ''),
|
| 116 |
+
'about': response_data.get('about', ''),
|
| 117 |
+
'current_role': (f"{response_data.get('experiences', [{}])[0].get('title', '')} at "
|
| 118 |
+
f"{response_data.get('experiences', [{}])[0].get('subtitle', '')}"),
|
| 119 |
+
'education': (f"{response_data.get('educations', [{}])[0].get('subtitle', '')} at "
|
| 120 |
+
f"{response_data.get('educations', [{}])[0].get('title', '')}")
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
# Append the dictionary to the list
|
| 124 |
+
profile_info_list.append(profile_info)
|
| 125 |
+
|
| 126 |
+
return profile_info_list
|
| 127 |
+
|
| 128 |
+
# Function to populate DataFrame with LinkedIn information
|
| 129 |
+
def get_LI_info(df):
|
| 130 |
+
links = df['Links'].tolist()
|
| 131 |
+
profile_info_list = scrape_linkedins(links)
|
| 132 |
+
|
| 133 |
+
# Add new columns to the DataFrame
|
| 134 |
+
df['Full Name'] = ''
|
| 135 |
+
df['Headline'] = ''
|
| 136 |
+
df['Connections'] = ''
|
| 137 |
+
df['Country'] = ''
|
| 138 |
+
df['Address'] = ''
|
| 139 |
+
df['About'] = ''
|
| 140 |
+
df['Current Role'] = ''
|
| 141 |
+
df['Education'] = ''
|
| 142 |
+
|
| 143 |
+
# Populate DataFrame with profile information
|
| 144 |
+
for i in range(len(profile_info_list)):
|
| 145 |
+
df.at[i, 'Full Name'] = profile_info_list[i]['full_name']
|
| 146 |
+
df.at[i, 'Headline'] = profile_info_list[i]['headline']
|
| 147 |
+
df.at[i, 'Connections'] = profile_info_list[i]['connections']
|
| 148 |
+
df.at[i, 'Country'] = profile_info_list[i]['country']
|
| 149 |
+
df.at[i, 'Address'] = profile_info_list[i]['address']
|
| 150 |
+
df.at[i, 'About'] = profile_info_list[i]['about']
|
| 151 |
+
df.at[i, 'Current Role'] = profile_info_list[i]['current_role']
|
| 152 |
+
df.at[i, 'Most Recent Education'] = profile_info_list[i]['education']
|
| 153 |
+
|
| 154 |
+
return df
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def get_scrape_data(csv_file, social_media):
|
| 158 |
+
df = pd.read_csv(csv_file.name)
|
| 159 |
+
if social_media == 'LinkedIN':
|
| 160 |
+
output_df = get_LI_info(df)
|
| 161 |
+
elif social_media == 'Instagram':
|
| 162 |
+
output_df = get_insta_info(df)
|
| 163 |
+
|
| 164 |
+
file_name = './output.csv'
|
| 165 |
+
outpu_df.to_csv('./output.csv')
|
| 166 |
+
completion_status = "Done"
|
| 167 |
+
return completion_status, data_preview, gr.DownloadButton(label='Download AI Content', value=file_name, visible=True)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
with gr.Blocks() as block:
|
| 171 |
+
gr.Markdown("""
|
| 172 |
+
# Social Media Scraper Dashboard
|
| 173 |
+
This dashboard is scrapes data from Linkedin and Instagram
|
| 174 |
+
""")
|
| 175 |
+
|
| 176 |
+
with gr.Column(visible=True):
|
| 177 |
+
gr.Markdown('''
|
| 178 |
+
# Scrape Content
|
| 179 |
+
Upload a spreadsheet with descriptions of website content
|
| 180 |
+
''')
|
| 181 |
+
csv_file = gr.File(label='Input CSV File (must be CSV File)')
|
| 182 |
+
social_media = gr.Radio(choices=['LinkedIn', 'Instagram'], label='Which Social Media?', info = 'Which Social Media do you want to scrape from?')
|
| 183 |
+
con_gen_btn = gr.Button('Scrape')
|
| 184 |
+
status = gr.Textbox(label='Completion Status')
|
| 185 |
+
download_btn = gr.DownloadButton(label='Download Content', visible=False)
|
| 186 |
+
|
| 187 |
+
con_gen_btn.click(get_scarpe_data, inputs=[csv_file, social_media], outputs=[status, download_btn])
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
block.queue(default_concurrency_limit=5)
|
| 191 |
+
block.launch()
|