Update graphql.py

This commit is contained in:
Kacper Ziubryniewicz
2018-08-09 20:04:20 +02:00
committed by GitHub
parent d98d802a33
commit 8beb1e5753

View File

@@ -5,6 +5,7 @@ import json
import re
from .models import *
from .utils import *
import ast
# Shameless copy from https://stackoverflow.com/a/8730674
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
@@ -143,10 +144,49 @@ def graphql_to_poll_option(a):
vote=a.get('viewer_has_voted') == 'true' if isinstance(a.get('viewer_has_voted'), str) else a.get('viewer_has_voted')
)
rtn.uid = a.get('id')
rtn.voters = list(map(lambda x:x.get('node').get('id'), a.get('voters').get('edges'))) if isinstance(a.get('voters'), dict) else a.get('voters')
rtn.voters = [m.get('node').get('id') for m in a.get('voters').get('edges')] if isinstance(a.get('voters'), dict) else a.get('voters')
rtn.votes_count = a.get('voters').get('count') if isinstance(a.get('voters'), dict) else a.get('total_count')
return rtn
def graphql_to_event(a):
if a.get('event_members') is not None:
rtn = Event(
time=a.get('event_time'),
title=a.get('title'),
location=a.get('location_name')
)
if a.get('location_id') != 0:
rtn.location_id = str(a.get('location_id'))
rtn.creator_id = a.get('creator_id')
guests = a.get("event_members")
rtn.going = [uid for uid in guests if guests[uid] == "GOING"]
rtn.declined = [uid for uid in guests if guests[uid] == "DECLINED"]
rtn.invited = [uid for uid in guests if guests[uid] == "INVITED"]
return rtn
elif a.get('id') is None:
rtn = Event(
time=a.get('event_time'),
title=a.get('event_title'),
location=a.get('event_location_name'),
location_id=a.get('event_location_id')
)
rtn.uid = a.get('event_id')
rtn.creator_id = a.get('event_creator_id')
guests = ast.literal_eval(a.get('guest_state_list'))
else:
rtn = Event(
time=a.get('time'),
title=a.get('event_title'),
location=a.get('location_name')
)
rtn.uid = a.get('id')
rtn.creator_id = a.get('lightweight_event_creator').get('id')
guests = a.get('event_reminder_members').get('edges')
rtn.going = [m.get('node').get('id') for m in guests if m.get('guest_list_state') == "GOING"]
rtn.declined = [m.get('node').get('id') for m in guests if m.get('guest_list_state') == "DECLINED"]
rtn.invited = [m.get('node').get('id') for m in guests if m.get('guest_list_state') == "INVITED"]
return rtn
def graphql_to_message(message):
if message.get('message_sender') is None:
message['message_sender'] = {}
@@ -174,6 +214,7 @@ def graphql_to_user(user):
if user.get('profile_picture') is None:
user['profile_picture'] = {}
c_info = get_customization_info(user)
event_reminders = [graphql_to_event(event) for event in user['event_reminders']['nodes']]
return User(
user['id'],
url=user.get('url'),
@@ -188,7 +229,8 @@ def graphql_to_user(user):
own_nickname=c_info.get('own_nickname'),
photo=user['profile_picture'].get('uri'),
name=user.get('name'),
message_count=user.get('messages_count')
message_count=user.get('messages_count'),
event_reminders=event_reminders
)
def graphql_to_thread(thread):
@@ -210,6 +252,8 @@ def graphql_to_thread(thread):
else:
last_name = user.get('name').split(first_name, 1).pop().strip()
event_reminders = [graphql_to_event(event) for event in thread['event_reminders']['nodes']]
return User(
user['id'],
url=user.get('url'),
@@ -225,7 +269,8 @@ def graphql_to_thread(thread):
own_nickname=c_info.get('own_nickname'),
photo=user['big_image_src'].get('uri'),
message_count=thread.get('messages_count'),
last_message_timestamp=last_message_timestamp
last_message_timestamp=last_message_timestamp,
event_reminders=event_reminders
)
else:
raise FBchatException('Unknown thread type: {}, with data: {}'.format(thread.get('thread_type'), thread))
@@ -237,6 +282,7 @@ def graphql_to_group(group):
last_message_timestamp = None
if 'last_message' in group:
last_message_timestamp = group['last_message']['nodes'][0]['timestamp_precise']
event_reminders = [graphql_to_event(event) for event in group['event_reminders']['nodes']]
return Group(
group['thread_key']['thread_fbid'],
participants=set([node['messaging_actor']['id'] for node in group['all_participants']['nodes']]),
@@ -246,13 +292,15 @@ def graphql_to_group(group):
photo=group['image'].get('uri'),
name=group.get('name'),
message_count=group.get('messages_count'),
last_message_timestamp=last_message_timestamp
last_message_timestamp=last_message_timestamp,
event_reminders=event_reminders
)
def graphql_to_room(room):
if room.get('image') is None:
room['image'] = {}
c_info = get_customization_info(room)
event_reminders = [graphql_to_event(event) for event in room['event_reminders']['nodes']]
return Room(
room['thread_key']['thread_fbid'],
participants=set([node['messaging_actor']['id'] for node in room['all_participants']['nodes']]),
@@ -267,6 +315,7 @@ def graphql_to_room(room):
approval_requests = set(node.get('id') for node in room['thread_queue_metadata'].get('approval_requests', {}).get('nodes')),
join_link = room['joinable_mode'].get('link'),
privacy_mode = bool(room.get('privacy_mode')),
event_reminders=event_reminders
)
def graphql_to_page(page):