Twitter ride posting Python code

Twitter ride posting Python code

Postby NoyzSource » Tue Jul 06, 2010 9:48 pm

I'm going to post this right away but clean it up a little and post it again later.

Code: Select all
#This program 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, version 3 of the License

#This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.


import os
import re
import twitter
twitapi = twitter.Api(username='BeverlyPark', password='password')


#PING.FM Settings
#SET YOUR APP KEY HERE
user_app_key = ""
#DO NOT EDIT BELOW THIS LINE!

__version__ = "1.0 Final"
__api_key__ = "Eric Deleted the API key"
#DO NOT EDIT __API_KEY__!!!

def status(input):
    #message = input #raw_input('Status update:')
    input.replace(' ', '&#32;')
   
    os.system("wget --post-data 'api_key="+ __api_key__ +"&user_app_key="+ user_app_key +"&post_method=status&body="+ input +"' -O .pingfm_cache  http://api.ping.fm/v1/user.post")
   
    print "===== API output ====="
    #Printataan uloste
    os.system("cat .pingfm_cache")
    #poistetaan cache
    os.system("rm .pingfm_cache")
    return(0)

#all_replies = api.GetReplies()
#all_direct_messages = api.GetDirectMessages()
#read the BikeRides.rc file
path_to_BikeRidesrc_file = '.BikeRides/BikeRides.rc'
dog = open(path_to_BikeRidesrc_file)
while 1:
    tim = dog.readline()
    if len(tim)==0: break #check for end of file
    bob = tim.split() #split the rc file lines
    if bob[0] == 'last_reply_id': #check the first split word
        reply_since = bob #set the last message to check for Twitter API
    if bob[0] == 'last_direct_message_id': #check the first split word
        direct_message_since = bob #set the last message to check for Twitter API
       
#print reply_since[2]
#print direct_message_since[2]

########################################
##  Parsing Replies for BikeRides

all_reply_ids = [long(reply_since[2])]
#get all replies since last reply specified in BikesRides.rc
all_new_replies = twitapi.GetReplies(1,reply_since[2])

#search_for_bikerides
for message in all_new_replies:
    #search for #bikeride in the replies
    print message.text
    print message.id
    if re.search("#bikeride",message.text):
        #print message.text
        #generate a unique-ish ID from the last 4 chars of the Twitter ID
        unique_ID = str(message.id)[-4:]
        #print unique_ID
        message_without_beverlypark = message.text.replace('@beverlypark', ' ')
        #print "Is it gone: " + message_without_beverlypark
        filename = str(unique_ID) + '.txt'
        ericfilewrite = open(filename, "w")
        ericfilewrite.write(message_without_beverlypark)
        ericfilewrite.close()
        #send the reply and four digit ID to Ken and Eric
        twitapi.PostDirectMessage('noyzsource', unique_ID + " " + message.text)
        #print message.user
    #collect all Twitter IDs in one place to use later.
    all_reply_ids.append(message.id)
    #write the reply to a file with a unique 4 digit ID
   
print all_reply_ids   
#write the last ID of the Reply to the BikeRides.rc file
reply_since[0] = 'last_reply_id'
reply_since[1] = '='
reply_since[2] = str(max(all_reply_ids))
print reply_since
output_rc_file = open(path_to_BikeRidesrc_file, "w")
output_rc_file.write(reply_since[0] + " " + reply_since[1] + " " + reply_since[2] + "\n")


print 'end of reply section'
print '####################'
print 'start of direct message section'
########################################
##  Parsing Direct Messages for Approval


all_direct_message_ids = [long(direct_message_since[2])]
#get all direct messages since last DM specified in BikesRides.rc
all_new_direct_messages = twitapi.GetDirectMessages(1,direct_message_since[2])
#print all_new_direct_messages


for message in all_new_direct_messages:
    #print message.text
    if re.search('approve', message.text): #Parse message for "approve" or "reject"
        approved_message_id = message.text.split()[1] #Parse message for Unique ID
        print 'Approved message: ' +approved_message_id
        #if approve forward message to ping.fm
            #read message with unique ID from file
        fullpath = '//home//epenne//sourceCode//BikeRides//src//'
        filename = str(approved_message_id) + '.txt'
        print fullpath
        if os.path.exists(filename):
            approved_message = open(filename,"r")
            print approved_message
            for johnny in approved_message.readlines(): #while 1:
                print johnny
                #forward message to ping.fm
                status(johnny)
            approved_message.close()
            os.remove(filename)
        else:
            print 'file: '+filename+' does not exist.'
    elif re.search('reject', message.text):
        rejected_message_id = message.text.split()[1]
        filename = str(rejected_message_id) + '.txt'
        print 'Removing file: ' + filename
        os.remove(filename)       
    #collect all Twitter IDs in one place to use later.
    all_direct_message_ids.append(message.id)
   
   
#write the last ID of the Direct Messages to the BikeRides.rc file
direct_message_since[0] = 'last_direct_message_id'
direct_message_since[1] = '='
direct_message_since[2] = str(max(all_direct_message_ids))
print direct_message_since

output_rc_file.write(direct_message_since[0] + " " + direct_message_since[1] + " " + direct_message_since[2])
output_rc_file.close()

User avatar
NoyzSource
Site Admin
 
Posts: 711
Joined: Mon Aug 20, 2007 9:20 am
Location: Cedar Rapids, IA

Re: Twitter ride posting Python code

Postby musthavemuzk » Fri Jul 09, 2010 10:17 am

yup that's alot of characters that dont mean squat to me

explain please
User avatar
musthavemuzk
 
Posts: 115
Joined: Mon Apr 26, 2010 11:36 am
Location: Cedar Rapids


Return to Tech Support

Who is online

Users browsing this forum: No registered users and 1 guest