Skip to main content

bf6 portal web grpc as python package

Project description

Battlefield Portal web-grpc

This npm and python package can be used to directly call the https://portal.battlefield.com/ api. we're making this public since you can read the javascript of the website and figure this out yourself easily anyway, but we want to make sure only 1 github repo has to be kept in sync with the api and the rest that uses it just has to update a package and a few code changes to still have it work.

https://www.npmjs.com/package/bfportal-grpc

https://pypi.org/project/bfportal-grpc/

example

import { CommunityGamesClient, communitygames } from 'bfportal-grpc'

const communityGames = new CommunityGamesClient(
  'https://kingston-prod-wgw-envoy.ops.dice.se',
  null
)
const metadata = {
  'x-dice-tenancy': 'prod_default-prod_default-kingston-common',
  'x-gateway-session-id': sessionId,
  'x-grpc-web': '1',
  'x-user-agent': 'grpc-web-javascript/0.1',
}

const request = new communitygames.GetPlaygroundRequest()
request.setPlaygroundid(testPlayground)
const response = await communityGames.getPlayground(request, metadata)
const modRules = response
  .getPlayground()
  ?.getOriginalplayground()
  ?.getModrules()
  ?.getCompatiblerules()
  ?.getRules()
if (modRules instanceof Uint8Array) {
  console.log(new TextDecoder().decode(modRules))
}
const playgroundName = response
  .getPlayground()
  ?.getOriginalplayground()
  ?.getName()

the proto files are accessable directly via "node_modules/bfportal-grpc/proto/communitygames.proto" to for example decode to json:

import { load } from 'protobufjs'

// use reponse from previous example
const root = await load('node_modules/bfportal-grpc/proto/communitygames.proto')
const AwesomeMessage = root.lookupType(
  'web.communitygames.PlaygroundInfoResponse'
)
const decoded = AwesomeMessage.decode(response.serializeBinary())
const json_str = JSON.stringify(decoded, null, 4)

non-async example

import { CommunityGamesClient, communitygames } from 'bfportal-grpc'

const communityGames = new CommunityGamesClient(
  'https://kingston-prod-wgw-envoy.ops.dice.se',
  null
)
const metadata = {
  'x-dice-tenancy': 'prod_default-prod_default-kingston-common',
  'x-gateway-session-id': sessionId,
  'x-grpc-web': '1',
  'x-user-agent': 'grpc-web-javascript/0.1',
}

const request = new communitygames.GetPlaygroundRequest()
request.setPlaygroundid('bbe433c0-13fa-11ed-bc32-24a8c2c0764e')
const call = communityGames.getPlayground(
  request,
  metadata,
  (_err: grpcWeb.Error, response: communitygames.PlaygroundInfoResponse) => {
    // console.log("err:", _err)
    var modRules = response
      .getPlayground()
      ?.getOriginalplayground()
      ?.getModrules()
      ?.getCompatiblerules()
      ?.getRules()
    if (modRules instanceof Uint8Array) {
      console.log(new TextDecoder().decode(modRules))
    }

    load(
      'node_modules/bfportal-grpc/proto/communitygames.proto',
      function (err, root) {
        if (err) throw err
        if (root == undefined) return

        const AwesomeMessage = root.lookupType(
          'web.communitygames.PlaygroundInfoResponse'
        )

        let decoded = AwesomeMessage.decode(response.serializeBinary())
        fs.writeFile(
          'test.json',
          JSON.stringify(decoded, null, 4),
          function (err: any) {
            if (err) {
              console.log(err)
            }
          }
        )
      }
    )
  }
)

python

for python you can use the 'sonora' package to do grpc-web

import asyncio
import sonora.aio
from bfportal_grpc import communitygames_pb2, communitygames_pb2_grpc, access_token, authentication_pb2, authentication_pb2_grpc

async def main():
    cookie = access_token.Cookie(sid="", remid="")
    token = await access_token.getBf2042GatewaySession(cookie)

    async with sonora.aio.insecure_web_channel(
        f"https://kingston-prod-wgw-envoy.ops.dice.se"
    ) as channel:
        stub = authentication_pb2_grpc.AuthenticationStub(channel)
        auth_response: authentication_pb2.AuthResponse = await stub.viaAuthCode(authentication_pb2.AuthRequest(platform=1, authCode=token, redirectUri='https://portal.battlefield.com/'), metadata=(
            ('x-dice-tenancy', 'prod_default-prod_default-kingston-common'),
            ('x-grpc-web', '1'),
            ('x-user-agent', 'grpc-web-javascript/0.1')
        ))

        stub = communitygames_pb2_grpc.CommunityGamesStub(channel)
        response: communitygames_pb2.PlaygroundInfoResponse = await stub.getPlayground(communitygames_pb2.GetPlaygroundRequest(playgroundId="10992a10-461a-11ec-8de0-d9f491f92236"), metadata=(
            ('x-dice-tenancy', 'prod_default-prod_default-kingston-common'),
            ('x-gateway-session-id', auth_response.sessionId),
            ('x-grpc-web', '1'),
            ('x-user-agent', 'grpc-web-javascript/0.1')
        ))

        print(response.playground.originalPlayground.name)

if __name__ == "__main__":
    asyncio.run(main())

current build method from proto to javascript via python

needs proto-compile, which can be installed with:

pip3 install proto-compile

and build with:

proto-compile --clear-output-dirs --verbosity=1 ./proto ./src/proto grpc-web --grpc_web_out_options="import_style=typescript,mode=grpcweb"

building for python requires grpcio-tools, which can be installed with:

pip3 install grpcio-tools

and build with:

poetry run compile-proto

python package used: https://github.com/romnn/proto-compile

Pushing your changes

package versions can be made with npm run build and npm version patch git push --tags origin main to release. for python poetry build.

example library used: https://github.com/tomchen/example-typescript-package

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bfportal_grpc_bf6-0.1.8.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bfportal_grpc_bf6-0.1.8-cp313-cp313-macosx_26_0_arm64.whl (39.3 kB view details)

Uploaded CPython 3.13macOS 26.0+ ARM64

File details

Details for the file bfportal_grpc_bf6-0.1.8.tar.gz.

File metadata

  • Download URL: bfportal_grpc_bf6-0.1.8.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.9 Darwin/25.1.0

File hashes

Hashes for bfportal_grpc_bf6-0.1.8.tar.gz
Algorithm Hash digest
SHA256 4d5dc8064ced5a56cd520540d3dce07b5b8473988d9c834b18bbcb6787d38a28
MD5 d742d078c5a14df5e5e869583088ea52
BLAKE2b-256 2c7f92f2de0b5c269a664e44e865e89c76b83646369ac0ddf58657babccf50d6

See more details on using hashes here.

File details

Details for the file bfportal_grpc_bf6-0.1.8-cp313-cp313-macosx_26_0_arm64.whl.

File metadata

File hashes

Hashes for bfportal_grpc_bf6-0.1.8-cp313-cp313-macosx_26_0_arm64.whl
Algorithm Hash digest
SHA256 71df4e02496f05f143073657e423fc764d1db3ccbb723133d5e5dec00722b9e6
MD5 e8967f2f5f3eaedf8140da0b4102da8e
BLAKE2b-256 6518d7b9fdf1d728f766e2ffb416c58c3a12dece6195c6281210e9f47509f5df

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page