项目作者: tuokri

项目描述 :
Rising Storm 2: Vietnam WebAdmin Python Interface
高级语言: Python
项目地址: git://github.com/tuokri/rs2wapy.git
创建时间: 2019-10-29T18:26:49Z
项目社区:https://github.com/tuokri/rs2wapy

开源协议:MIT License

下载


rs2wapy

Travis
Maintainability

Rising Storm 2: Vietnam WebAdmin Python Interface

Provides a Python interface for performing RS2 WebAdmin
tasks programmatically.

The library uses PycURL internally to communicate with RS2 WebAdmin.

Work in progress; interface will change!

Brief Usage Examples

This section contains some brief usage examples.
For more comprehensive tutorials check out the
examples repository.

Installation
  1. # Requires Python=>3.7
  2. pip install rs2wapy
Steam Web API key (optional)

Setting your Steam Web API key as an environment variable
allows rs2wapy to offer some extra functionality.

Unix:

  1. export STEAM_WEB_API_KEY="TOPSECRETKEY"

Windows:

  1. set STEAM_WEB_API_KEY="TOPSECRETKEY"
Quickstart

It is recommended to create a new WebAdmin account for
rs2wapy.

  1. from rs2wapy import RS2WebAdmin
  2. wa = RS2WebAdmin(
  3. username="AutoModerator",
  4. password="topsecret123",
  5. webadmin_url="http://localhost:8080/",
  6. )
Poll server ranked status and switch map automatically
  1. while True:
  2. if not wa.get_current_game().ranked:
  3. wa.post_chat_message("Unranked bug happened! Changing map in 5 seconds!")
  4. time.sleep(5)
  5. wa.change_map("VNTE-Resort")
  6. time.sleep(1)
Forward in-game chat to a Discord webhook with discord.py.
  1. import time
  2. from discord import RequestsWebhookAdapter
  3. from discord import Webhook
  4. from discord.utils import escape_markdown
  5. from discord.utils import escape_mentions
  6. from rs2wapy import RS2WebAdmin
  7. from rs2wapy.models import AllTeam
  8. from rs2wapy.models import BlueTeam
  9. from rs2wapy.models import RedTeam
  10. # Discord webhook info.
  11. webhook = Webhook.partial(
  12. id=123456,
  13. token="abcdefg",
  14. adapter=RequestsWebhookAdapter()
  15. )
  16. # Webadmin credentials.
  17. USERNAME = "Admin"
  18. PASSWORD = "adminpassword"
  19. URL = "http://127.0.0.1:8080/ServerAdmin"
  20. TEAM_TO_EMOJI = {
  21. BlueTeam: ":blue_square:",
  22. RedTeam: ":red_square:",
  23. AllTeam: ":white_square_button:",
  24. }
  25. TEAM_TO_TEAMNAME = {
  26. BlueTeam: "SOUTH",
  27. RedTeam: "NORTH",
  28. AllTeam: "ALL",
  29. }
  30. def get_team_emoji(team):
  31. try:
  32. return TEAM_TO_EMOJI[team]
  33. except KeyError:
  34. return "?"
  35. def get_team_name(team):
  36. try:
  37. return TEAM_TO_TEAMNAME[team]
  38. except KeyError:
  39. return "?"
  40. def main():
  41. webadmin = RS2WebAdmin(USERNAME, PASSWORD, URL)
  42. messages = []
  43. while True:
  44. try:
  45. messages.extend(webadmin.get_chat_messages())
  46. except Exception as e:
  47. print(f"error getting messages: {e}")
  48. print("attempting to reconnect...")
  49. webadmin = RS2WebAdmin(USERNAME, PASSWORD, URL)
  50. if messages:
  51. for message in messages:
  52. text = message.text
  53. sender = message.sender
  54. team = message.team
  55. team_name = get_team_name(team)
  56. team_emoji = get_team_emoji(team)
  57. # Prevent pinging @everyone from in-game chat
  58. # and other "funny" stuff.
  59. text = escape_markdown(text)
  60. text = escape_mentions(text)
  61. sender = escape_markdown(sender)
  62. sender = escape_mentions(sender)
  63. try:
  64. webhook.send(f"**{sender}** [{team_name}] {team_emoji}: {text}")
  65. except Exception as e:
  66. print(f"error sending message: {e}")
  67. messages = []
  68. time.sleep(3)

The above are just simple examples of how to use the library. In the future,
the library will be able to automate all tasks which RS2 WebAdmin offers.
You can check the status of currently implemented WebAdmin features here:
https://github.com/tuokri/rs2wapy/issues/9.