File tree Expand file tree Collapse file tree 4 files changed +24
-17
lines changed Expand file tree Collapse file tree 4 files changed +24
-17
lines changed Original file line number Diff line number Diff line change 30
30
def FB_POST_INSIGHTS_V1 ():
31
31
@task
32
32
def CREATE_TABLE_IF_NEEDED ():
33
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
34
- post_sql = """
33
+ create_post_table_sql = """
35
34
CREATE TABLE IF NOT EXISTS `pycontw-225217.ods.ods_pycontw_fb_posts` (
36
35
id STRING,
37
36
created_at TIMESTAMP,
38
37
message STRING
39
38
)
40
39
"""
41
- client .query (post_sql )
42
- insights_sql = """
40
+ create_insights_table_sql = """
43
41
CREATE TABLE IF NOT EXISTS `pycontw-225217.ods.ods_pycontw_fb_posts_insights` (
44
42
post_id STRING,
45
43
query_time TIMESTAMP,
@@ -48,24 +46,29 @@ def CREATE_TABLE_IF_NEEDED():
48
46
share INTEGER
49
47
)
50
48
"""
51
- client .query (insights_sql )
49
+
50
+ bg_project = os .getenv ("BIGQUERY_PROJECT" )
51
+ client = bigquery .Client (project = bg_project )
52
+ client .query (create_post_table_sql )
53
+ client .query (create_insights_table_sql )
52
54
53
55
@task
54
56
def SAVE_FB_POSTS_AND_INSIGHTS ():
55
57
posts = request_posts_data ()
56
58
57
59
last_post = query_last_post ()
58
- if last_post is None :
59
- new_posts = posts
60
- else :
61
- new_posts = [
60
+ new_posts = (
61
+ [
62
62
post
63
63
for post in posts
64
64
if datetime .strptime (
65
65
post ["created_time" ], "%Y-%m-%dT%H:%M:%S%z"
66
66
).timestamp ()
67
67
> last_post ["created_at" ].timestamp ()
68
68
]
69
+ if last_post is not None
70
+ else posts
71
+ )
69
72
70
73
if not dump_posts_to_bigquery (
71
74
[
Original file line number Diff line number Diff line change @@ -113,9 +113,12 @@ def dump_posts_to_bigquery(posts: list[dict]) -> bool:
113
113
client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
114
114
job_config = bigquery .LoadJobConfig (
115
115
schema = [
116
- bigquery .SchemaField ("id" , "STRING" , mode = "REQUIRED" ),
117
- bigquery .SchemaField ("created_at" , "TIMESTAMP" , mode = "REQUIRED" ),
118
- bigquery .SchemaField ("message" , "STRING" , mode = "REQUIRED" ),
116
+ bigquery .SchemaField (field_name , field_type , mode = "REQUIRED" )
117
+ for field_name , field_type in [
118
+ ("id" , "STRING" ),
119
+ ("created_at" , "TIMESTAMP" ),
120
+ ("message" , "STRING" ),
121
+ ]
119
122
],
120
123
write_disposition = "WRITE_APPEND" ,
121
124
)
@@ -128,6 +131,7 @@ def dump_posts_to_bigquery(posts: list[dict]) -> bool:
128
131
job .result ()
129
132
return True
130
133
except Exception as e :
134
+ # TODO: catch with more specific exception
131
135
logger .error (f"Failed to dump posts to BigQuery: { e } " , exc_info = True )
132
136
return False
133
137
Original file line number Diff line number Diff line change 27
27
28
28
29
29
def create_table_if_needed () -> None :
30
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
31
30
post_sql = """
32
31
CREATE TABLE IF NOT EXISTS `pycontw-225217.ods.ods_pycontw_ig_posts` (
33
32
id STRING,
34
33
created_at TIMESTAMP,
35
34
message STRING
36
35
)
37
36
"""
38
- client .query (post_sql )
39
37
insights_sql = """
40
38
CREATE TABLE IF NOT EXISTS `pycontw-225217.ods.ods_pycontw_ig_posts_insights` (
41
39
post_id STRING,
@@ -47,6 +45,9 @@ def create_table_if_needed() -> None:
47
45
views INTEGER
48
46
)
49
47
"""
48
+
49
+ client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
50
+ client .query (post_sql )
50
51
client .query (insights_sql )
51
52
52
53
@@ -122,7 +123,6 @@ def request_posts_data() -> list[dict]:
122
123
media_list = response .json ()["data" ]
123
124
124
125
media_insight_list = []
125
-
126
126
for media in media_list :
127
127
media_insight_url = f"https://graph.facebook.com/v20.0/{ media ['id' ]} "
128
128
querystring = {
Original file line number Diff line number Diff line change 10
10
11
11
12
12
def create_table_if_needed () -> None :
13
- client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
14
13
post_sql = """
15
14
CREATE TABLE IF NOT EXISTS `pycontw-225217.ods.ods_pycontw_linkedin_posts` (
16
15
id STRING,
17
16
created_at TIMESTAMP,
18
17
message STRING
19
18
)
20
19
"""
21
- client .query (post_sql )
22
20
insights_sql = """
23
21
CREATE TABLE IF NOT EXISTS `pycontw-225217.ods.ods_pycontw_linkedin_posts_insights` (
24
22
post_id STRING,
@@ -30,6 +28,8 @@ def create_table_if_needed() -> None:
30
28
views INTEGER
31
29
)
32
30
"""
31
+ client = bigquery .Client (project = os .getenv ("BIGQUERY_PROJECT" ))
32
+ client .query (post_sql )
33
33
client .query (insights_sql )
34
34
35
35
# Example output from the Rapid API, not all fields will exists for a specific post
You can’t perform that action at this time.
0 commit comments