User:Tryitbot/source

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

<source lang=python>



import string, re import wikipedia import unicodedata import wikipedia, sys

  1. import marshal

import os import time,datetime


nowTime = time.mktime( datetime.datetime.utcnow().timetuple() ) timeRE = re.compile('(\d\d):(\d\d), (\d\d?) (January|February|March|April|May|June|July|August|September|October|November|December) (\d\d\d\d) \((UTC|GMT)\)')


  1. if os.path.exists("marshal_votea"):
  2. fh = open("marshal_votes","r+b")
# stuff = marshal.loads(fh.read())
# print stuff
  1. else :

def countVotes(textv,supoppnet,users, user) :

  supoppnet[3] = 0
  somei= textv.find('Image:Symbol support vote.svg') 
  if somei > 0 :
            
    if users.find(user) < 0: 
        supoppnet[0] = supoppnet[0] + 1
        #print user
        supoppnet[3] = 1
            
  
  somei= textv.find('Image:Symbol oppose vote.svg') 
  if somei > 0 :
    if users.find(user) < 0: 
        supoppnet[1] = supoppnet[1] + 1
        supoppnet[3] = 1

  somei= textv.find('Image:Symbol neutral vote.svg') 
  if somei > 0 :
    if users.find(user) < 0: 
          supoppnet[2] = supoppnet[2] + 1
          supoppnet[3] = 1
 # print 'sup=', supoppnet
  return supoppnet          
 

stuff = ['a','b'] print stuff[0]

site = wikipedia.getSite()


  1. page = wikipedia.Page(site, u"Commons:Featured picture candidates/candidate list")

page = wikipedia.Page(site, u"User:Mbz1/Commons:Featured picture candidates/candidate list test") text = page.get() con = text.count ( 'Commons:Featured picture candidates' )

nominations = text.split('LIST OF NOMINATIONS -->') nominations = nominations [1] nominations1=nominations.split('{{')

for i in range(1, con):

 nominations1[i]=nominations1[i].strip()
 nomname=nominations1[i][0:len(nominations1[i]) - 2]
 print len(nominations1[i])
 print nomname
 #sys.exit() 
 page1 = wikipedia.Page(site, nomname)
 text1 = page1.get()
 originaltext = text1
 originallistOfimages=originaltext.split("===")
 text1 = text1.replace(' ',)
 # Pro and kontra are redirect pages. Before I replaced them with Support and oppose I was getting error in a program Wikipedia.py.
 # I do not like hardcoding. Maybe something could be done to work with redirect pages.
 text1 = text1.replace('pro','support')
 text1 = text1.replace('kontra','oppose')
 # handle situations, when a user adds some wording to s vote inside {{}}
 #text1 = text1.replace('|','}}')
 #text1 = text1.replace('====','===')
 #Split a nomination in case there are few edits within the same nomination

 listOfimages=text1.split("===")

 for imageindex in range (0,len(listOfimages)) :

   text1 = listOfimages[imageindex]
  
   if text1.find('}}') > 0 and text1.find('result:) < 0 :
     # print text1 
   #if'}}' in text1  and  ("'result:" in text1) < 0 : 
      votes = text1.split('{{')
      votecon = len(votes) 
      supoppnet = [0,0,0,0]
      users = 

      for i1 in range(0, votecon):


        user=votes[i1][votes[i1].find("User:"):votes[i1].find("]]")+1]
       
        user=user.upper()
        #The string "users" is used to disregard a second vote from the same user.

       # Print votes[i1]
        
        vote=votes[i1][0:votes[i1].find("}}")]
       # print 'votes =' ,votes[i1]
        if '' in votes[i1]  and ('' in votes[i1]) == False:
          vote = 'Info'
       

        #get  text from a nomination to compile a template page name. At this point I do not care, if it is INFO, Comment or a real vote
        #print "vote =" , vote
  1. sys.exit()
        vote = vote.split('|')
      
        vote = vote[0]
        vote=vote.strip()
        if len(vote) == 0 :
           vote = 'Info'
        #print 'votes=', vote, ' ', len(vote)


        templatevote = "template:"+vote
        
        if (templatevote in stuff)  == False:


          pagev = wikipedia.Page(site, templatevote)

    
          if pagev.exists() : 

            textv = pagev.get()
            #print 'textv', textv
 
            stuff.append(templatevote) 
            stuff.append(textv)
            isuser = len(user)  
            if isuser <> 0 :
               supoppnet = countVotes(textv,supoppnet,users, user )


            if supoppnet [3] == 1 :
              users = users + user


          else :
             print templatevote
        else :
            textvind=stuff.index(templatevote)  + 1
            textv = stuff[textvind]   
            
            isuser = len(user)  
            if isuser <> 0 :
               supoppnet = countVotes(textv,supoppnet,users, user )
           
            if supoppnet [3] == 1 :
              users = users + user
            print users, supoppnet


      print "number=" , i
      print 'supports=',  supoppnet[0]
      print 'opposes=', supoppnet[1]
      print 'netural=', supoppnet[2]
      
      # here result text should go. I was going to compile the real text later. First I wanted to make sure it is post in the right place.
      
      originallistOfimages[ imageindex]  += "\n" +" result text " + "\n"
     

      page1.put(originaltext)




else:

  print 'The for loop is over'
  
  #fh.dump("marshal_votea", stuff)
 # fh.seek(0)
 # marshal.dump(stuff,fh)
 # marshal.dump(stuff, fh.asFile())
 # fh.close()
  #os.remove("marshal.dat")