Last active
August 29, 2015 14:13
-
-
Save halit/69661027f1e4b7d90ff4 to your computer and use it in GitHub Desktop.
Feed the troll!
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
#--coding:utf-8-- | |
import requests | |
import json | |
import colorama | |
import sys | |
DETECT_WORDS = (u"açıklandı", u"aciklandi", u"açiklandi", u"açiklandı", u"sistemde", u"sistem de") | |
COMMENT = u"Ortalamayi bilen var mi?" | |
FB_PAGE_ID = "197232100307006" | |
FB_API_URL = "https://graph.facebook.com/v2.2/" | |
FB_PAGE_URL = "{}{}/feed".format(FB_API_URL, FB_PAGE_ID) | |
FB_COMMENTS_URL = "{}{}/comments" | |
FB_ACCESS_TOKEN = "" | |
HEADERS = {"access_token": FB_ACCESS_TOKEN} | |
def connect_url(url, **kwargs): | |
method = kwargs.pop("method") | |
requester = requests.__dict__.get(method) | |
try: | |
result = requester(url, **kwargs) | |
except requests.ConnectionError: | |
print colorama.Fore.RED + colorama.Style.BRIGHT, "Connection Error." | |
sys.exit(1) | |
else: | |
return result | |
if __name__ == "__main__": | |
result = connect_url(FB_PAGE_URL, params=HEADERS, method="get") | |
if result.ok: | |
posts = json.loads(result.content)['data'] | |
detects = [post for post in posts for word in DETECT_WORDS if word in post.get('message', '')] | |
unique_detects = [] | |
for detect in detects: | |
comments = detect.get('comments', None) | |
if comments: | |
comment_data = [COMMENT == comment['message'] for comment in comments['data']] | |
if any(comment_data): | |
continue | |
else: | |
unique_detects.extend([detect for comment in comments['data'] if not COMMENT == comment['message']]) | |
for detect in unique_detects: | |
comment_url = FB_COMMENTS_URL.format(FB_API_URL, detect['id']) | |
message_payload = HEADERS.copy() | |
message_payload['message'] = COMMENT | |
message_result = connect_url(comment_url, data=message_payload, method="post") | |
if message_result.ok: | |
print colorama.Fore.GREEN + colorama.Style.BRIGHT, "Message sended to:", detect['from']['name'] | |
else: | |
print colorama.Fore.RED + colorama.Style.BRIGHT, "Error occured. Maybe token?" | |
else: | |
print colorama.Fore.RED + colorama.Style.BRIGHT, "Error occured. Maybe token?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment