Created
February 4, 2018 11:10
-
-
Save amrit3701/3c5ff8915fddd912d10e06c73eef9fe3 to your computer and use it in GitHub Desktop.
This script calculates winners data of sscsldh (LuvLdh) Facebook page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from decouple import config | |
| import facebook | |
| import requests | |
| import re | |
| import json | |
| def getMaximiumLikes(api, posts): | |
| likesdic = {} | |
| usernamedic = {} | |
| for post in posts: | |
| response = requests.get("https://graph.facebook.com/v2.11/" + post["id"] + "?fields=reactions.summary(true).limit(0),permalink_url&access_token=" + config('PAGE_ACCESS_TOKEN')) | |
| response = response.json() | |
| likes = int(response["reactions"]["summary"]["total_count"]) | |
| username = re.search('Username: (.*)\n', post["message"]).group(1) | |
| user_profile = re.search('\n(.*)\nCategory:', post["message"]).group(1) | |
| post_img = api.get_object(post["id"], fields="link")["link"] | |
| usernamedic = {'profile': user_profile, 'img': post_img} | |
| if not likes in likesdic: | |
| likesdic[likes] = [{username: usernamedic}] | |
| else: | |
| likesdic[likes].append(usernamedic) | |
| return likesdic | |
| def updateContestWinners(): | |
| access_token = config('PAGE_ACCESS_TOKEN') | |
| api = facebook.GraphAPI(access_token) | |
| posts = api.get_object("me", fields="posts")['posts']['data'] | |
| PhotographyPosts = [] | |
| ContentWritingPosts = [] | |
| SouvenirPosts = [] | |
| json_dic = {} | |
| for post in posts: | |
| try: | |
| if "Category: Photography Contest #LuvLdh #sscsLdh" in post['message']: | |
| PhotographyPosts.append(post) | |
| elif "Content Writing Contest #LuvLdh #sscsLdh" in post['message']: | |
| ContentWritingPosts.append(post) | |
| elif "Souvenir Contest #LuvLdh #sscsLdh" in post['message']: | |
| SouvenirPosts.append(post) | |
| except KeyError: | |
| continue | |
| json_dic["PhotographyContest"] = getMaximiumLikes(api, PhotographyPosts) | |
| json_dic["ContentWritingContest"] = getMaximiumLikes(api, ContentWritingPosts) | |
| json_dic["SouvenirContest"] = getMaximiumLikes(api, SouvenirPosts) | |
| with open('./WinnersData.json', 'w') as outfile: | |
| json.dump(json_dic, outfile) | |
| updateContestWinners() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment