Guest User

Untitled

a guest
Jan 29th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. import MySQLdb as dbapi
  2. import sys
  3. import csv
  4. import json
  5. import requests
  6. import urllib2
  7.  
  8. # dbServer='localhost'
  9. # dbPass='supersecretpassword'
  10. # dbSchema='dbTest'
  11. # dbUser='root'
  12. api_endpoint = 'http://puja_api.sportskeeda.com/v1/author/'
  13. dbServer='localhost'
  14. dbPass=''
  15. dbSchema='sportskeeda'
  16. dbUser='sportskeeda'
  17. database_location="/var/lib/mysql/sportskeeda"
  18. conn = dbapi.connect(host=dbServer,user=dbUser,passwd=dbPass,db="sportskeeda")
  19. c = conn.cursor()
  20. # print "done"
  21.  
  22. dbQuery = "SELECT distinct author_id FROM keeda_author_fact WHERE author_post_count>0 ORDER BY author_post_count DESC LIMIT 0,50000;"
  23. c.execute(dbQuery)
  24. results = c.fetchall()
  25.  
  26. authors = []
  27. authors_final = []
  28. api_array = []
  29. for result in results:
  30.  
  31. authors.append(result)
  32. for author in authors:
  33. author1 = str(author)
  34. auth = author1.split(',')
  35. auth2 = auth[0].split('(')[1]
  36. # print auth2
  37. authors_final.append(auth2)
  38. # print authors_final
  39. # with open('/home/puja/Documents/fb-api-test/test.csv', 'w') as outfile:
  40. # writer = csv.writer(outfile)
  41. # writer.writerow(['Author Id', 'author email', 'author nicename', 'posts published', 'reads received', 'editors pick', 'last published date'])
  42. for ids in authors_final:
  43. url_to_hit = api_endpoint + ids
  44. url_to_hit_2 = "http://puja_api.sportskeeda.com/v1/author_posts/" + ids
  45. # print url_to_hit
  46. # response = urllib2.urlopen('api.sportskeeda.com/v1/author/4390L')
  47. # data = json.load(response)
  48. response = requests.get(url_to_hit)
  49. json_data = json.loads(response.text)
  50. response2 = requests.get(url_to_hit_2)
  51. json_data2 = json.loads(response2.text)
  52. # print json_data["fan_title"]
  53. author_id = ids
  54. if 'user_email' in json_data.keys():
  55. user_email = json_data["user_email"]
  56. else:
  57. user_email = ' '
  58. if 'user_nicename' in json_data.keys():
  59. user_nicename = json_data["user_nicename"]
  60. else:
  61. user_nicename = ' '
  62. if 'author_url' in json_data.keys():
  63. author_url = json_data["author_url"]
  64. else:
  65. author_url = ' '
  66. if 'posts_published' in json_data.keys():
  67. posts_published = json_data["posts_published"]
  68. else:
  69. posts_published = 0
  70. if 'editors_pick' in json_data.keys():
  71. editors_pick = json_data["editors_pick"]
  72. else:
  73. editors_pick = 0
  74. if 'reads_received' in json_data.keys():
  75. reads_received = json_data["reads_received"]
  76. else:
  77. reads_received = 0
  78. # if "post_date_formatted" in json_data2[0].keys():
  79. # print json_data2[0]
  80. count = len(json_data2)
  81. # print count
  82. if count>0:
  83. if json_data2[0] and 'post_date_formatted' in json_data2[0].keys():
  84. last_published_date = json_data2[0]["post_date_formatted"]
  85. else:
  86. print "foo"
  87. last_published_date = 0
  88. print user_email, user_nicename, posts_published, reads_received, editors_pick, last_published_date
  89. with open('/home/puja/Documents/fb-api-test/test2.csv', 'a') as outfile:
  90. writer = csv.writer(outfile)
  91. writer.writerow([author_id, user_email, user_nicename, author_url, posts_published, editors_pick, reads_received, last_published_date])
  92. # fields = ['Author Id', 'post count','total reads', 'points(includes editors picks)', 'author name', 'author email']
  93. # writer = csv.DictWriter(outfile)
  94.  
  95. # writer.writeheader()
  96. # for x in result:
  97. # writer.writerow(x)
  98. def get_all_author_ids():
  99. bServer='localhost'
  100. dbPass=''
  101. dbSchema='sportskeeda'
  102. dbUser='sportskeeda'
  103. database_location="/var/lib/mysql/sportskeeda"
  104. conn = dbapi.connect(host=dbServer,user=dbUser,passwd=dbPass,db="sportskeeda")
  105. c = conn.cursor()
  106. # print "done"
  107. array = []
  108. dbQuery = "SELECT distinct author_id FROM keeda_author_fact ORDER BY author_post_count DESC LIMIT 10;"
  109. c.execute(dbQuery)
  110. results = c.fetchall()
  111. while result in results:
  112. array.append(result)
  113. print array
  114.  
  115. def get_all_data_from_db():
  116. bServer='localhost'
  117. dbPass=''
  118. dbSchema='sportskeeda'
  119. dbUser='sportskeeda'
  120. database_location="/var/lib/mysql/sportskeeda"
  121. conn = dbapi.connect(host=dbServer,user=dbUser,passwd=dbPass,db="sportskeeda")
  122. c = conn.cursor()
  123.  
  124.  
  125.  
  126. # get_all_author_ids()
  127. # print "done"
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. # print url_to_hit
  145. # api_array.append(url_to_hit)
  146. # response = requests.get(url_to_hit)
  147. # print "foo"
  148. # json_data = (response.text)
  149. # print json_data
  150.  
  151. # outfile = open( "/home/puja/Documents/fb-api-test", "w" )
  152.  
  153. # writer = csv.writer( outfile )
  154.  
  155. # write header
  156.  
  157.  
  158.  
  159. # with open('/home/puja/Documents/fb-api-test/test.csv', 'w') as outfile:
  160. # # fields = ['Author Id', 'post count','total reads', 'points(includes editors picks)', 'author name', 'author email']
  161. # # writer = csv.DictWriter(outfile)
  162. # writer = csv.writer(outfile)
  163. # writer.writerow(['Author Id', 'post count','total reads', 'points(includes editors picks)', 'author name', 'author email'])
  164. # # writer.writeheader()
  165. # for x in result:
  166. # writer.writerow(x)
  167. # print "done"
Add Comment
Please, Sign In to add comment