python: Publicar los tweets de hoy en wordpress

Aquí el script final para añadir unos tweets especificos de hoy en wordpress:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import urllib
import datetime as date
import time
import simplejson as json
import re
import MySQLdb

def getTweets(id):
    now = date.datetime.now().strftime("%Y-%m-%d")
    url = "http://search.twitter.com/search.json?q=+from%3A"+u+"+since%3A"+now
    json_text = urllib.urlopen(url).read()
    json_obj = json.loads(json_text)

    tweets = []
    for result in json_obj['results']:
        tweets.append(result['text'])
    return tweets

def isInArray(text, array):
    for a in array:
        m = re.search(a, text)
        if(m):
            return True
    return None

def text2link(text):
    # links
    m = re.sub(r'(?i)(^|\s+|\A)(http|https|ftp)(\://\S+)(\s+|\Z|$)', '\\1<a href=\"\\1\\2\" onclick=\"window.open(this.href); return false;\">\\2\\3</a>\\4', text)
    # users
    m = re.sub(r'(?i)(^|\s+|\A)(@)(\S+)(\s+|\Z)', '\\1<a href=\"http://twitter.com/\\3\" onclick=\"window.open(this.href); return false;\">\\2\\3</a>\\4', m)
    # hashtag
    m = re.sub(r'(?i)(^|\s+|\A)(#)(\S+)(\s+|\Z)', '\\1<a href=\"https://twitter.com/search?q=%23\\3\" onclick=\"window.open(this.href); return false;\">\\2\\3</a>\\4', m)
    return m

def insert_wordpress(post_content, ddbb_config, id_user):

    now = date.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    now_reduce = date.datetime.now().strftime("%d-%m-%Y")

    conn = MySQLdb.connect (host = ddbb_info['host'], user = ddbb_config['user'], passwd = ddbb_config['passwd'], db = ddbb_config['db'])

    cursor = conn.cursor()
    insert = ''
    insert = insert + "insert `wordpress`.`wp_posts`"
    insert = insert + "(post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status,"
    insert = insert + "comment_status, ping_status, post_password, post_name, to_ping, pinged,"
    insert = insert + "post_modified, post_modified_gmt, post_content_filtered, post_parent,"
    insert = insert + "guid, menu_order, post_type, post_mime_type, comment_count) "
    insert = insert + "values ("
    insert = insert + id_user +", '"+now+"', '"+now+"', '"+post_content+"', 'twitter: "+now_reduce+"', '', 'publish',"
    insert = insert + "'open', 'open', '', 'twitter-"+now_reduce+"', '', '',"
    insert = insert + "'"+now+"', '"+now+"', '', 0,"
    insert = insert + "'', 0, 'post', '', 0)"
    #print insert
    cursor.execute(insert)
    #print "Number of rows inserted: %d" % cursor.rowcount
    cursor.close()
    conn.close ()

# parámetros a cambiar
users = ['ZiTAL', 'HQH']
tags = ['#hispabyte', '#gnu', '#linux', '@HQH']
ddbb_config = {'host': 'localhost', 'user': 'root', 'passwd': 'root', 'db': 'wordpress'}
id_user = 1

text = ''

for u in users:
    tweets = getTweets(u)
    time.sleep(2)

    text_user = "<p>" + text2link("@"+u) + ":</p>\n"
    text_user = text_user + "<p>\n"

    text_tweet = ''
    for tweet in tweets:
            if(isInArray(tweet, tags)):
            text_tweet = text_tweet + text2link(tweet) + "<br />\n"            
    if(text_tweet):
        text = text_user + text_tweet + "</p>\n" 

if(text):
    insert_wordpress(text, ddbb_config, id_user)
    #print text

Al final el parámetro until dejó de funcionar…

Hay que tener en cuenta que se tienen que cambiar los pa?ametros de la sección: “# parámetros a cambiar”

Y lo ejecutamos como siempre:

./ztwitter.py
This entry was posted in Gnu / Linux, Programacion, Tutoriales / Manuales and tagged , , . Bookmark the permalink.

Deja un comentario

Tu dirección de correo electrónico no será publicada.

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.