Currently I'm working on tweepy and I need to store tweet id in a variable and want it to retain this value when the script is made to run again. I know I can do this using files and db but I want to do it using environment variable. Looking for a nudge in the right direction. I have set the environment variable from the terminal using the command >export en=1 Here is the code that I'm using :
#!/usr/bin/env python
# encoding: utf-8
import tweepy
import time
import random
import os
t=time.time()
#my api keys
consumer_key = "xxx"
consumer_secret = "xxx"
access_key = "xxx"
access_secret = "xxx"
toReply="xxx"
rt=['hello','hi','okay','bye']
#rt=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','you','suck','bruh',':P']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ts=api.user_timeline(screen_name=toReply,count=1)
#g=open('id.txt','r')
#x=g.read()
#g.close()
x=os.environ["en"]
print x
l=[]
#print dir(ts)
#print ts.max_id
#print type(ts.max_id)
def unique(y,l):
for i in range(0,len(l)):
if l[i]==y:
return 0
if int(x)!=ts.max_id:
for tweet in ts:
for i in range(0,len(rt)+1):
y=random.choice(rt)
#print y
if(unique(y,l)!=0):
#print("unique")
api.update_status("@" + toReply + " "+y, in_reply_to_status_id = tweet.id)
#print y
l.append(y)
os.environ["en"]=str(ts.max_id)
print os.environ["en"]
'''
f=open('id.txt','w')
f.write(str(ts.max_id))
f.close()
'''
t1=time.time()-t
print t1