Giter Site home page Giter Site logo

mfcauto.py-1's Introduction

mfcauto.py

mfcauto.py is a Python port of my MFCAuto NodeJS module. Why? Primarily because I like Python and wanted an excuse to refresh my Python skills. Both packages are being actively maintained (as of April 16, 2016) so feel free to use whichever suits your scenario/tastes, although at the moment the NodeJS package is more feature complete and stable.

Initially the API surface will map very closely to that for NodeJS, but my intent is to update the API and make it more Pythonic over time. This means a lot of potentially breaking changes. See DIFFERENCES.rst for a partial list of known differences between the two packages.

Setup

This package requires Python 3.5 or later.

# Install or upgrade to the latest version of mfcauto.py
$ pip install --upgrade git+https://github.com/Damianonymous/mfcauto.py@master

To install a specific commit or tag, refer to the pip documentation.

Examples

Query a model's details

SimpleClient is an abstraction that uses the default asyncio event loop. With SimpleClient, connect() is a blocking call that will not return until the connection to MFC's chat servers has been lost. So it should be the last call in your script.

Since connect() is blocking, to do something after connecting a SimpleClient you need to add a listener for the FCTYPE.CLIENT_CONNECTED event on the client instance before connecting. SimpleClient also emits an FCTYPE.CLIENT_MODELSLOADED event when the model list has finished populating from the server.

from mfcauto import SimpleClient, FCTYPE

client = SimpleClient()

def handler(packet):
    print(packet)
    client.disconnect()

client.on(FCTYPE.USERNAMELOOKUP, handler)
client.on(FCTYPE.CLIENT_CONNECTED, lambda: client.tx_cmd(FCTYPE.USERNAMELOOKUP, 0, 20, 0, 'AspenRae'))

client.connect(False)

There is a much more complete version of this example in the tests folder, userLookup.py, which can be run directly from there.

Using Client with your own event loop

SimpleClient is enough for the vast majority of cases. But if you need to do something more advanced like running multiple Clients simultaneously or running a Client on a different thread, you can manually manage your event loop like so...

import asyncio
import mfcauto

loop = asyncio.get_event_loop()
client = mfcauto.Client(loop)

###########################################
# Hook up any event handlers on the client object
# and/or mfcauto.Model objects here.
###########################################

loop.run_until_complete(client.connect())
loop.run_forever()
loop.close()

See the 'tests' folder for several other examples.

mfcauto.py-1's People

Contributors

damianonymous avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.