Hack Forums
[Python] Grabbing text by line - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Grabbing text by line (/showthread.php?tid=6030790)



Grabbing text by line - JJLAWTON4 - Dec 6, 2019

I am in the middle of making a discord bot. I am wanting to go get to a pastebin link and grab some account info and then send that to the user. BUT I want to grab like one set of credentials at a time. Basically by line and ideally random aswell. I know how to do all but the part of randomizing what its grabbing aswell as having it grabbed by one line


RE: Grabbing text by line - Charger - Dec 6, 2019

Make the request
Read the response
Split the text at \r\n
Each result from above is a line

Return splitRes[Math.random(0,len(splitRes))]


RE: Grabbing text by line - incognito04 - Dec 6, 2019

PHP Code

<?php 
# -*- coding: utf-8 -*-

import typing
import asyncio

from random import shuffle
from aiohttp import ClientSession

class API:
    
def __init__(self):
        
self._session = ClientSession()

    
async def request(self, method: str, url: str, **kwargs) -> str:
        
async with self._session.request(method, url, **kwargs) as request:
            return 
await request.text()

    
async def close(self):
        
await self._session.close()

    
async def __aenter__(self) -> 'API':
        return 
self

    async def __aexit__
(self, *args):
        
await self.close()


class 
Pastebin(API):
    
def __init__(self):
        
super().__init__()

    
async def raw(self, paste: str) -> str:
        return 
await self.request("GET", f"https://pastebin.com/raw/{paste}")


async def shuffled_data(paste: str) -> typing.List[str]:
    
async with Pastebin() as pastebin:
        
data = (await pastebin.raw(paste)).splitlines()
    
shuffle(data)
    return 
data

async def main
():
    
paste = input("Paste => ")
    print(
await shuffled_data(paste))
    
if 
__name__ == "__main__":
    
asyncio.run(main()) 



RE: Grabbing text by line - JJLAWTON4 - Dec 7, 2019

I eneded up sorting it out. But basically I just used the random module and split it into a list to randomly grab one