Skip to content

Instantly share code, notes, and snippets.

@Skarlett
Created April 27, 2018 14:38
Show Gist options
  • Save Skarlett/bd5bb4008fd307522666642427a19873 to your computer and use it in GitHub Desktop.
Save Skarlett/bd5bb4008fd307522666642427a19873 to your computer and use it in GitHub Desktop.

pTemplates Plug&Play Module


Legend:

  • 1.0 - Basics / Prerequisite Knowledge
    • 1.0.1 Requirements
    • 1.1 Creating your first template
      • 1.1.1 Instance Settings
      • 1.1.2 Class Settings
      • 1.1.3 Predefined variables
  • 2.0 Extending built-ins to customize
    • 2.1 EventManager
    • 2.2 Namespaces
    • 2.3 IDEngine
    • 2.4 TimeBelt
    • 2.5 ThumbMorpher

Basics 1.0

ptemplates module is used independently by Armada, and is not a system library. (import ptemplates will not work.) ptemplates uses a plug & play system, where __init__ is ptemplates and you redefine default objects. each seperate file inside the folder src/ptemplates besides __init__ is considered a "template."

The idea of using ptemplates is the ability to scope, customize, configure, and deploy dynamic bots. Many application settings you will notice are missing here (Proxies, database structure, etc). This in contract of our scoping and minimal programming. Proxies, database queries, are all handled outside the template by the core application.

ptemplates uses and abuses play & plug abilities, writing your template and placing it into src/ptemplates/ and if present in there, and defined in the template USE = True it will automatically load and be used.

Requirements (1.0.1)

Each template has a simple base of requirements to be loaded into memory for it to be used.

  • Must have defined a function named setup and take one argument
  • each Template Object must have a UUID as an int and must be unique from all other Template UUIDs.

Each template Class is kept in memory (under armada.database.loaded), and holds subvalues for timestamps and other stats. Each template's main purpose is to produce a DefaultEventManager object that will be then pushed into a seperate thread outside its scope.

1.1 Creating your first template

Creating templates can be a tiny amount of lines

import __init__

USE = True # Autoload and use

class My_Template(__init__.DummyTemplate):
  UUID = 10 # UUID no other template has
  def create(self):
    self.name = "God"
    self.password = "12345_unguessable"
    self.auth_methods = [self.login] # Force no register

def setup(armada):
  armada.loaded.add(My_Template)

But there's much configuration that can be done to each template.

Instance settings (1.1.1)

These settings should be modifed by defining the method self.create. These settings should be seen as "for each bot created".

  • name - EventManager's username (str)
  • password - EventManager's password (str) | Default = Random string 12-20 Chars
  • auth_methods - Available authenification methods [ self.login | self.register ] (list) Default = [self.login, self.register]
  • mini - mini bio for EventManager (str)
  • full - full bio for EventManager (str)
  • namelist - if not name defined, this checks against a list of names
  • email - email used if self.register called
  • age - age that appears on EventManager
  • left - position for thumbnail (x) (int)
  • top - position for thumbnail (y) (int)
  • bottom - position for thumbnail (ts) (int)
  • sex - gender representation for EventManager (str) Requires (F|f|M|m)
  • location - EventManager's location representation (zipcode | Country Abrv )
  • picture - Filepath to jpg/gif/png or raw byte data
  • namespace - Use file pool object (define this in onInit) (self.NameSpace)
  • image_engine - function used for finding positional arguments for thumbnail.

Class settings (1.1.2)

These settings should be seen as globals for each Template, and are defined outside of the instance.

  • max_alive - Max amount of template spawns (int) | Default=100
  • alivefor - Each bot's alive time (int) while 0 = infinity | Default=45min
  • ignore_warnings - Don't Prompt warnings (bool) | Default=False
  • save_in_db - Allow spawns to be saved and used again | Default=True
  • write_on_register - Write template information (mini, picture, etc) on registeration | Default = False
  • write_on_login - Writes template information (mini, picture, etc) on login | Default = False
  • use_timebelt - uses self.TimeBelt Class (bool) | Default = True
  • EventManager (DefaultEventManager) (Class)
  • NameSpace (FileSystemNameSpace) (Class)
  • TimeBelt (TimeBelt) (Class)
  • ThumbMorpher (Class)
  • IDEngine (Class)

Predefined variables (1.1.3)

  • db (armada.Database) (Instance)
  • password (str) (see 1.1.1)
  • auth_methods (list) (see 1.1.1)
  • image_engine (DLIB_REAL_ADULT_FACE_ENGINE) (func)

Extending Built-ins to customize (2.0)

Everything inside the template is basically moddable. Everything can rewritten inside of its instance in any method. To take true advantage of python's inheritance and class settings. And considering scoping in mind, I made it so you can redefine even its own children objects before even __init__ occurs.

Lets talk more about this, and its other Objects you can use.

Every Class and Instance Setting is moddable, and you may use the references below for Class inheritance modifications.

DefaultEventManager (2.1)

DummyTemplate.EventManager

A good example of doing this is...

import __init__

class Bot(__init__.DefaultEventManager):
  # Remember ch.RoomManager? Well this is it.
  # Slightly modified by DefaultEventManager
  def onReady(self, pm):
    if pm._connected:
      print(self.name+' is online')

class My_Template(__init__.DummyTemplate):
  EventManager = Bot

This is the RoomManager object from ch.py, and should be referenced for their Documentation.

Noted modifications:

  • Depreciated (DefaultEventManager) OnPMPing and replaced with OnTick

  • Depreciated (DefaultEventManager) OnPMConnect and replaced with OnReady

  • Depreciated (DefaultEventManager) OnPMDisconnect and replaced with OnDie

  • Depreciated (RoomManager) all Room methods. And any methods that use or driven from Room methods should be considered unusable.

  • Appended (RoomManager) RoomManager.pm.profile for inside EventBased methods

  • Appended (RoomManager) (proxy, header, auth) keyword arguments to RoomManager parameters

    • proxy (ch.Proxy) Handles SOCKS5/4 HTTP proxy requests
    • header (dict) Passes headers in HTTP requests
    • Pre-Auth (str) You don't have to reauthenticate anymore.
  • Appended (RoomManager) HTTP.py lib for handling HTTP requests

FileSystemNameSpace (2.2)

DummyTemplate.NameSpace

This object is has to be spawned in DummyTemplate.onInit and is spawned by

# Unexpected behavior will happen if not bound to self.namespace
self.namespace = self.NameSpace(self.db, 'Media_directory_name')

This object's job is simply to abstract from developer's pain of dealing with a database and an active file directory. It uses the database, to find epacks data, and then gives that data using the method dispense()

vids, imgs = self.namespace.dispense()

Its class is held inside the template as NameSpace, and can be modded by doing the following...

class New_NameSpace(__init__.FileSystemNameSpace):
  # Define your new methods
  pass

class My_Template(__init__.DummyTemplate):
  NameSpace = New_NameSpace
  def onInit(self):
    self.namespace = self.NameSpace(self.db, 'MyFavoriteDirName')

TimeBelt (2.3)

DummyTemplate.TimeBelt

This Class is simply resposible to scale and adjust each template's cooloff time by asserting the hour of the day, and if its a holiday. It uses simple maths, (multiplication by decimials) against the original cooloff time provided by the template, and contains 24 items (floats), called a TimeSheet. Each item in the TimeSheet is called by the index of whatever hour it is.

modification of this class would be:

class New_TimeBelt(__init__.TimeBelt):
  def __init__(self):
    TimeBelt.__init__(self)
  
    self._timezone = -6 # chicago timezone | center US timezone
    self._friday = self._sat = \
    self._mon = self._tues = self._wed = \
    self._thurs = self._sun = TimeSheet(
      0.6, 0.7, 0.7, 0.8, 1.0, 1.4,  # 12am - 6am
      1.2, 1.2, 1.1, 1.0, 1.0, 1.0,  # 7am - 12pm
      1.0, 1.0, 0.9, 0.9, 0.9, 0.9,  # 1pm - 6pm
      1.0, 0.9, 0.8, 0.7, 0.7, 0.6   # 7pm - 12am
    )

class My_Template(__init__.DummyTemplate):
  TimeBelt = New_TimeBelt    

####ThumbnailMorpher (2.4) DummyTemplate.ThumbMorpher

This is a rather simple extension, it attempts to an equation on the thumbnail chords. And creating your own and plugging it in is basically the same proceedure as everything else.

 class New_ThumbMorpher(__init__.ThumbnailMorpher):
    def morph(self, left, top, bottom):
      return left+1, top+1, bottom+1 # math stuff
 
 class My_Template(__init__.DummyTemplate):
   ThumbMorpher = New_ThumbMorpher

IdentityEngine (2.5)

DummyTemplate.IDEngine

This is also just a polish extension like ThumbMorpher, it changes the headers for each EventManager instance.

class New_IDEngine(__init__.IndentityEngine):
  def give(self):
    return {'User-Agent': 'ouch security'}

class My_Template(__init__.DummyTemplate):
  IDEngine = New_IDEngine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment