Raised This Month: $32 Target: $400
 8% 

Reddit Puns using Python


Post New Thread Reply   
 
Thread Tools Display Modes
Plugin Info:     Modification:   ALL        Category:   Fun Stuff       
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
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 12-12-2020 , 03:02   Re: Reddit Puns using Python
Reply With Quote #2

Fake UserAgent is required
https://pypi.org/project/fake-useragent/
easy-install pip3 install fake-useragent
__________________

Last edited by Relaxing; 12-12-2020 at 03:02.
Relaxing is offline
Alber9091
Veteran Member
Join Date: Jun 2014
Location: Karachi, Pakistan
Old 12-12-2020 , 11:30   Re: Reddit Puns using Python
Reply With Quote #3

Great !!
Alber9091 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-12-2020 , 17:36   Re: Reddit Puns using Python
Reply With Quote #4

If you're going to release a plugin, you should probably explain what in the world it is supposed to do. Also, what is the point of posting Python code?
__________________
fysiks is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 12-13-2020 , 19:09   Re: Reddit Puns using Python
Reply With Quote #5

The plugin fetches a pun from the reddit page r/oneliners using the inter-process communication with Python which gets the json format of rising posts and prints a random joke on the chat.
It's more likely a test plugin that can open a limitless amount of plugin ideas interacting with Python, such as checking the local wether, latest news, amxx updates, youtube api, sending email though a huge list of open source libraries.
__________________
Relaxing is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 12-13-2020 , 21:03   Re: Reddit Puns using Python
Reply With Quote #6

If it's a "test" plugin then you shouldn't release it as a new plugin submission, simply provide it with the post in Code Snippets/Tutorials (since that's what you just said it actually is). Also, you should provide your explanation with the plugin post so that people can understand what it does.
__________________
fysiks is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 02-01-2021 , 02:50   Re: Reddit Puns using Python
Reply With Quote #7

You're right, it doesn't belong to this subforum nor intented to be a plugin.
__________________
Relaxing is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-21-2021 , 04:56   Re: Reddit Puns using Python
Reply With Quote #8

Moved to tutorials section.
__________________
HamletEagle is offline
M Ali
Junior Member
Join Date: Dec 2021
Old 12-21-2021 , 02:47   Re: Reddit Puns using Python
Reply With Quote #9

@client.event
async def on_message(message):
if message.content.startswith(".meme"):
subreddit = reddit.subreddit("memes")
all_subs = []
top = subreddit.top(limit = 50)

for submission in top:
all_subs.append(submission)

random_sub = random.choice(all_subs)
name = random_sub.title
url = random_sub.url
em = discord.Embed(title = name, color = 0xFF5733)

em.set_image(url = url)
await message.channel.send(embed = em)
M Ali is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:56.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode