-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (109 loc) · 4.14 KB
/
app.py
File metadata and controls
133 lines (109 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import io
import pandas as pd
import requests
from bs4 import BeautifulSoup
from flask import Flask, jsonify, render_template, request
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
app = Flask(__name__)
# --- Facebook SDK Initialization ---
# PLEASE REPLACE WITH YOUR OWN CREDENTIALS
APP_ID = 'YOUR_APP_ID'
APP_SECRET = 'YOUR_APP_SECRET'
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
FacebookAdsApi.init(APP_ID, APP_SECRET, ACCESS_TOKEN)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/audience_network_adsets')
def get_audience_network_adsets():
try:
fields = [
'name',
'targeting',
]
ad_account = AdAccount('me')
adsets = ad_account.get_ad_sets(fields=fields)
# Filter for ad sets that target the Audience Network
audience_network_adsets = []
for adset in adsets:
targeting = adset.get('targeting', {})
publisher_platforms = targeting.get('publisher_platforms', [])
if 'audience_network' in publisher_platforms:
audience_network_adsets.append({
'name': adset['name'],
'id': adset['id'],
'targeting': targeting,
})
return jsonify(audience_network_adsets)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/seo_analysis')
def seo_analysis():
url = request.args.get('url')
if not url:
return jsonify({'error': 'URL parameter is required'}), 400
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
title = soup.find('title').string if soup.find('title') else 'No title found'
meta_description = soup.find('meta', attrs={'name': 'description'})
meta_description = meta_description.get('content') if meta_description else 'No meta description found'
h1_tags = [h1.string for h1 in soup.find_all('h1')]
return jsonify({
'title': title,
'meta_description': meta_description,
'h1_tags': h1_tags,
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/data_analysis', methods=['POST'])
def data_analysis():
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and file.filename.endswith('.csv'):
try:
csv_data = io.StringIO(file.stream.read().decode('UTF8'))
df = pd.read_csv(csv_data)
return jsonify({
'summary_stats': df.describe().to_dict(),
'shape': df.shape,
})
except Exception as e:
return jsonify({'error': str(e)}), 500
else:
return jsonify({'error': 'Invalid file type, please upload a CSV'}), 400
@app.route('/business_leads')
def business_leads():
leads = [
{'company': 'Innovate Corp', 'contact': 'John Doe', 'email': '[email protected]'},
{'company': 'Data Solutions', 'contact': 'Jane Smith', 'email': '[email protected]'},
{'company': 'Global Connect', 'contact': 'Peter Jones', 'email': '[email protected]'},
]
return jsonify(leads)
@app.route('/enhanced_analytics')
def enhanced_analytics():
try:
fields = [
'name',
'insights.fields(spend,impressions,clicks,ctr,cpc)',
]
ad_account = AdAccount('me')
adsets = ad_account.get_ad_sets(fields=fields)
analytics_data = []
for adset in adsets:
insights = adset.get('insights', {}).get('data', [])
if insights:
analytics_data.append({
'name': adset['name'],
'id': adset['id'],
'insights': insights[0],
})
return jsonify(analytics_data)
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, port=8080)