Skip to content

Commit

Permalink
Restructure data_injector files
Browse files Browse the repository at this point in the history
  • Loading branch information
thenav56 committed Apr 16, 2024
1 parent fb50f0c commit ea021ed
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 48 deletions.
47 changes: 47 additions & 0 deletions apps/cap_feed/data_injector/feed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
import os

from apps.cap_feed.models import Country, Feed, LanguageInfo

module_dir = os.path.dirname(__file__) # get current directory


# inject feed configurations if not already present
def inject_feeds():
file_path = os.path.join(
os.path.dirname(module_dir),
'feeds.json',
)
with open(file_path, encoding='utf-8') as file:
feeds = json.load(file)
print('Injecting feeds...')
unique_countries = set()
feed_counter = 0
for feed_entry in feeds:
try:
feed = Feed()
feed.url = feed_entry['capAlertFeed']
feed.country = Country.objects.get(iso3=feed_entry['iso3'])
feed_counter += 1
unique_countries.add(feed_entry['iso3'])
if Feed.objects.filter(url=feed.url).first():
continue
feed.format = feed_entry['format']
feed.polling_interval = 60
feed.enable_polling = True
feed.enable_rebroadcast = True
feed.official = True
feed.save()

language_info = LanguageInfo()
language_info.feed = feed
language_info.name = feed_entry['name']
language_info.language = feed_entry['language']
language_info.logo = feed_entry['picUrl']
language_info.save()

except Exception as e:
print(feed_entry['name'])
print(f'Error injecting feed: {e}')

print(f'Injected {feed_counter} feeds for {len(unique_countries)} unique countries')
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import requests

from .models import Admin1, Continent, Country, Feed, LanguageInfo, Region
from apps.cap_feed.models import Admin1, Continent, Country, Region

module_dir = os.path.dirname(__file__) # get current directory

Expand Down Expand Up @@ -43,7 +43,7 @@ def process_continents():
continent_data = json.loads(response.content)
process_continents()
else:
file_path = os.path.join(module_dir, 'geographical/continents.json')
file_path = os.path.join(os.path.dirname(module_dir), 'geographical/continents.json')
with open(file_path) as file:
continent_data = json.load(file)
process_continents()
Expand All @@ -68,7 +68,7 @@ def process_regions():
region_data = json.loads(response.content)
process_regions()
else:
file_path = os.path.join(module_dir, 'geographical/ifrc-regions.json')
file_path = os.path.join(os.path.dirname(module_dir), 'geographical/ifrc-regions.json')
with open(file_path) as file:
region_data = json.load(file)
process_regions()
Expand Down Expand Up @@ -125,7 +125,7 @@ def process_countries_opendatasoft():
region_data = json.loads(response.content)
process_regions()
else:
file_path = os.path.join(module_dir, 'geographical/ifrc-regions.json')
file_path = os.path.join(os.path.dirname(module_dir), 'geographical/ifrc-regions.json')
with open(file_path) as file:
region_data = json.load(file)
process_regions()
Expand All @@ -137,7 +137,7 @@ def process_countries_opendatasoft():
country_data = json.loads(response.content)
process_countries_ifrc()
else:
file_path = os.path.join(module_dir, 'geographical/ifrc-countries-and-territories.json')
file_path = os.path.join(os.path.dirname(module_dir), 'geographical/ifrc-countries-and-territories.json')
with open(file_path) as file:
country_data = json.load(file)
process_countries_ifrc()
Expand All @@ -149,7 +149,7 @@ def process_countries_opendatasoft():
country_data = json.loads(response.content)
process_countries_opendatasoft()
else:
file_path = os.path.join(module_dir, 'geographical/opendatasoft-countries-and-territories.geojson')
file_path = os.path.join(os.path.dirname(module_dir), 'geographical/opendatasoft-countries-and-territories.geojson')
with open(file_path) as file:
country_data = json.load(file)
process_countries_opendatasoft()
Expand Down Expand Up @@ -184,45 +184,7 @@ def process_admin1s():
admin1_data = json.loads(response.content)
process_admin1s()
else:
file_path = os.path.join(module_dir, 'geographical/geoBoundariesCGAZ_ADM1.geojson')
file_path = os.path.join(os.path.dirname(module_dir), 'geographical/geoBoundariesCGAZ_ADM1.geojson')
with open(file_path, encoding='utf-8') as f:
admin1_data = json.load(f)
process_admin1s()


# inject feed configurations if not already present
def inject_feeds():
file_path = os.path.join(module_dir, 'feeds.json')
with open(file_path, encoding='utf-8') as file:
feeds = json.load(file)
print('Injecting feeds...')
unique_countries = set()
feed_counter = 0
for feed_entry in feeds:
try:
feed = Feed()
feed.url = feed_entry['capAlertFeed']
feed.country = Country.objects.get(iso3=feed_entry['iso3'])
feed_counter += 1
unique_countries.add(feed_entry['iso3'])
if Feed.objects.filter(url=feed.url).first():
continue
feed.format = feed_entry['format']
feed.polling_interval = 60
feed.enable_polling = True
feed.enable_rebroadcast = True
feed.official = True
feed.save()

language_info = LanguageInfo()
language_info.feed = feed
language_info.name = feed_entry['name']
language_info.language = feed_entry['language']
language_info.logo = feed_entry['picUrl']
language_info.save()

except Exception as e:
print(feed_entry['name'])
print(f'Error injecting feed: {e}')

print(f'Injected {feed_counter} feeds for {len(unique_countries)} unique countries')
7 changes: 4 additions & 3 deletions apps/cap_feed/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from celery import shared_task
from django.utils import timezone

from . import data_injector as di
from .data_injector.geo import inject_geographical_data
from .data_injector.feed import inject_feeds
from .formats import format_handler as fh
from .models import Alert, AlertInfo, Feed, ProcessedAlert

Expand Down Expand Up @@ -40,6 +41,6 @@ def remove_expired_alert_records():

@shared_task
def inject_data():
di.inject_geographical_data()
di.inject_feeds()
inject_geographical_data()
inject_feeds()
return "injected data"

0 comments on commit ea021ed

Please sign in to comment.