Jump to content
Guest Madacala

Script ce comentează la postăriile noi ale unei pagini.

Recommended Posts

Inițial voiam să fac un script ce comentează doar pe 9GAG mereu când apare o postare nouă pentru că de obicei cel ce comentează primul acolo ajunge în top comments însă m-am gândit să-l fac mai dinamic și să fie valabil pentru orice pagină:
Aveți aici sursa pe Github în caz că doriți să-i dați un star: https://github.com/emanuelcepoi/stalkpy

Sau codul direct aici:

 

from facepy import GraphAPI

#Your acces token goes here
personal_access_token = ""

#Creating an GraphAPI instance
graph = GraphAPI(personal_access_token)

#Page ID/Name to check for new posts
pageToCheck = ''

#Comment text to post
commentToPost = 'This comment was posted using stalkpy'

#Function that gets the latest post and returns its id
def getLastPost(pageID):

    #Using facepy to get de data
    a = graph.get(
        path='{0}/posts?limit=1'.format(pageID)
    )
    #Parsing the data over here
    b  = a['data'][0]['id']

    #Returning parsed data
    return b

#Function that comments on a post with the commentToPost variable content
def comment(postId,textToComment):
    graph.post(
        path='{0}/comments'.format(postId),
        message = textToComment
    )

#Seting "a" equal to the last post ID on the page
a = getLastPost(pageToCheck)

while True:
    #Check if there is a new post
    if a == getLastPost(pageToCheck):
        print ("No new posts Lord Sam")
    #If 'a' is not equal to getLastPost() it means that there's a new post, so comment on it
    else:
        comment(getLastPost(pageToCheck),commentToPost)
        print ("Commented on a new post Lord Sam")
        #After commenting set 'a' equal to the new post ID, this way the script will run again
        a = getLastPost(pageToCheck

)

Am explicat aici mai multe: https://github.com/emanuelcepoi/stalkpy/blob/master/README.md

Enjoy!

Link to comment
Share on other sites

  • Active Members

Cateva sfaturi:

 

- nu mai comenta fiecare linie de cod.

- fiecare metoda ar trebui sa contina docstring.

- numele functiilor ar trebui sa fie denumite astfel: getLastPost -> get_last_post (snake_case)

- regula de mai sus se aplica si la numele variabilelor

- in jurul operatorilor ar trebui sa existe un singur spatiu

- cel mai important e ca (,) codul scris sa fie consistent. Foloseste ori double-quotes ori single-quotes cand definesti un string.

- nu creea variabile inutile, mai ales daca sunt folosite o singura data

- daca variabilele sunt folosite intr-o singura functie, nu are rost sa le faci globale. Fa-le argumente sau defineste-le in functia respectiva.

- if __name__ == '__main__'

 

Cum as face eu:

 

from facepy import GraphAPI

personal_access_token = ''
graph = GraphAPI(personal_access_token)


def get_last_post(page_id):
    """
    Ce face functia asta ?
    """
    a = graph.get(path='{0}/posts?limit=1'.format(page_id))

    return a['data'][0]['id']


def comment(post_id, text_to_comment):
    """
    Ce face functia asta ?
    """
    graph.post(
        path='{0}/comments'.format(post_id),
        message=text_to_comment
    )


def main(page_to_check='', comment_to_post='This comment was posted using stalkpy'):
    a = get_last_post(page_to_check)

    while True:
        if a == get_last_post(page_to_check):
            print('No new posts Lord Sam')
        else:
            comment(get_last_post(page_to_check), comment_to_post)
            print('Commented on a new post Lord Sam')
            a = get_last_post(page_to_check)


if __name__ == '__main__':
    main()

Mai multe despre styleguide gasesti aici.

 

Edited by MrGrj
  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...