# Path: pogom/pgoapi/utilities.py
# def to_camel_case(value):
#     return ''.join(word.capitalize() if word else '_' for word in value.split('_'))
# 
# def get_time(ms=False):
#     if ms:
#         return int(time.time() * 1000)
#     else:
#         return int(time.time())
# 
# def get_format_time_diff(low, high, ms=True):
#     diff = (high - low)
#     if ms:
#         m, s = divmod(diff / 1000, 60)
#     else:
#         m, s = divmod(diff, 60)
#     h, m = divmod(m, 60)
# 
#     return (h, m, s)
# 
# class Rand48(object):
#     def __init__(self, seed):
#         self.n = seed
#     def seed(self, seed):
#         self.n = seed
#     def srand(self, seed):
#         self.n = (seed << 16) + 0x330e
#     def next(self):
#         self.n = (25214903917 * self.n + 11) & (2**48 - 1)
#         return self.n
#     def drand(self):
#         return self.next() / 2**48
#     def lrand(self):
#         return self.next() >> 17
#     def mrand(self):
#         n = self.next() >> 16
#         if n & (1 << 31):
#             n -= 1 << 32
#         return n
# 
# def long_to_bytes(val, endianness='big'):
#     """
#     Use :ref:`string formatting` and :func:`~binascii.unhexlify` to
#     convert ``val``, a :func:`long`, to a byte :func:`str`.
# 
#     :param long val: The value to pack
# 
#     :param str endianness: The endianness of the result. ``'big'`` for
#       big-endian, ``'little'`` for little-endian.
# 
#     If you want byte- and word-ordering to differ, you're on your own.
# 
#     Using :ref:`string formatting` lets us use Python's C innards.
#     """
# 
#     # one (1) hex digit per four (4) bits
#     width = val.bit_length()
# 
#     # unhexlify wants an even multiple of eight (8) bits, but we don't
#     # want more digits than we need (hence the ternary-ish 'or')
#     width += 8 - ((width % 8) or 8)
# 
#     # format width specifier: four (4) bits per hex digit
#     fmt = '%%0%dx' % (width // 4)
# 
#     # prepend zero (0) to the width, to zero-pad the output
#     s = unhexlify(fmt % val)
# 
#     if endianness == 'little':
#         # see http://stackoverflow.com/a/931095/309233
#         s = s[::-1]
# 
#     return s
# 
# def generate_location_hash_by_seed(authticket, lat, lng, acc=5):
#     first_hash = xxhash.xxh32(authticket, seed=HASH_SEED).intdigest()
#     location_bytes = d2h(lat) + d2h(lng) + d2h(acc)
#     loc_hash = xxhash.xxh32(location_bytes, seed=first_hash).intdigest()
#     return ctypes.c_int32(loc_hash).value
# 
# def generate_location_hash(lat, lng, acc=5):
#     location_bytes = d2h(lat) + d2h(lng) + d2h(acc)
#     loc_hash = xxhash.xxh32(location_bytes, seed=HASH_SEED).intdigest()
#     return ctypes.c_int32(loc_hash).value
# 
# def generate_request_hash(authticket, request):
#     first_hash = xxhash.xxh64(authticket, seed=HASH_SEED).intdigest()
#     req_hash = xxhash.xxh64(request, seed=first_hash).intdigest()
#     return ctypes.c_int64(req_hash).value
# 
# def f2i(float):
#     return struct.unpack('<Q', struct.pack('<d', float))[0]


import os
import re
import time
import base64
import random
import logging
import requests
import subprocess
import six
import ctypes
from google.protobuf import message
from importlib import import_module
from .protobuf_to_dict import protobuf_to_dict
from .exceptions import NotLoggedInException, ServerBusyOrOfflineException, ServerSideRequestThrottlingException, ServerSideAccessForbiddenException, UnexpectedResponseException, AuthTokenExpiredException, ServerApiEndpointRedirectException
from .utilities import to_camel_case, get_time, get_format_time_diff, Rand48, long_to_bytes, generate_location_hash_by_seed, generate_location_hash, generate_request_hash, f2i
from .protos import POGOProtos
from POGOProtos.Networking.Envelopes.RequestEnvelope_pb2 import RequestEnvelope
from POGOProtos.Networking.Envelopes.ResponseEnvelope_pb2 import ResponseEnvelope
from POGOProtos.Networking.Requests.RequestType_pb2 import RequestType
from POGOProtos.Networking.Envelopes.SignalAgglomUpdates_pb2 import SignalAgglomUpdates
from POGOProtos.Networking.Platform.Requests.SendEncryptedSignatureRequest_pb2 import SendEncryptedSignatureRequest
    def _build_main_request(self, subrequests, player_position=None):
        self.log.debug('Generating main RPC request...')

        request = RequestEnvelope()
        request.status_code = 2
        request.request_id = self.get_rpc_id()
        request.accuracy = random.choice((5, 5, 5, 10, 10, 30, 50, 65))

        if player_position:
            request.latitude, request.longitude, altitude = player_position

        # generate sub requests before SignalAgglomUpdates generation
        request = self._build_sub_requests(request, subrequests)

        ticket = self._auth_provider.get_ticket()
        if ticket:
            self.log.debug('Found Session Ticket - using this instead of oauth token')
            request.auth_ticket.expire_timestamp_ms, request.auth_ticket.start, request.auth_ticket.end = ticket
            ticket_serialized = request.auth_ticket.SerializeToString()

        else:
            self.log.debug('No Session Ticket found - using OAUTH Access Token')
            request.auth_info.provider = self._auth_provider.get_name()
            request.auth_info.token.contents = self._auth_provider.get_access_token()
            request.auth_info.token.unknown2 = 59
            ticket_serialized = request.auth_info.SerializeToString() #Sig uses this when no auth_ticket available

        if self._signal_agglom_gen:
            sig = SignalAgglomUpdates()


# Path: pogom/pgoapi/utilities.py
# def to_camel_case(value):
#     return ''.join(word.capitalize() if word else '_' for word in value.split('_'))
# 
# def get_time(ms=False):
#     if ms:
#         return int(time.time() * 1000)
#     else:
#         return int(time.time())
# 
# def get_format_time_diff(low, high, ms=True):
#     diff = (high - low)
#     if ms:
#         m, s = divmod(diff / 1000, 60)
#     else:
#         m, s = divmod(diff, 60)
#     h, m = divmod(m, 60)
# 
#     return (h, m, s)
# 
# class Rand48(object):
#     def __init__(self, seed):
#         self.n = seed
#     def seed(self, seed):
#         self.n = seed
#     def srand(self, seed):
#         self.n = (seed << 16) + 0x330e
#     def next(self):
#         self.n = (25214903917 * self.n + 11) & (2**48 - 1)
#         return self.n
#     def drand(self):
#         return self.next() / 2**48
#     def lrand(self):
#         return self.next() >> 17
#     def mrand(self):
#         n = self.next() >> 16
#         if n & (1 << 31):
#             n -= 1 << 32
#         return n
# 
# def long_to_bytes(val, endianness='big'):
#     """
#     Use :ref:`string formatting` and :func:`~binascii.unhexlify` to
#     convert ``val``, a :func:`long`, to a byte :func:`str`.
# 
#     :param long val: The value to pack
# 
#     :param str endianness: The endianness of the result. ``'big'`` for
#       big-endian, ``'little'`` for little-endian.
# 
#     If you want byte- and word-ordering to differ, you're on your own.
# 
#     Using :ref:`string formatting` lets us use Python's C innards.
#     """
# 
#     # one (1) hex digit per four (4) bits
#     width = val.bit_length()
# 
#     # unhexlify wants an even multiple of eight (8) bits, but we don't
#     # want more digits than we need (hence the ternary-ish 'or')
#     width += 8 - ((width % 8) or 8)
# 
#     # format width specifier: four (4) bits per hex digit
#     fmt = '%%0%dx' % (width // 4)
# 
#     # prepend zero (0) to the width, to zero-pad the output
#     s = unhexlify(fmt % val)
# 
#     if endianness == 'little':
#         # see http://stackoverflow.com/a/931095/309233
#         s = s[::-1]
# 
#     return s
# 
# def generate_location_hash_by_seed(authticket, lat, lng, acc=5):
#     first_hash = xxhash.xxh32(authticket, seed=HASH_SEED).intdigest()
#     location_bytes = d2h(lat) + d2h(lng) + d2h(acc)
#     loc_hash = xxhash.xxh32(location_bytes, seed=first_hash).intdigest()
#     return ctypes.c_int32(loc_hash).value
# 
# def generate_location_hash(lat, lng, acc=5):
#     location_bytes = d2h(lat) + d2h(lng) + d2h(acc)
#     loc_hash = xxhash.xxh32(location_bytes, seed=HASH_SEED).intdigest()
#     return ctypes.c_int32(loc_hash).value
# 
# def generate_request_hash(authticket, request):
#     first_hash = xxhash.xxh64(authticket, seed=HASH_SEED).intdigest()
#     req_hash = xxhash.xxh64(request, seed=first_hash).intdigest()
#     return ctypes.c_int64(req_hash).value
# 
# def f2i(float):
#     return struct.unpack('<Q', struct.pack('<d', float))[0]


# Path: pogom/pgoapi/rpc_api.py
import os
import re
import time
import base64
import random
import logging
import requests
import subprocess
import six
import ctypes
from google.protobuf import message
from importlib import import_module
from .protobuf_to_dict import protobuf_to_dict
from .exceptions import NotLoggedInException, ServerBusyOrOfflineException, ServerSideRequestThrottlingException, ServerSideAccessForbiddenException, UnexpectedResponseException, AuthTokenExpiredException, ServerApiEndpointRedirectException
from .utilities import to_camel_case, get_time, get_format_time_diff, Rand48, long_to_bytes, generate_location_hash_by_seed, generate_location_hash, generate_request_hash, f2i
from .protos import POGOProtos
from POGOProtos.Networking.Envelopes.RequestEnvelope_pb2 import RequestEnvelope
from POGOProtos.Networking.Envelopes.ResponseEnvelope_pb2 import ResponseEnvelope
from POGOProtos.Networking.Requests.RequestType_pb2 import RequestType
from POGOProtos.Networking.Envelopes.SignalAgglomUpdates_pb2 import SignalAgglomUpdates
from POGOProtos.Networking.Platform.Requests.SendEncryptedSignatureRequest_pb2 import SendEncryptedSignatureRequest

    def _build_main_request(self, subrequests, player_position=None):
        self.log.debug('Generating main RPC request...')

        request = RequestEnvelope()
        request.status_code = 2
        request.request_id = self.get_rpc_id()
        request.accuracy = random.choice((5, 5, 5, 10, 10, 30, 50, 65))

        if player_position:
            request.latitude, request.longitude, altitude = player_position

        # generate sub requests before SignalAgglomUpdates generation
        request = self._build_sub_requests(request, subrequests)

        ticket = self._auth_provider.get_ticket()
        if ticket:
            self.log.debug('Found Session Ticket - using this instead of oauth token')
            request.auth_ticket.expire_timestamp_ms, request.auth_ticket.start, request.auth_ticket.end = ticket
            ticket_serialized = request.auth_ticket.SerializeToString()

        else:
            self.log.debug('No Session Ticket found - using OAUTH Access Token')
            request.auth_info.provider = self._auth_provider.get_name()
            request.auth_info.token.contents = self._auth_provider.get_access_token()
            request.auth_info.token.unknown2 = 59
            ticket_serialized = request.auth_info.SerializeToString() #Sig uses this when no auth_ticket available

        if self._signal_agglom_gen:
            sig = SignalAgglomUpdates()


            sig.location_hash_by_token_seed = generate_location_hash_by_seed(ticket_serialized, request.latitude, request.longitude, request.accuracy)