<commit_before># Copyright 2014 Bevbot LLC, All Rights Reserved
#
# This file is part of the Pykeg package of the Kegbot project.
# For more information on Pykeg or Kegbot, see http://kegbot.org/
#
# Pykeg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Pykeg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pykeg.  If not, see <http://www.gnu.org/licenses/>.

"""Celery tasks for Twitter."""

import os
import tempfile
from contextlib import closing

import tweepy
import requests
from celery.task import task

from pykeg.plugin import util

logger = util.get_logger(__name__)

@task(expires=60)
def send_tweet(consumer_key, consumer_secret, oauth_token, oauth_token_secret, tweet, image_url):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True)
    auth.set_access_token(oauth_token, oauth_token_secret)
    api = tweepy.API(auth)

    local_media = None
    try:
        local_media = download_file(image_url)
    except requests.exceptions.RequestException as e:
        logger.warning('Error downloading media url "%s": %s' % (image_url, e))

    if not local_media:
        logger.info('Sending text-only tweet: {}'.format(repr(tweet)))
        result = api.update_status(tweet)
    else:
        logger.info('Sending media tweet.')
        try:
            result = api.update_with_media(local_media, status=tweet)
        finally:
            os.unlink(local_media)
    return result

def download_file(url):
    r = requests.get(url, stream=True)
    ext = os.path.splitext(url)[1]
    fd, pathname = tempfile.mkstemp(suffix=ext)
    logger.info('Downloading file %s to path %s' % (url, pathname))
    with closing(os.fdopen(fd, 'wb')):
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk:
                os.write(fd, chunk)
    return pathname
<commit_msg>Twitter: use string pathname.<commit_after># Copyright 2014 Bevbot LLC, All Rights Reserved
#
# This file is part of the Pykeg package of the Kegbot project.
# For more information on Pykeg or Kegbot, see http://kegbot.org/
#
# Pykeg is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Pykeg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pykeg.  If not, see <http://www.gnu.org/licenses/>.

"""Celery tasks for Twitter."""

import os
import tempfile
from contextlib import closing

import tweepy
import requests
from celery.task import task

from pykeg.plugin import util

logger = util.get_logger(__name__)

@task(expires=60)
def send_tweet(consumer_key, consumer_secret, oauth_token, oauth_token_secret, tweet, image_url):
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret, secure=True)
    auth.set_access_token(oauth_token, oauth_token_secret)
    api = tweepy.API(auth)

    local_media = None
    if image_url:
        try:
            local_media = download_file(image_url)
        except requests.exceptions.RequestException as e:
            logger.warning('Error downloading media url "%s": %s' % (image_url, e))

    if not local_media:
        logger.info('Sending text-only tweet: {}'.format(repr(tweet)))
        result = api.update_status(tweet)
    else:
        logger.info('Sending media tweet.')
        try:
            result = api.update_with_media(local_media, status=tweet)
        finally:
            os.unlink(local_media)
    return result

def download_file(url):
    r = requests.get(url, stream=True)
    ext = os.path.splitext(url)[1]
    fd, pathname = tempfile.mkstemp(suffix=ext)
    logger.info('Downloading file %s to path %s' % (url, pathname))
    with closing(os.fdopen(fd, 'wb')):
        for chunk in r.iter_content(chunk_size=1024): 
            if chunk:
                os.write(fd, chunk)
    return str(pathname)

{'fraction_non_alphanumeric': 0.08920392584514722, 'fraction_numerical': 0.005234460196292257, 'mean_word_length': 3.5093411996066863, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3492833', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1377, 'n_words': 477}