-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
60 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import os | ||
import pickle | ||
import json | ||
import multiprocessing | ||
import pycountry | ||
|
||
|
||
def filter_info(old): | ||
new = { | ||
'created_at': old['created_at'], | ||
'id': old['id'], | ||
'text': old['text'], | ||
'source': old['source'], | ||
'country_code': old['place']['country_code'] | ||
} | ||
return new | ||
|
||
|
||
def file_processor(q): | ||
while True: | ||
file_path = q.get() | ||
with open(file_path) as f: | ||
for line in f: | ||
tweet = filter_info(json.loads(line)) | ||
country = tweet['country_code'] | ||
if country == '': | ||
continue | ||
if country not in country_tweets: | ||
country_tweets[country] = list() | ||
tweets = country_tweets[country] | ||
tweets.append(tweet) | ||
q.task_done() | ||
|
||
|
||
if __name__ == '__main__': | ||
q = multiprocessing.JoinableQueue() | ||
manager = multiprocessing.Manager() | ||
country_tweets = manager.dict() | ||
for i in range(os.cpu_count()-1): | ||
p = multiprocessing.Process(target=file_processor, args=(q,), daemon=True) | ||
p.start() | ||
for path, dirs, files in os.walk('processed'): | ||
for file in files: | ||
file_path = os.path.join(path, file) | ||
if file.endswith('.json'): | ||
q.put(file_path) | ||
|
||
q.join() | ||
for country, tweets in country_tweets.items(): | ||
with open(os.path.join('countries', country + '.json'), 'a') as f: | ||
f.write(json.dumps(tweets)) | ||
|
||
print('done, safe to ctrl-c if it does not exit automatically') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters