In [1]:
import json
import pandas as pd
import requests

In [2]:
with open('data/search_engine/answer.csv', 'r') as a:
    answer_df = pd.read_csv(a)
    
with open('data/search_engine/post.csv', 'r') as b:
    post_df = pd.read_csv(b)

In [3]:
answer_df = answer_df.fillna('')

In [4]:
answer_df.head()


Out[4]:
answer_text date_created date_updated search_table_id category_main category_sec category_thir category_four category_five link_main link_sec link_thir name_link_main name_link_sec name_link_thir
0 No se require visa, sin embargo, se requiere u... 6-Aug-19 6-Aug-19 1 Viaje Visa Nueva Zelanda Auckland https://www.immigration.govt.nz/new-zealand-vi... Immigracion de NZ
1 El tiempo determinado por la embajada ade Nuev... 6-Aug-19 6-Aug-19 2 Viaje Visa Nueva Zelanda https://www.immigration.govt.nz/new-zealand-vi... Immigracion de NZ
2 Existen muchas agencias de immigracion que te ... 6-Aug-19 6-Aug-19 3 Negocios Agencias Estudiar Visa Blog
3 Para trabajar de mensajero conduciendo una mot... 6-Aug-19 6-Aug-19 4 Vida Seguro Trabajo Negocios https://deliveroo.bikmo.com/ https://www.youi.com.au/ Deliveroo Seguros Youi
4 La documentacion requerida para la aplicacion ... 6-Aug-19 6-Aug-19 5 Visa Documentos Trabajo

In [5]:
post_df = post_df.fillna('')

In [6]:
request_data = []
for i, row in answer_df.iterrows():
    answer = {
        "answer": row["answer_text"],
        "answer_html": "",
        "category_1": row["category_main"],
        "category_2": row["category_sec"],
        "category_3": row["category_thir"],
        "category_4": row["category_four"],
        "category_5": row["category_five"],
        "link_1": row["link_main"],
        "link_2": row["link_sec"],
        "link_3": row["link_thir"],
        "label_link_1": row["name_link_main"],
        "label_link_2": row["name_link_sec"],
        "label_link_3": row["name_link_thir"],
        "thumbs_up_count": 0,
        "thumbs_down_count": 0
    }
    
    selected_post_df = post_df[post_df["search_table_id"] == row["search_table_id"]]
    
    posts = [{
        "city": post["city"],
        "country": post["country"],
        "question_text": post["post_question_text"],
        "url": post["url"],
        "raw_text": post["post_raw_Text"],
        "fb_post_id": post["post_id"],
        "fb_group_id": post["group_id"],
        "fb_group_name": post["group_name"],
        "fb_timestamp": "",
        "reaction_like": 0,
        "reaction_love": 0,
        "reaction_haha": 0,
        "reaction_wow": 0,
        "tag_1": "",
        "tag_2": "",
        "tag_3": "",
        "tag_4": "",
        "tag_5": ""
    } for idx, post in selected_post_df.iterrows()]
    answer["posts"] = posts
    request_data.append(answer)

In [7]:
request_data[0]


Out[7]:
{'answer': 'No se require visa, sin embargo, se requiere un permiso que se obtiene online si viajas despues del 1 de Octuber 2019. Se recomienda aplicar al menos 3 días antes del viaje. Puedes encontrar mas información en el siguiente enlace <LINK1>',
 'answer_html': '',
 'category_1': 'Viaje',
 'category_2': 'Visa',
 'category_3': 'Nueva Zelanda',
 'category_4': 'Auckland',
 'category_5': '',
 'link_1': 'https://www.immigration.govt.nz/new-zealand-visas/apply-for-a-visa/about-visa/nzeta',
 'link_2': '',
 'link_3': '',
 'label_link_1': 'Immigracion de NZ',
 'label_link_2': '',
 'label_link_3': '',
 'thumbs_up_count': 0,
 'thumbs_down_count': 0,
 'posts': [{'city': 'Melbourne',
   'country': 'Australia',
   'question_text': 'Se necesita visa para hacer escala en Auckland?',
   'url': 'https://m.facebook.com/groups/8599519511?view=permalink&id=10157271055259512',
   'raw_text': '',
   'fb_post_id': '',
   'fb_group_id': '',
   'fb_group_name': '',
   'fb_timestamp': '',
   'reaction_like': 0,
   'reaction_love': 0,
   'reaction_haha': 0,
   'reaction_wow': 0,
   'tag_1': '',
   'tag_2': '',
   'tag_3': '',
   'tag_4': '',
   'tag_5': ''},
  {'city': 'Melbourne',
   'country': 'Australia',
   'question_text': 'Se necesita visa para hacer escala en Auckland?',
   'url': 'https://m.facebook.com/groups/8599519511?view=permalink&id=10156990656259512',
   'raw_text': '',
   'fb_post_id': '',
   'fb_group_id': '',
   'fb_group_name': '',
   'fb_timestamp': '',
   'reaction_like': 0,
   'reaction_love': 0,
   'reaction_haha': 0,
   'reaction_wow': 0,
   'tag_1': '',
   'tag_2': '',
   'tag_3': '',
   'tag_4': '',
   'tag_5': ''},
  {'city': 'Sydney',
   'country': 'Australia',
   'question_text': '¿Necesito visa para hacer escala en Nueva Zelanda?',
   'url': 'https://www.facebook.com/groups/ColombianosEnSydney/permalink/2375323772477865/',
   'raw_text': '',
   'fb_post_id': '',
   'fb_group_id': '',
   'fb_group_name': '',
   'fb_timestamp': '',
   'reaction_like': 0,
   'reaction_love': 0,
   'reaction_haha': 0,
   'reaction_wow': 0,
   'tag_1': '',
   'tag_2': '',
   'tag_3': '',
   'tag_4': '',
   'tag_5': ''},
  {'city': 'Sydney',
   'country': 'Australia',
   'question_text': '¿Puedo hacer escala en Nueva Zelanda si solo tengo la visa australiana?',
   'url': 'https://www.facebook.com/groups/colombianosensydneyoficial/permalink/921400294650354/?sale_post_id=921400294650354',
   'raw_text': '',
   'fb_post_id': '',
   'fb_group_id': '',
   'fb_group_name': '',
   'fb_timestamp': '',
   'reaction_like': 0,
   'reaction_love': 0,
   'reaction_haha': 0,
   'reaction_wow': 0,
   'tag_1': '',
   'tag_2': '',
   'tag_3': '',
   'tag_4': '',
   'tag_5': ''}]}

In [8]:
len(request_data)


Out[8]:
196

In [9]:
api = "https://stage.passporr.com/api/v1/admin/answers/bulk"
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjgyMDY1OTMsImlhdCI6MTU2NTYxNDU5Mywic3ViIjoxfQ.qxNikrv0fSh_T9XhSQoexAzC2MBh1Xc-A0C58Q0DCWM"
try:
    headers = {'Authorization': f"Bearer {token}"}
    response = requests.post(api, json=request_data, headers=headers)
    print(response)
    print(response.raise_for_status())
except Exception as e:
    print(e)


<Response [201]>
None

In [10]:
api = "https://passporr.com/api/v1/admin/answers/bulk"
token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1NjgyMDY5MzgsImlhdCI6MTU2NTYxNDkzOCwic3ViIjoxfQ.-Cbd2y98B-cl7UUGQE4DC5j5VLL8xZA0FMIQJW4OShU"
try:
    headers = {'Authorization': f"Bearer {token}"}
    response = requests.post(api, json=request_data, headers=headers)
    print(response)
    print(response.raise_for_status())
except Exception as e:
    print(e)


<Response [201]>
None

In [ ]: