Skip to content

Instantly share code, notes, and snippets.

@norsez
Last active August 25, 2017 06:48
Show Gist options
  • Select an option

  • Save norsez/28dbf7e5df44e7d76599001c97ab1fa9 to your computer and use it in GitHub Desktop.

Select an option

Save norsez/28dbf7e5df44e7d76599001c97ab1fa9 to your computer and use it in GitHub Desktop.

Revisions

  1. Norsez Orankijanan revised this gist Aug 25, 2017. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions comments_and_likes.py
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,15 @@
    # Loading post comments and likes by Norsez Orankijanan norsez@gmail.com
    # based on example by James Thornton's script https://gist.github.com/jkuruzovich/b8485a368f80a3b88df46326cf54bbce
    # Loading post comments and likes by Norsez Orankijanan nor@telenordigital.com
    # based on example by James Thornton, http://jamesthornton.com

    # Facebook API Docs
    # https://developers.facebook.com/docs/graph-api/using-graph-api#reading

    # Get Your Facebook Access Token Here...
    # https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me

    #HOW TO USE
    #edit ACCESS_TOKEN to your current access token from the link above
    #edit POST_ID to the post id you are interested in
    #edit run this script
    # Before running this script...
    # Set your Facebook Access Token as an environment variable in your terminal:
    #export ACCESS_TOKEN="EAACEdEose0cBAHZCLwgcdvTYfXqyahiPi7zEO3qWVBf6Yde3MIYEAa0L0IhvKUzJ2djfbf0XEkUumFdo7TMBSTwScul7RXlshMwn2H60uzjtZC6DpsJgpfOAH4YTXaaKrcoWzSs89IyeMUwNaiIOoT7WQR3tVP5ybnVRjC6H5WG9Ez1973MY8BZBAs8AooZD"


    import io
    @@ -22,8 +21,8 @@
    import csv

    # get Facebook access token from environment variable
    ACCESS_TOKEN = "EAACEdEose0cBAHgTFfnBKmy7WcMzXgm1TbxVLUDuOoE7pCIf4vzLxU1jZCk4o8znZC2orBZCBGCOQr3z7i1ost1b1lhOYlTeIZAZBr0PiC5x5V527qc1n05ojdsQcNhl7xzrYBj69KSqNi4iMJ53ofJ73EP1ufw8RrCmTKTZA8fg5JwjYp3L5pCtLUCtE4UPoZD"
    POST_ID = "1056303127838075"
    ACCESS_TOKEN = "EAACEdEose0cBAHuxmvyPjbqaZAPEmJjziRHbQhPEqnHz9Sffpddz9yw43V0qms0vS5f3WvhfXFSqxCXTvyXrY1ZAFLeNZAedmrfTDZAmiqdxm7ZBerrCVAu5QgBtIZA5NLXrZAPx8LB6y9AggxTvgF1BQSUEsOHdO03dxFGYr06Fuh30IBFVLQTbCtVULJOEVQZD"
    POST_ID = "1059511694183885"
    # build the URL for the API endpoint
    host = "https://graph.facebook.com"
    params = urllib.parse.urlencode({"access_token": ACCESS_TOKEN})
    @@ -45,7 +44,7 @@ def loadComments():
    path = "/v2.10/{}/comments".format(POST_ID)
    url = "{host}{path}?{params}".format(host=host, path=path, params=params)
    nextURL = url
    fname = "output_facebook_comments" + str(int(time.time())) + ".csv"
    fname = "output_facebook_comments_" + POST_ID + "_" + str(int(time.time())) + ".csv"
    total = 0
    with io.open(fname,'w',encoding='utf8') as f:
    rowWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    @@ -72,7 +71,7 @@ def loadLikes():
    path = "/v2.10/{}/likes".format(POST_ID)
    url = "{host}{path}?{params}".format(host=host, path=path, params=params)
    nextURL = url
    fname = "output_facebook_likes" + str(int(time.time())) + ".csv"
    fname = "output_facebook_likes_" + POST_ID + "_" + str(int(time.time())) + ".csv"
    total = 0
    with io.open(fname,'w',encoding='utf8') as f:
    rowWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
  2. Norsez Orankijanan created this gist Aug 22, 2017.
    98 changes: 98 additions & 0 deletions comments_and_likes.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    # Loading post comments and likes by Norsez Orankijanan norsez@gmail.com
    # based on example by James Thornton's script https://gist.github.com/jkuruzovich/b8485a368f80a3b88df46326cf54bbce

    # Facebook API Docs
    # https://developers.facebook.com/docs/graph-api/using-graph-api#reading

    # Get Your Facebook Access Token Here...
    # https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me

    #HOW TO USE
    #edit ACCESS_TOKEN to your current access token from the link above
    #edit POST_ID to the post id you are interested in
    #edit run this script


    import io
    import json
    import urllib
    import urllib.request
    import pprint
    import time
    import csv

    # get Facebook access token from environment variable
    ACCESS_TOKEN = "EAACEdEose0cBAHgTFfnBKmy7WcMzXgm1TbxVLUDuOoE7pCIf4vzLxU1jZCk4o8znZC2orBZCBGCOQr3z7i1ost1b1lhOYlTeIZAZBr0PiC5x5V527qc1n05ojdsQcNhl7xzrYBj69KSqNi4iMJ53ofJ73EP1ufw8RrCmTKTZA8fg5JwjYp3L5pCtLUCtE4UPoZD"
    POST_ID = "1056303127838075"
    # build the URL for the API endpoint
    host = "https://graph.facebook.com"
    params = urllib.parse.urlencode({"access_token": ACCESS_TOKEN})



    def query(url):
    # open the URL and read the response
    resp = urllib.request.urlopen(url).read()

    # convert the returned JSON string to a Python datatype
    me = json.loads(resp)

    # display the result
    #pprint.pprint(me)
    return me

    def loadComments():
    path = "/v2.10/{}/comments".format(POST_ID)
    url = "{host}{path}?{params}".format(host=host, path=path, params=params)
    nextURL = url
    fname = "output_facebook_comments" + str(int(time.time())) + ".csv"
    total = 0
    with io.open(fname,'w',encoding='utf8') as f:
    rowWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    while nextURL != '':
    #print("loading page: " + nextURL)
    data = query(nextURL)
    for r in data['data']:
    from_name = r['from']['name']
    from_id = r['from']['id']
    rowWriter.writerow([r['created_time'],
    from_name,
    from_id,
    r['message'],
    r['id']])
    total = total + len(data['data'])
    if 'next' in data['paging']:
    nextURL = data['paging']['next']
    else:
    nextURL = ''
    print("number of comments loaded: {}".format(total))
    print("total comments downloaded: {}".format(total))

    def loadLikes():
    path = "/v2.10/{}/likes".format(POST_ID)
    url = "{host}{path}?{params}".format(host=host, path=path, params=params)
    nextURL = url
    fname = "output_facebook_likes" + str(int(time.time())) + ".csv"
    total = 0
    with io.open(fname,'w',encoding='utf8') as f:
    rowWriter = csv.writer(f, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    while nextURL != '':
    #print("loading page: " + nextURL)
    data = query(nextURL)
    for r in data['data']:
    rowWriter.writerow([
    r['name'],
    r['id']
    ])
    total = total + len(data['data'])
    if 'next' in data['paging']:
    nextURL = data['paging']['next']
    else:
    nextURL = ''
    print("number of likes loaded: {}".format(total))
    print("total likes downloaded: {}".format(total))

    #
    #
    loadLikes()
    loadComments()