View Single Post
Plugin Info:     Modification:          Category:         
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 12-12-2020 , 02:58   Reddit Puns using Python
Reply With Quote #1

Test plugin of https://forums.alliedmods.net/showthread.php?t=329181
Command: /pun


Python script
Code:
import sys
import time
import datetime
import random
import requests
import socket
from fake_useragent import UserAgent

host = ''
port = 54500
url = "https://www.reddit.com/r/oneliners/randomrising/.json?kind=t3"

s = socket.socket()
s.bind((host, port))

s.listen(32)
conn, address = s.accept()
print("Connection from: " + str(address))

def memes():
    global data
    global conn

    ua = UserAgent()
    response = requests.get(url, headers={'User-agent': ua.random})

    if response.ok:
        output = response.json()['data']['children']
        
        for child in range(25):
            json = output[child]['data']
            
            if json:
                print (json['title'])
                conn.send(json['title'].encode())
                break

while True:
    data = conn.recv(256).decode()
    if not data:
        break
    print("from connected user: " + str(data))
    memes()
Plugin
Code:
#include <amxmodx> #include <sockets> #define LOCAL "localhost" #define PORT 54500 new s, error, data[128]; new dot[] = '.'; public plugin_init(){     register_clcmd("say /pun", "clcmd_pun"); } public plugin_cfg(){     s = socket_open(LOCAL, PORT, SOCKET_TCP, error);     if (!error){         set_task(0.1, "get_data", .flags="b");         data = "connected";         socket_send(s, data, charsmax(data));     } } public clcmd_pun(){     socket_send(s, dot, charsmax(dot)); } public get_data(){     if (socket_is_readable(s)){         socket_recv(s, data, charsmax(data));         client_print(0, print_chat, "%s", data);     } }
__________________

Last edited by Relaxing; 12-12-2020 at 02:58.
Relaxing is offline