<commit_before>import urllib
import re
import json
import requests

from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

from allauth.socialaccount.models import SocialApp, SocialAccount, SocialToken
from twitter import Twitter, OAuth

from scheduler.models import ScheduledPost


def post_to_facebook(post):
    try:
        account = SocialAccount.objects.get(user=post.user,
                                            provider=post.service)
    except SocialAccount.DoesNotExist:
        return None
    try:
        access_token = account.socialtoken_set.get().token
    except SocialToken.DoesNotExist:
        return None

    params = {
        "access_token": access_token
    }
    media_objs = json.loads(post.attached_media)
    media = []
    for med in media_objs:
        try:
            med_file = open(med, "rb")
        except IOError:
            continue
        response = requests.post(
            "https://graph.facebook.com/{0}/photos?{1}".format(
                account.uid,
                urllib.urlencode(params)
            ),
            files={"source": med_file}
        )
        if response.ok:
            media.append(response.json().id)

    if media:
        params["object_attachment"] = media[0]

    message = post.status.encode("utf-8")
    params["message"] = message
    link = re.findall("https?://[^\s]+", post.status)
    if link:
        params["link"] = link[0]

    response = requests.post(
        "https://graph.facebook.com/{0}/feed?{1}".format(
            account.uid,
            urllib.urlencode(params)
        ))
    if response.ok:
        post.is_posted = True
        post.save()


def post_to_twitter(post):
    app = SocialApp.objects.get(provider=post.service)
    try:
        account = SocialAccount.objects.get(user=post.user,
                                            provider=post.service)
    except SocialAccount.DoesNotExist:
        return None
    try:
        token = account.socialtoken_set.get().token
        token_secret = account.socialtoken_set.get().token_secret
    except SocialToken.DoesNotExist:<commit_msg>Returned data is a dict.<commit_after>import urllib
import re
import json
import requests

from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone

from allauth.socialaccount.models import SocialApp, SocialAccount, SocialToken
from twitter import Twitter, OAuth

from scheduler.models import ScheduledPost


def post_to_facebook(post):
    try:
        account = SocialAccount.objects.get(user=post.user,
                                            provider=post.service)
    except SocialAccount.DoesNotExist:
        return None
    try:
        access_token = account.socialtoken_set.get().token
    except SocialToken.DoesNotExist:
        return None

    params = {
        "access_token": access_token
    }
    media_objs = json.loads(post.attached_media)
    media = []
    for med in media_objs:
        try:
            med_file = open(med, "rb")
        except IOError:
            continue
        response = requests.post(
            "https://graph.facebook.com/{0}/photos?{1}".format(
                account.uid,
                urllib.urlencode(params)
            ),
            files={"source": med_file}
        )
        if response.ok:
            media.append(response.json()["id"])

    if media:
        params["object_attachment"] = media[0]

    message = post.status.encode("utf-8")
    params["message"] = message
    link = re.findall("https?://[^\s]+", post.status)
    if link:
        params["link"] = link[0]

    response = requests.post(
        "https://graph.facebook.com/{0}/feed?{1}".format(
            account.uid,
            urllib.urlencode(params)
        ))
    if response.ok:
        post.is_posted = True
        post.save()


def post_to_twitter(post):
    app = SocialApp.objects.get(provider=post.service)
    try:
        account = SocialAccount.objects.get(user=post.user,
                                            provider=post.service)
    except SocialAccount.DoesNotExist:
        return None
    try:
        token = account.socialtoken_set.get().token
        token_secret = account.socialtoken_set.get().token_secret
    except SocialToken.DoesNotExist:
{'fraction_non_alphanumeric': 0.10348027842227378, 'fraction_numerical': 0.0032482598607888632, 'mean_word_length': 2.1490138787436086, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3387989', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1202, 'n_words': 243}