yoga1290

TwtBlog.py

Jun 7th, 2011
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1.  
  2. import time
  3. import urllib
  4. import sys
  5. import os
  6.  
  7. def splitTags(txt):
  8.     res=[]
  9.     while txt.find('#') >-1:
  10.         eInd=txt.find('#')+1
  11.         if (eInd-2>=0 and not txt[eInd-2] in range(ord('a'),ord('z')+1) and not txt[eInd-2] in range(ord('A'),ord('Z')+1) and not txt[eInd-2] in range(ord('0'),ord('9')+1) and not txt[eInd-2] in ['&'] ) or eInd<=0:
  12.             while eInd<len(txt):
  13.                 if ord(txt[eInd]) in range(ord('a'),ord('z')+1) or ord(txt[eInd]) in range(ord('A'),ord('Z')+1) or ord(txt[eInd]) in range(ord('0'),ord('9')+1):
  14.                     eInd+=1
  15.                 else:   break
  16.             if len(txt[txt.index('#')+1 :   eInd])>0 and not res.__contains__(txt[txt.index('#')+1:eInd].lower()):
  17.                 res.append( txt[    txt.index('#')+1    :   eInd ].lower()  )
  18.         txt=txt[eInd+1:]
  19.     return res
  20. if __name__=='__main__':
  21.         map,tags={},[]
  22. #        allTags=""
  23.     username='yoga1290'
  24.         res=open('twt.txt','r').read();
  25.     old_serial=open('lastRE.txt','r').read();
  26.         try:
  27.             tags=open("_tags.txt","r").read().strip().split('\n')
  28.             for t in tags:
  29.                 map[t]=1
  30.         except:
  31.             tags=[]
  32.     if True:
  33.         print '>'
  34.         entries=urllib.urlopen("http://search.twitter.com/search.atom?q=from%3A"+username).read().split("</entry>")
  35.         entries.pop() #last 1 should be empty
  36.         twt,re='',''
  37.         for e in entries:
  38.             re=e[e.index('http://twitter.com/'+username+'/statuses/'):e.rindex('" rel="alternate"/>')]
  39.             if re==old_serial:  #Don't repeat yourself!
  40.                 break
  41.                         twt=e[e.rindex('<title>')+len('<title>'):e.rindex('</title>')]
  42.                         html=twt #e[e.rindex('<content type="html">')+len('<content type="html">'):e.rindex('</content>')]
  43.                         html=html.replace("\r\n","<br>");
  44.                         html=html.replace("\n","<br>");
  45.                         html=html.replace("'","\'");
  46.                         html=html.replace('"','\"');
  47.                        
  48.             res+='twt[n]="'+ html  +'";\n'
  49.            
  50.                         tmp=""
  51.                         for tag in splitTags(twt):
  52.                             if not map.has_key(tag):
  53.                                 tags.append(tag)
  54.                                 map[tag]=tag
  55.  #                               allTags+=","+tag
  56.                             #tmp+=","+tag
  57.                             tmp=""
  58.                             try:
  59.                                 tmp=open(tag,'r').read()
  60.                             except:
  61.                                 tmp=""
  62.                             f=open(tag,'w')
  63.                             f.write(re+'\n'+html+'\n'+tmp)
  64.                             f.close()
  65.  
  66.  
  67.                         f=open("_tags.txt",'w')
  68.                         for t in tags:
  69.                             f.write(t+"\n")
  70.                         f.close()
  71.  
  72. #                        for t in tags:
  73.  #                       if tmp.find(',')>-1:
  74.   #                          tmp=tmp[tmp.find(',')+1:]
  75.    #                     res+='tag[n++]="'+tmp+'";\n\n'
  76.  
  77.        
  78.                 #Memorizing last RE link
  79.         e=entries[0]
  80.         old_serial=e[e.index('http://twitter.com/'+username+'/statuses/'):e.rindex('" rel="alternate"/>')]
  81.                 out=open('lastRE.txt','w')
  82.         out.write(old_serial)
  83.         out.close()
  84.            
  85.  
  86.                 #Relax for a while
  87.                 print '<'
  88. #                out=open('twt.txt','w')
  89. #       out.write(res+'\nalltg="'+allTags+'";\n')
  90. #       out.close()
  91. #       time.sleep(10*60)
Advertisement
Add Comment
Please, Sign In to add comment