Advertisement
Guest User

Get data from iacr

a guest
Jan 19th, 2012
1,590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.63 KB | None | 0 0
  1. import sqlite3
  2. import sys
  3. import urllib
  4. import datetime
  5. import os
  6. import pickle
  7. import itertools
  8.  
  9. def create_table(nodes=None,edges=None):
  10.     db_file_name="/tmp/example.sqlite"
  11.    
  12.     if os.path.isfile(db_file_name):
  13.         os.remove(db_file_name)
  14.  
  15.     db = sqlite3.connect(db_file_name)
  16.  
  17.     db.text_factory = str
  18.     cur = db.cursor()
  19.    
  20.     cur.executescript("""
  21.        create table nodes(
  22.            id,
  23.            label,
  24.            x,
  25.            y,
  26.            size
  27.        );
  28.    
  29.        create table edges(
  30.            source,
  31.            target,
  32.            label,
  33.            weight
  34.        );
  35.        """)
  36.    
  37.     c = db.cursor()
  38.    
  39.     nodes=nodes.items()
  40.    
  41.     for i in xrange(len(nodes)):
  42.         c.execute('insert into nodes values (?,?,?,?,?)', (str(nodes[i][1]),str(nodes[i][0]),str(i),str(i),1))
  43.  
  44.     edges=edges.items()
  45.    
  46.     for i in xrange(len(edges)):
  47.         c.execute('insert into edges values (?,?,?,?)', (str(edges[i][0]),str(edges[i][1]),'','1'))
  48.    
  49.     db.commit()
  50.    
  51.     c.close()
  52.  
  53. def fetch_data_from_iacr(**kwargs):
  54.     year_start=kwargs.get('year_start',1996)
  55.     year_end=kwargs.get('year_end',datetime.datetime.now().year)
  56.     field=kwargs.get('field',"author")
  57.     path_to_pkl=kwargs.get('path_to_pkl',None)
  58.  
  59.     nodes={}
  60.     edges={}
  61.    
  62.     stop=3
  63.    
  64.     if path_to_pkl is not None:
  65.         pkl_file = open(path_to_pkl, 'rb')
  66.         all_authors = pickle.load(pkl_file)
  67.         pkl_file.close()
  68.  
  69.     for year in xrange(year_start,year_end+1):
  70.         if path_to_pkl is not None:
  71.             for s in all_authors[year]:
  72.                 authors=s
  73.                
  74.                 authors=authors.replace(", and ", ",")
  75.                 authors=authors.replace(" and ", ",")
  76.                 authors=authors.replace("  ", "")
  77.                 authors=authors.replace(", ", ",")
  78.                
  79.                 authors=authors.split(",")
  80.                
  81.                 for a in authors:
  82.                     if a not in nodes:
  83.                         nodes[a]=len(nodes)
  84.    
  85.                 for a in itertools.combinations(authors, 2):
  86.                     edges[nodes[a[0]]]=nodes[a[1]]
  87.    
  88.                 #print "{0}".format(authors)
  89.                 #print "{0} ({1})".format(authors,"http://eprint.iacr.org/cgi-bin/cite.pl?entry={0}/{1}".format(year,str(num_article).zfill(3)))
  90.         else:
  91.             num_article=1
  92.             while (1):
  93.                 sys.stdout.write("{0}: {1}\r".format(year,num_article))
  94.                 sys.stdout.flush()
  95.                 f = urllib.urlopen("http://eprint.iacr.org/cgi-bin/cite.pl?entry={0}/{1}".format(year,str(num_article).zfill(3)))
  96.                 s = f.read()
  97.                 f.close()
  98.    
  99.                 s=str(s)
  100.                
  101.                 pos=s.find(field)
  102.                
  103.                 if pos == -1:
  104.                     num_article = num_article + 1
  105.                     stop=stop - 1
  106.                     if stop == 0:
  107.                         break
  108.                     continue
  109.                 else:
  110.                     stop=3
  111.                
  112.                 s=s[pos:]
  113.                 s=s[:s.find("\n")]
  114.    
  115.                 pos=len("{0} = {1}".format(field,"{"))
  116.                
  117.                 pos_end=len(s)-1
  118.    
  119.                 while s[pos_end] != '}':
  120.                     pos_end=pos_end-1
  121.    
  122.                 authors=s[pos:pos_end]
  123.                
  124.                 authors=authors.replace(", and ", ",")
  125.                 authors=authors.replace(" and ", ",")
  126.                 authors=authors.replace("  ", "")
  127.                 authors=authors.replace(", ", ",")
  128.                
  129.                 authors=authors.split(",")
  130.  
  131.                 for a in authors:
  132.                     if a not in nodes:
  133.                         nodes[a]=len(nodes)
  134.    
  135.                 for a in itertools.combinations(authors, 2):
  136.                     edges[nodes[a[0]]]=nodes[a[1]]
  137.    
  138.                 #print "{0}".format(authors)
  139.                 #print "{0} ({1})".format(authors,"http://eprint.iacr.org/cgi-bin/cite.pl?entry={0}/{1}".format(year,str(num_article).zfill(3)))
  140.                
  141.                 num_article = num_article + 1
  142.    
  143.             sys.stdout.write("\n")
  144.  
  145.     return [nodes,edges]
  146.  
  147. def blob_all_data_from_iacr(**kwargs):
  148.     year_start=kwargs.get('year_start',1996)
  149.     year_end=kwargs.get('year_end',datetime.datetime.now().year)
  150.     field=kwargs.get('field',"author")
  151.  
  152.     all_authors={}
  153.     stop=3
  154.  
  155.     for year in xrange(year_start,year_end+1):
  156.         num_article=1
  157.         all_authors[year]=[]
  158.         while (1):
  159.             sys.stdout.write("{0}: {1}\r".format(year,num_article))
  160.             sys.stdout.flush()
  161.             f = urllib.urlopen("http://eprint.iacr.org/cgi-bin/cite.pl?entry={0}/{1}".format(year,str(num_article).zfill(3)))
  162.             s = f.read()
  163.             f.close()
  164.  
  165.             s=str(s)
  166.            
  167.             pos=s.find(field)
  168.            
  169.             if pos == -1:
  170.                 num_article = num_article + 1
  171.                 stop=stop - 1
  172.                 if stop == 0:
  173.                     break
  174.                 continue
  175.             else:
  176.                 stop=3
  177.  
  178.             s=s[pos:]
  179.             s=s[:s.find("\n")]
  180.  
  181.             pos=len("{0} = {1}".format(field,"{"))
  182.            
  183.             pos_end=len(s)-1
  184.  
  185.             while s[pos_end] != '}':
  186.                 pos_end=pos_end-1
  187.  
  188.             authors=''
  189.            
  190.             while pos != pos_end:
  191.                 authors=authors+s[pos]
  192.                 pos=pos+1
  193.            
  194.             all_authors[year].append(authors)
  195.  
  196.             num_article = num_article + 1
  197.  
  198.         sys.stdout.write("\n")
  199.  
  200.     output = open('/tmp/all_authors.pkl', 'wb')
  201.  
  202.     pickle.dump(all_authors, output)
  203.  
  204.     output.close()
  205.  
  206. def main():
  207.     path_to_pkl='/tmp/all_authors.pkl'
  208.     #path_to_pkl=None
  209.     year_start=1996
  210.     year_end=2012
  211.  
  212.     if path_to_pkl is not None:
  213.         if not os.path.isfile(path_to_pkl):
  214.             blob_all_data_from_iacr(year_start=year_start,year_end=year_end)
  215.         else:
  216.             pkl_file = open(path_to_pkl, 'rb')
  217.             all_authors = pickle.load(pkl_file)
  218.             pkl_file.close()
  219.             for year in xrange(year_start,year_start+1):
  220.                 if not year in all_authors:
  221.                     print "Only year {0} is presented in file.".format(year-1)
  222.                     return -1
  223.                     break
  224.  
  225.     [nodes,edges]=fetch_data_from_iacr(field="author",year_start=year_start,year_end=year_end,path_to_pkl=path_to_pkl)
  226.     create_table(nodes,edges)
  227.  
  228. if __name__ == "__main__":
  229.     sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement