Commit 5b418051 authored by Cody Zacharias's avatar Cody Zacharias Committed by GitHub

Add custom CSV support

parent 2d23c0a5
...@@ -15,22 +15,9 @@ import time ...@@ -15,22 +15,9 @@ import time
def write(entry, f): def write(entry, f):
print(entry, file=open(f, "a", encoding="utf-8")) print(entry, file=open(f, "a", encoding="utf-8"))
def writeCSV(Tweet, file): def writeCSV(Tweet, config):
fieldnames = [
"id", data = {
"date",
"time",
"timezone",
"user_id",
"username",
"tweet",
"replies",
"retweets",
"likes",
"location",
"hashtags",
"link"]
row = {
"id": Tweet.id, "id": Tweet.id,
"date": Tweet.datestamp, "date": Tweet.datestamp,
"time": Tweet.timestamp, "time": Tweet.timestamp,
...@@ -45,11 +32,36 @@ def writeCSV(Tweet, file): ...@@ -45,11 +32,36 @@ def writeCSV(Tweet, file):
"hashtags": Tweet.hashtags, "hashtags": Tweet.hashtags,
"link": Tweet.link "link": Tweet.link
} }
if not (os.path.exists(file)):
with open(file, "w", newline='', encoding="utf-8") as csv_file: if config.Custom_csv:
fieldnames = config.Custom_csv
row = {}
for f in fieldnames:
row[f] = data[f]
else:
fieldnames = [
"id",
"date",
"time",
"timezone",
"user_id",
"username",
"tweet",
"replies",
"retweets",
"likes",
"location",
"hashtags",
"link"
]
row = data
if not (os.path.exists(config.Output)):
with open(config.Output, "w", newline='', encoding="utf-8") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader() writer.writeheader()
with open(file, "a", newline='', encoding="utf-8") as csv_file: with open(config.Output, "a", newline='', encoding="utf-8") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames) writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writerow(row) writer.writerow(row)
...@@ -180,7 +192,7 @@ async def Tweets(tw, location, config, conn): ...@@ -180,7 +192,7 @@ async def Tweets(tw, location, config, conn):
if config.Output != None: if config.Output != None:
if config.Store_csv: if config.Store_csv:
writeCSV(Tweet, config.Output) writeCSV(Tweet, config)
elif config.Store_json: elif config.Store_json:
writeJSON(Tweet, config.Output) writeJSON(Tweet, config.Output)
else: else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment