Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Adapter Design Pattern #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion scraping/scrape_twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@
import json
import datetime

class Username:
def __init__(self, username):
self.username = username

class TweetID:
def __init__(self, tweet_id):
self.tweet_id = tweet_id

class Hashtags:
def __init__(self, hashtags):
self.hashtags.hashtags

class Links:
def __init__(self, links):
self.links = links

class Timestamp:
def __init__(self, timestamp):
self.timestamp = timestamp

class Text:
def __init__(self, text):
self.text = text

class Adapter:
def __init__(self, obj, adpater_methods):
self.obj = obj
self.__dict__.update(adpater_methods)

def __getattribute__(self, attribute):
return getattr(self.obj, attribute)

def original_dict(self):
return self.obj.__dict__

if __name__ == '__main__':
search_query = "WuhanVirus OR 2019nCoV OR Coronavirus OR WuhanCoronavirus OR coronaviruses OR coronavirusoutbreak OR coronavirus OR Covid-19 OR COVID-19 OR ChineseCoronavirus OR Coronaoutbreak"
filename = "corona_twitter.json"
Expand All @@ -11,10 +46,22 @@
print("Found: {} tweets".format(len(tweets)))

j = []
username = Username()
tweet_id = TweetID()
hashtags = Hashtags()
links = Links()
timestamp = Timestamp()
text = Text()

for t in tweets:
t.timestamp = t.timestamp.isoformat()
print("{} {} {} {} {}: {}".format(t.username, t.tweet_id, t.hashtags, t.links, t.timestamp, t.text))
j.append(t.__dict__)
j.append(Adapter(username, t.username)
j.append(Adapter(tweetid, t.tweet_id)
j.append(Adapter(hashtags, t.hashtags)
j.append(Adapter(links, t.links)
j.append(Adapter(timestamp, t.timestamp)
j.append(Adapter(text, t.text)

with open(filename, "w") as f:
f.write(json.dumps(j))