[ENG/ITA] Someone on Hive Is Going to Use My Last Python Project!

in #hive-1466209 days ago


La versione italiana si trova sotto quella inglese

The italian version is under the english one


Someone Is Going to Use My Last Python Project on Hive!

I will start right away with a small premise: I will not say who is the “someone” to whom the title refers.

Why? Because he is the first user who has supported me on Hive, who always gives me great tips whenever I need them and who has never stopped being an example for me and for the whole Community. And I am not only talking about the Italian one, but about Hive as a whole.

The help he has given me, and still gives me, is so much and it certainly will not be with a script that I will feel like I have repaid him.

So I don't want to say his name because to me it would feel like I'm trying to give the impression that I am doing him a favor, when the truth is that I am just trying to give back 1% of the support he has given me so far.


Here's why this script

If there's one thing I believe in, it's that in life we need to give back the good we receive: it feels wonderful to do so, and it feels even better when we do it towards someone we know deserves it.

Therefore, over the past month, I have been actively working with one goal in mind: to create something that would make this person's life easier, helping him save time when doing some of his activities here on Hive.

More specifically, knowing that he runs a contest in the Olio di Balena community and that in order to do so he is forced to check the requirements of every possible participant, I tried to create a script that could automatically perform this tasks for him.

To do this, I took advantage of the knowledge I have acquired over the past few months, although the users with whom I have engaged most would get their hands in their hair seeing how messy the code I wrote is 🤣

The problem was that every time I thought I was done something would pop up that didn't work... I'd fix it and here's another thing that didn't go the way it was supposed to...

And then I don't want to rethink about how difficult it was trying to figure out how some of the methods (is that how they are called?) of the Hive API worked: without clear instructions in many cases the only solution was to try and try again, hoping that something at some point would give the desired result!

Finally, however, after two days spent almost entirely in front of the PC - I happened to have a completely free weekend, a more unique than rare event - I did it!


Success!

The script works! The generated results are reliable and require, on average, to wait less than 30 seconds.

The reliability of the result has already been evaluated in the last contest held on Whale Oil, and this means that, soon, the user for whom this tool is designed will be able to start using it, saving time that he can then use for other activities - or to have a break!

The fulfillment this gives me is huge; to have created something that will be used by someone is beautiful! To think that my first project that will have a real practical application will help someone who does so much for all of Hive gives me even more happiness and motivates me to do even better.

As I have done in the past I tag some users, i.e. @felixxx and @gamer00, who are really helping me a lot with this exploration of my coding world: although their skills are several times vaster than mine, they had the patience to answer several of my doubts. The scripts that they share on Hive have been crucial to better understand how the Hive APIs work, and if it were not for them, I would never have been able to figure out the (numerous) problems I encountered creating this script.


Some snippets

Unlike the previous times, I won't share the whole code today, mainly because it is quite long and I don't want to clog up these posts every time with two kilometers of code 🤣

So here are the functions that deal with getting the information I need from the Hive APIs: the one to get the custom json operations is the one that drove me crazy, because of a paging system that didn't want to cooperate 😅


# Get posts published in the target community
def get_community_posts(url, last_author, last_permlink, session: requests.Session):
    data = f'{{"jsonrpc":"2.0", "method":"bridge.get_ranked_posts", "params":{{"sort":"created","tag":"hive-146620","observer":"", "limit": 100,  "start_author":"{last_author}", "start_permlink":"{last_permlink}"}}, "id":1}}'
    response = get_response(data, url, session)
    posts = response.json()["result"]
    return posts


# Get replies from target author
def get_replies(url, author, session: requests.Session):
    data = f'{{"jsonrpc":"2.0", "method":"bridge.get_account_posts", "params":{{"sort":"comments", "account": "{author}", "limit": 100}}, "id":1}}'
    response = get_response(data, url, session)
    replies = response.json()["result"]
    return replies


# Get all custom operations from target account, poll votes included
def get_custom_json(author, num, url, session: requests.Session):
    data = f'{{"jsonrpc":"2.0", "method":"condenser_api.get_account_history", "params":["{author}", {num}, 1000, 262144], "id":1}}'
    response = get_response(data, url, session)
    custom_json = response.json()["result"]
    return custom_json


I know, what follows is an infinite while loop, but I had really run out of energy today at the end and couldn't find a more elegant way to search through a user's custom json operations until I find a vote in a poll in the time frame I'm interested in (3 weeks)...


# Check if target account voted in one of the 3 last polls
def has_voted_poll(last_poll, author, url, session: requests.Session):
    today = datetime.now()
    three_weeks_ago = today - timedelta(days=21, hours=23)
    num = -1
    while True:
        custom_json = get_custom_json(author, num, url, session)
        for op in custom_json:
            link = op[1]["op"][1]["id"]
            if link in last_poll:
                return True
        timestamp = custom_json[0][1]["timestamp"]
        timestamp_formatted = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S")
        if timestamp_formatted < three_weeks_ago:
            return False
        num = custom_json[0][0]


A small addition to the function that takes care of the word count of a post: now the text is first cleaned up by hyperlinks and images, so that the result is not skewed.


# Clean text before counting words
def clean_markdown(md_text):
    # Remove images
    md_text = re.sub(r"!\[.*?\]\(.*?\)", "", md_text)

    # Remove hyperlinks
    md_text = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", md_text)

    return md_text


And then if anyone is interested in the full code I am of course willing to share it!


cover made with Bing AI and edited with GIMP

to support the #OliodiBalena community, @balaenoptera is 3% beneficiary of this post


If you've read this far, thank you! If you want to leave an upvote, a reblog, a follow, a comment... well, any sign of life is really much appreciated!


Versione italiana

Italian version


Qualcuno su Hive Utilizzerà il Mio Ultimo Progetto in Python!

Parto subito con una piccola premessa: non dirò chi è il "qualcuno" a cui si riferisce il titolo.

Perchè? Perchè si tratta del primo utente che mi ha supportato su Hive, che da sempre mi dà ottimi consigli ogni volta che ne ho bisogno e che non ha mai smesso di essere di esempio per me e per tutta la Community. E non parlo solo di quella italiana, ma di tutta Hive.

L'aiuto che lui mi ha dato, e ancora mi dà, è tantissimo e non sarà certo con uno script che mi sentirò di averlo ricambiato.

Non voglio quindi dire il suo nome perchè mi sembrerebbe di voler dare l'impressione che gli sto facendo un favore, quando la verità è che sto solo cercando di ricambiare l'1% del supporto che lui mi ha dato sino ad oggi.


Ecco il perchè di questo script

Se c'è una cosa in cui credo è che nella vita bisogna restituire il bene che riceviamo: farlo è bellissimo, ed è ancora più bello quando lo si fa nei confronti di qualcuno che sappiamo meritarlo.

In quest'ultimo mese ho perciò lavorato attivamente con un solo obiettivo in testa: creare qualcosa che potesse semplificare la vita di questa persona, aiutandola a risparmiare tempo quando svolge qualcuna delle sue attività qui su Hive.

In particolare, sapendo che gestisce un contest nella community Olio di Balena e che per farlo è costretto a verificare i requisiti di ogni possibile partecipante, ho cercato di creare uno script che potesse effettuare in maniera automatica questi controlli al suo posto.

Per farlo ho sfruttato le conoscenze che ho acquisito in questi mesi, anche se gli utenti con i quali mi sono maggiormente confrontato si metterebbero le mani nei capelli vedendo che confusione c'è nel codice che ho scritto 🤣

Il problema è stato che ogni volta che pensavo di aver finito saltava fuori qualcosa che non funzionava... lo risolvevo ed ecco un'altra cosa non andava come doveva...

E non voglio poi ripensare a quanto è stato difficile cercare di capire il funzionamento di alcuni metodi (si chiamano così?) delle API di Hive: senza spiegazioni chiare in molti casi l'unica soluzione è stata provare e riprovare, sperando che qualcosa ad un certo punto desse il risultato sperato!

Alla fine, però, dopo due giorni passati quasi interamente davanti al pc - mi è capitato un weekend completamente libero, evento più unico che raro - ce l'ho finalmente fatta!


Successo!

Lo script funziona! I risultati generati sono attendibili e richiedono, di media, meno di 30 secondi di attesa.

L'affidabilità del risultato è già stata valutata nell'ultimo contest organizzato su Olio di Balena e ciò significa che, a breve, l'utente a cui è destinato questo strumento potrà iniziare ad usarlo, risparmiando del tempo che così potrà sfruttare per altre attività - o per riposarsi!

La soddisfazione che ciò mi dà è enorme; aver creato qualcosa che verrà utilizzato da qualcuno è bellissimo! Pensare poi che il mio primo progetto che avrà un'applicazione pratica reale aiuterà una persona che fa tanto per tutta Hive mi dà ancora più felicità e mi sprona a voler fare ancora meglio.

Come ho già fatto in passato taggo alcuni utenti, come @felixxx e @gamer00, che mi stanno aiutando davvero tanto in questa mia esplorazione del mondo della programmazione: sebbene le loro capacità siano parecchio più ampie delle mie, hanno avuto la pazienza di rispondere a diversi dei miei dubbi. Gli script poi che condividono su Hive sono stati essenziali per capire meglio il funzionamento delle API di Hive e, se non fosse stato per loro, non sarei mai stato in grado di venire a capo dei (numerosi) problemi che ho incontrato creando questo script.


Alcuni snippets

A differenza delle altre volte, oggi non condividerò l'intero codice, principalmente perchè è abbastanza lungo e non voglio intasare ogni volta questi post con due chilometri di codice 🤣

Ecco quindi le funzioni che si occupano di ottenere dalle API di Hive le informazioni che mi occorrono: quella per ottenere le operazioni custom json è quella che mi ha fatto impazzire, per colpa di un sistema di paging che non voleva saperne di collaborare 😅


# Get posts published in the target community
def get_community_posts(url, last_author, last_permlink, session: requests.Session):
    data = f'{{"jsonrpc":"2.0", "method":"bridge.get_ranked_posts", "params":{{"sort":"created","tag":"hive-146620","observer":"", "limit": 100,  "start_author":"{last_author}", "start_permlink":"{last_permlink}"}}, "id":1}}'
    response = get_response(data, url, session)
    posts = response.json()["result"]
    return posts


# Get replies from target author
def get_replies(url, author, session: requests.Session):
    data = f'{{"jsonrpc":"2.0", "method":"bridge.get_account_posts", "params":{{"sort":"comments", "account": "{author}", "limit": 100}}, "id":1}}'
    response = get_response(data, url, session)
    replies = response.json()["result"]
    return replies


# Get all custom operations from target account, poll votes included
def get_custom_json(author, num, url, session: requests.Session):
    data = f'{{"jsonrpc":"2.0", "method":"condenser_api.get_account_history", "params":["{author}", {num}, 1000, 262144], "id":1}}'
    response = get_response(data, url, session)
    custom_json = response.json()["result"]
    return custom_json


Lo so, quello che segue è un while loop infinito, ma oggi avevo davvero esaurito le energie alla fine e non ho trovato un modo più elegante per cercare tra le operazioni custom json di un utente finchè non trovo un voto in un sondaggio nel lasso di tempo che mi interessa (3 settimane)...


# Check if target account voted in one of the 3 last polls
def has_voted_poll(last_poll, author, url, session: requests.Session):
    today = datetime.now()
    three_weeks_ago = today - timedelta(days=21, hours=23)
    num = -1
    while True:
        custom_json = get_custom_json(author, num, url, session)
        for op in custom_json:
            link = op[1]["op"][1]["id"]
            if link in last_poll:
                return True
        timestamp = custom_json[0][1]["timestamp"]
        timestamp_formatted = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S")
        if timestamp_formatted < three_weeks_ago:
            return False
        num = custom_json[0][0]


Una piccola aggiunta alla funzione che si occupa di contare il numero di parole di un post: adesso il testo viene previamente ripulito da hyperlinks ed immagini, in modo da non falsare il risultato.


# Clean text before counting words
def clean_markdown(md_text):
    # Remove images
    md_text = re.sub(r"!\[.*?\]\(.*?\)", "", md_text)

    # Remove hyperlinks
    md_text = re.sub(r"\[(.*?)\]\(.*?\)", r"\1", md_text)

    return md_text


Se poi qualcuno fosse interessato al codice completo sono ovviamente disponibile a condividerlo!


cover realizzata con Bing AI ed editata con GIMP

a supporto della community #OliodiBalena, il 3% delle ricompense di questo post va a @balaenoptera

Se sei arrivato a leggere fin qui, grazie! Se hai voglia di lasciare un upvote, un reblog, un follow, un commento... be', un qualsiasi segnale di vita, in realtà, è molto apprezzato!

Sort:  

Questo vuol dire far parte di una community! Grande!

@tipu curate 3

Congratulazioni per l'ottimo lavoro🐣
!PIZZA

Ps.: il qualcuno era però fin troppo facile da indovinare:
untitled.gif
!LOLZ

Ahahahpl per gli utenti di OdB lo sapevo che non ci sarebbero stati dubbi 😅

!LOL

untitled.gif
🤣

How does a computer get drunk?
It takes screen shots.

Credit: marshmellowman
@arc7icwolf, I sent you an $LOLZ on behalf of pousinha

(1/2)
NEW: Join LOLZ's Daily Earn and Burn Contest and win $LOLZ

Hey innanzitutto non ho avuto il tempo di risponderti all'altro tuo commento perchè il sabato ma soprattutto la domenica sono giorni in cui sto poco al pc e generalmente online e quindi lo farò domani.
Per quanto riguarda quello che hai fatto con il tuo script e l'impegno che hai messo per farlo dimostra che bella persona che sei.
La cosa bella è che volevo fare uno script come il tuo ma dato che ogni giorno voglio fare 100 cose ma poi ne riesco a fare 20, conoscendomi non sarei mai riuscito a trovare il tempo per scriverlo e quindi non lo avrei fatto mai... poi leggo il tuo commento in cui mi dici di averlo fatto e quindi ti posso solo dire che sei un grande!
Per il mio supporto non devi ringraziarmi ma anzi è proprio il contrario perchè quello che fai su Hive è tutto merito tuo e meriti assolutamente supporto.
Purtroppo ho finito i tipu ma tornerò domani, il tuo lavoro che sarà utilissimo a me ma soprattutto a tutta la comunità merita tutto il supporto che posso darti.
Quindi sono io che devo ringraziarti e non il contrario ;)
!ALIVE
!PIMP
!PIZZA
!LOL
!hiqvote
!discovery 50

il sabato ma soprattutto la domenica sono giorni in cui sto poco al pc e generalmente online

Vai tranquillo, ti capisco perfettamente perchè vale lo stesso per me :)

Per il mio supporto non devi ringraziarmi

Almeno su qualcosa ogni tanto non siamo d'accordo 🤣🤣🤣

La cosa bella è che volevo fare uno script come il tuo

L'avevo già intuito dai precedenti messaggi, ma ora ne ho la conferma!!! Ma te quindi sai programmare? Posso chiederti, per curiosità, se hai imparato da autodidatta o se hai proprio studiato? E conosci anche Python? O usi altri linguaggi di programmazione?

Comunque allora anche senza interfaccia non avrai problemi ad usare il mio script! Quando (e come) posso inviartelo? Perchè a questo punto credo proprio che sia pronto per l'uso 😃

E grazie per tutto il supporto! Non importava addirittura che tornassi il giorno dopo anche per il tipu ahahaah

!LOL !PIZZA !PIMP

I left my last girlfriend because she wouldn’t stop counting.
I wonder what she’s up to now.

Credit: reddit
@libertycrypto27, I sent you an $LOLZ on behalf of arc7icwolf

(1/10)
Delegate Hive Tokens to Farm $LOLZ and earn 110% Rewards. Learn more.

Ma te quindi sai programmare? Posso chiederti, per curiosità, se hai imparato da autodidatta o se hai proprio studiato? E conosci anche Python? O usi altri linguaggi di programmazione?

Tanti anni fa ero all'università ed ero uno studente nel corso di Informatica, avevo una buona media ma mi sono fermato a 5 esami dal diploma di laurea perchè ho iniziato un lavoro che mi piaceva ed era gratificante ma che non mi lasciava tempo di studiare... quindi ho le basi di programmazione Java ma sono anni che non programmo anche se da qualche mese ho una rotellina nella testa che gira e sto pensando di terminare quello che non ho terminato anni fa anche perchè oggi penso sia molto più facile di allora terminare un corso di studi anche online... quindi si non ho problemi a far girare programmi mentre per programmare mi piacerebbe fare qualcosa ma non ho mai tempo a causa di lavori che non hanno nulla a che fare con la programmazione...
!PIMP !PIZZA !LOL

The inventor of autocorrect has died
His funnel is tomato

Credit: reddit
@arc7icwolf, I sent you an $LOLZ on behalf of libertycrypto27

(1/10)
Delegate Hive Tokens to Farm $LOLZ and earn 110% Rewards. Learn more.

Capito! Ti dirò, anch'io è un po' di tempo che ho quella rotellina che mi gira in testa... solo che io dovrei iniziare da zero, per cui partirei un bel po' più indietro 😅 però programmare è così interessante che sono terribilmente pentito nella vita di non aver provato a seguire questa strada prima, in modo da farla diventare un lavoro, invece che un hobby...

Comunque come vuoi che te lo mandi lo script?

!PIZZA !LOL !LUV

@libertycrypto27, @arc7icwolf(2/4) sent you LUV. | tools | discord | community | HiveWiki | <>< daily

Made with LUV by crrdlx

What do you call a mobster who’s buried in cement?
A hardened criminal.

Credit: marshmellowman
@libertycrypto27, I sent you an $LOLZ on behalf of arc7icwolf

(2/10)
Delegate Hive Tokens to Farm $LOLZ and earn 110% Rewards. Learn more.


This post was shared and voted inside the discord by the curators team of discovery-it
Join our Community and follow our Curation Trail
Discovery-it is also a Witness, vote for us here
Delegate to us for passive income. Check our 80% fee-back Program

Thanks for the mention!

If your script only collects historical data, getting that data can be faster from hiveSQL than from the api. If it needs to run often, I would try that. It's "cheaper" as you can just do an SQL "select"...

Aynways: glad your script works!

It doesn't need to run often (only 1 time a week, 2 or 3 if one wants to double/triple check the result), but I feel you just gave me a new topic to explore :)

I found the docs here and I'm going to see if I can understand how hiveSQL works and if I can use it!

I'm also looking for your next post! :)))

Next post is finished. I just haven't sent it, yet. I've been busy...

Instead of posting code snippets, I decided to (this time for real) launch a website.
I spent the last 2 weeks learning stuff about http, headers, cookies, HTML, templates, webfonts...
I am building a full webserver with caddy (albeit for very little content right now) and a backend with 2 python servers.
I even learned a lot about Linux along the way.

So yeah... I post, when I feel like I have to tell something. For now I am still cooking.

I decided to (this time for real) launch a website

Oh god, that's incredible! I can't wait to see it :)))))))

E non voglio poi ripensare a quanto è stato difficile cercare di capire il funzionamento di alcuni metodi (si chiamano così?) delle API di Hive: senza spiegazioni chiare in molti casi l'unica soluzione è stata provare e riprovare, sperando che qualcosa ad un certo punto desse il risultato sperato!

Mi hai fatto ridere, perché mi sono rivisto io quando bestemmio in tutte le lingue con le API o qualsiasi cosa open source. Purtroppo è così, e di aneddoti ne ho una quintalata, tipo docs palesemente sbagliati perché si hanno fatto la tal cosa, ma non volevano farla, quindi hanno taroccato la documentazione.

Cavolo, ma siete tutti programmatori? 🤣

Adesso mi vergogno ancora di più per lo schifo di codice che scrivo e condivido 😅

Forse ogni volta dovrei aggiungere una introduzione con caratteri enormi all'inizio del post in cui dico "FACCIO SCHIFO, NON SO PROGRAMMARE, PRENDETE TUTTO COME SE LO AVESSO FATTO UN BAMBINO DI 5 ANNI" !LOL

tipo docs palesemente sbagliati perché si hanno fatto la tal cosa, ma non volevano farla, quindi hanno taroccato la documentazione

Ahahahah questa è bellissima 😂 e intanto uno impazzisce per capire come mai le cose non funzionano! Che merde 🤣

Non sono programmatore, system integrator al limite. Per cui tranquillo non giudico il codice di nessuno, tanto meno il tuo.

What's a dry orange good for?
Nothing at all. It's juiceless.

Credit: reddit
@tosolini, I sent you an $LOLZ on behalf of arc7icwolf

(2/10)
Delegate Hive Tokens to Farm $LOLZ and earn 110% Rewards. Learn more.

PIZZA!

$PIZZA slices delivered:
arc7icwolf tipped libertycrypto27 (x2)
libertycrypto27 tipped arc7icwolf (x2)
pousinha tipped arc7icwolf
@arc7icwolf(2/10) tipped @harbiter

Daje ottimo lavoro! Hai sicuramente fornito un servizio enorme alla community ✌🏻

Grazie mille @harbiter! Nel tempo spero di riuscire ad aggiungere anche nuovi funzioni... e magari poi sviluppare nuovi tools che possano aiutare OdB a crescere ancora di più.

!PIZZA

@libertycrypto27, the HiQ Smart Bot has recognized your request (1/3) and will start the voting trail.

In addition, @arc7icwolf gets !LOOL from @hiq.redaktion.

For further questions, check out https://hiq-hive.com or join our Discord. And don't forget to vote HiQs fucking Witness! 😻