diff --git a/src/colab_noosfero/data_importer.py b/src/colab_noosfero/data_importer.py index 0334f1e..ab0f6d0 100644 --- a/src/colab_noosfero/data_importer.py +++ b/src/colab_noosfero/data_importer.py @@ -100,7 +100,7 @@ def fetch_communities(self): json_data = self.get_json_data(url, 1, timestamp=timestamp, order="updated_at ASC") - if len(json_data) == 0: + if not len(json_data) or not len(json_data.get('communities', [])): return json_data = json_data['communities'] @@ -110,7 +110,11 @@ def fetch_communities(self): if element['image']: community.thumb_url = element['image']['thumb_url'] - community.save() + + try: + community.save() + except: + continue if 'categories' in element: self.fetch_community_categories(community, @@ -140,11 +144,22 @@ def fetch_software_communities(self): 'NoosferoSoftwareCommunity') json_data = self.get_json_data(url, 1, timestamp=timestamp, order="updated_at ASC") + + if not len(json_data) or not len(json_data.get('software_infos', [])): + return + json_data = json_data['software_infos'] for element in json_data: - software_communty = NoosferoSoftwareCommunity() - self.fill_object_data(element, software_communty) - software_communty.save() + software_community = NoosferoSoftwareCommunity() + self.fill_object_data(element, software_community) + + try: + software_community.save() + except: + continue + + self.save_last_update(json_data[-1]['updated_at'], + 'NoosferoSoftwareCommunity') def fetch_articles(self): url = '/api/v1/articles' @@ -152,15 +167,18 @@ def fetch_articles(self): json_data = self.get_json_data(url, 1, timestamp=timestamp, order="updated_at ASC") - if len(json_data) == 0: + if not len(json_data) or not len(json_data.get('articles', [])): return json_data = json_data['articles'] - for element in json_data: article = NoosferoArticle() self.fill_object_data(element, article) - article.save() + + try: + article.save() + except: + continue for category_json in element["categories"]: category = NoosferoCategory.objects.get_or_create(