Saturday, 14 September 2013

Python program run fine on Windows but not Linux

Python program run fine on Windows but not Linux

Currently, I'm working as a social manager for my company. Part of the job
is to manage several Twitter account for each department of the company.
Months ago, the boss said, "whenever the company post a new status, all
the department have to retweet it". That easy, but it really depressed
since i have to login, then logout all the account just to click retweet
button. So, i have an idea to make a small tool which can handle the job
for me.
The tool come with 2 files, and use tweepy module to handle oAuth, check
for new tweet and post retweet.
The first is spy.py: it will watch the company twitter to see if a new
tweet posted for each 30 minute.
import time
import tweepy
def spyMode(new_id):
# Setup spy agent
spy_consumer_key = 'spy_consumer_key'
spy_consumer_secret = 'spy_consumer_secret'
spy_access_token = 'spy_access_token'
spy_access_token_secret = 'spy_access_token_secret'
spy_auth = tweepy.OAuthHandler(spy_consumer_key, spy_consumer_secret)
spy_auth_url = spy_auth.get_authorization_url()
spy_auth.set_access_token(spy_access_token, spy_access_token_secret)
# Sleeping
print 'Spy is preparing, please wait...!'
time.sleep(1800)
print 'Prepare done, start spying now...!'
# Spy for new tweet from target user
spy_api = tweepy.API(auth_handler=spy_auth)
for status in spy_api.user_timeline(id='CompanyTwitter', count=1):
new_id = status.id
return new_id
The second one, named retweet.py. It's job is idle when the spy is running
and retweet when new status posted.
import time
import tweepy
import random
from spy import spyMode
account_file = 'deparment.txt'
account_line = 0
last_id = 0
new_id = 0
class ExtractAccount(object):
#This part is just to extract token information
pass
if __name__ == '__main__':
while True:
spy_id = spyMode(new_id)
if spy_id != last_id:
print 'Okay, spy see a new tweet ' + str(spy_id) + ' has been
posted!'
for account_line in xrange(deparmentCount):
try:
# Setup user agent
le_consumer_key = 'le_consumer_key'
le_consumer_secret = 'le_consumer_secret'
le_access_token = ExtractAccount().access_token()
le_access_token_secret =
ExtractAccount().access_token_secret()
le_time = time.strftime('%X')
auth = tweepy.OAuthHandler(le_consumer_key,
le_consumer_secret)
auth_url = auth.get_authorization_url()
auth.set_access_token(le_access_token,
le_access_token_secret)
# Create retweet for latest status
api = tweepy.API(auth_handler=auth)
if api.retweet(spy_id):
print 'At ' + le_time + ' a new retweet has been
created!'
except Exception as e:
print e
pass
account_line += 1
# Reset the bot
last_id = spy_id
account_line = 0
print 'Okay, you are good now!\n'
else:
print 'Spy has been sent, but found nothing!\n'
This tool running fine on my Windows machine, but when i upload it to the
company server which use CentOs. The retweet.py could not authenticate as
it said:
[{u message': Could not authenticate you', code': 32}]
Does everyone face this before? I know there are difference between Python
for Windows and Linux but i can't solve this!

No comments:

Post a Comment