Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. import cx_Oracle
  2. import sys
  3. import random
  4. import datetime
  5. logq = ("select count(*) "
  6. "from users u "
  7. "where u.usr =:u_id and u.pwd =:psw");
  8. r1 = ( "select count(*) "
  9. "from users u "
  10. "where u.usr =:u_id")
  11. r2 = ("insert into users "
  12. "values (:u_id, :u_psw, :u_name, :u_email, :u_city, :u_timezone)");
  13. re1 = ("insert into retweets "
  14. "values (:u_id, :t_id,:r_date) ");
  15. tweet_menu = ("select distinct t.tid, t.writer, t.tdate, t.text "
  16. "from follows f, tweets t,retweets r "
  17. "where f.flwer =:u_id "
  18. "and (f.flwee = t.writer or (f.flwee = r.usr and r.tid=t.tid)) "
  19. "order by t.tdate desc ");
  20. reply_number=( "select count(*) from tweets where tweets.replyto =:t_id ")
  21. retweets_number=( "select count(*) from retweets where retweets.tid =:t_id ")
  22. c1 = ( "select count(*) "
  23. "from tweets t "
  24. "where t.tid =:t_id")
  25. hashq =("select count(*) "
  26. "from hashtags h "
  27. "where h.term =:h_t ")
  28. hasha = ("insert into hashtags "
  29. "values (:h_t ) ")
  30. c2=("insert into tweets "
  31. "values (:t_id, :u_id, :t_date, :t_text,:r_to)");
  32. m1=("insert into mentions "
  33. "values (:t_id, :m_term ) ")
  34. search_t=("select distinct t.tid, t.writer, t.tdate, t.text "
  35. "from tweets t, hashtags h, mentions m "
  36. "where t.text LIKE :key_word OR (m.term LIKE :key_word and m.tid = t.tid) "
  37. "order by t.tdate desc " )
  38. search_u_n = ( "SELECT DISTINCT u.usr,u.name,u.city "
  39. "FROM users u "
  40. "WHERE u.name LIKE :key_word "
  41. "ORDER BY length(trim(u.name)) asc ")
  42. search_u_c = ( "SELECT DISTINCT u.usr,u.name,u.city "
  43. "FROM users u "
  44. "WHERE u.city LIKE :key_word "
  45. "ORDER BY length(trim(u.city)) asc ")
  46. u_number = ("SELECT COUNT(distinct t.tid), count(distinct f1.flwee), count(distinct f2.flwer) "
  47. "FROM tweets t,follows f1, follows f2 "
  48. "WHERE t.writer =:new_id and f1.flwer =:new_id and f2.flwee =:new_id ")
  49. u_tweets=("SELECT DISTINCT t.tid, t.tdate, t.text "
  50. "FROM tweets t, users u "
  51. "WHERE t.writer =:new_id "
  52. "ORDER BY t.tdate desc ")
  53. u_follow = ("insert into follows values (:u_id,:new_id,:new_date)")
  54. list_follow=("select u.usr,u.name "
  55. "FROM users u, follows f "
  56. "where f.flwee =:u_id "
  57. "and f.flwer =u.usr ")
  58. def login(connection,curs):
  59. reg=input("Login as register?[Y/N](exit:E):")
  60. flag=True
  61. if reg=="E":
  62.  
  63.  
  64. flag=False
  65. elif reg=="Y":
  66. u_id=input("Please enter your user id:")
  67. psw=input("Please enter your psw:")
  68. para={'u_id':u_id,'psw':psw}
  69.  
  70. curs.execute(logq,para);
  71. vaild=(curs.fetchall())[0][0]
  72. if(vaild==1):
  73. print("Aprove")
  74. flag=menu(connection,curs,u_id)
  75.  
  76. else:
  77. print("Invaild password or u_id")
  78.  
  79. elif reg == "N":
  80. unique=1
  81. while (unique == 1):
  82. u_id = random.randint(0,1000000);
  83. par={'u_id':u_id}
  84. curs.execute(r1,par)
  85. unique=(curs.fetchall())[0][0]
  86.  
  87. u_name=input("Please enter your name : ")
  88. u_psw=input("Please enter your password : ")
  89. u_email=input("Please enter your email : ")
  90. u_city=input("Please enter your city : ")
  91. u_timezone=input("Please enter your timezone : ")
  92. param = {"u_id": u_id, "u_psw": u_psw, "u_name": u_name, "u_email": u_email, "u_city":u_city, "u_timezone": u_timezone};
  93. curs.execute(r2,param);
  94. print("Your id is :",u_id);
  95. connection.commit();
  96.  
  97. return flag
  98. def menu(connection,curs,u_id):
  99. flag=True
  100. print("The recent tweets:[ID] [Writer] [Time] [TexT]")
  101. param={'u_id':u_id}
  102. curs.execute(tweet_menu,param)
  103. move_on=1
  104. while (move_on==1):
  105.  
  106. for i in range(0,5):
  107. r=curs.fetchone()
  108.  
  109.  
  110. if (r==None):
  111. print("Thats all")
  112. break
  113. print(r[0],r[1],r[2],r[3])
  114. if(r==None):
  115. break
  116. move_on= int(input("Type 1 to see more:"))
  117. print("")
  118.  
  119.  
  120. while(flag==True):
  121. print("")
  122. action=input("Please select one of the action:1.check the tweet;2.Search for a tweet;"
  123. "3.Search for user;4.Compose a tweet;5.List followers;6.manage lists;E.exit: ");
  124. print("")
  125. if(action=='1'):
  126. check_tweet(connection,curs,u_id)
  127. elif(action=='2'):
  128. search_for_tweet(connection,curs,u_id)
  129. elif(action=='3'):
  130. search_for_user(connection,curs,u_id)
  131.  
  132. elif(action=='4'):
  133.  
  134. compose_tweet(connection,curs,u_id)
  135. elif(action=='5'):
  136. list_follower(connection,curs,u_id)
  137. elif(action=='E'):
  138. flag=False
  139. return flag
  140. def search_for_tweet(connection,curs,u_id):
  141. keyword=input("Please enter the keyword for search: ")
  142. paras={'key_word':'%'+keyword+'%'}
  143. curs.execute(search_t,paras)
  144. move_on=1
  145. while (move_on==1):
  146.  
  147. for i in range(0,5):
  148. r=curs.fetchone()
  149.  
  150.  
  151. if (r==None):
  152. print("Thats all")
  153. break
  154. print(r[0],r[1],r[2],r[3])
  155. if(r==None):
  156. break
  157. move_on= int(input("Type 1 to see more:"))
  158. print("")
  159. check=input("Check any tweet?[Y/N] :")
  160. if(check=='Y'):
  161. check_tweet(connection,curs,u_id)
  162. def search_for_user(connection,curs,u_id):
  163. keyword=input("Please enter the keyword for search: ")
  164. parasun={'key_word':'%'+keyword+'%'}
  165. curs.execute(search_u_n,parasun)
  166. move_on=1
  167. while (move_on==1):
  168.  
  169. for i in range(0,5):
  170. r=curs.fetchone()
  171.  
  172.  
  173. if (r==None):
  174.  
  175. break
  176. print(r[0],r[1],r[2])
  177. if(r==None):
  178. break
  179. move_on= int(input("Type 1 to see more:"))
  180. curs.execute(search_u_c,parasun)
  181. move_on=1
  182. while (move_on==1):
  183. for i in range(0,5):
  184. r=curs.fetchone()
  185.  
  186. if (r==None):
  187. print("Thats all")
  188. break
  189. print(r[0],r[1],r[2])
  190. if(r==None):
  191. break
  192. move_on= int(input("Type 1 to see more:"))
  193. check=input("Check any users?[Y/N] :")
  194. if(check=='Y'):
  195. check_user(connection,curs,u_id,1)
  196. def check_user(connection,curs,u_id,mode):
  197. new_id=input("Please input the id of the user: ");
  198. param={'new_id':new_id}
  199. curs.execute(u_number,param)
  200. r=curs.fetchall();
  201.  
  202. print("Tweets: ",r[0][0]," Flowee: ",r[0][1],"Floweer ", r[0][2])
  203. curs.execute(u_tweets,param);
  204. print("Resent tweets: ")
  205. print("")
  206. move_on=1
  207. while (move_on==1):
  208. for i in range(0,3):
  209. r=curs.fetchone()
  210.  
  211. if (r==None):
  212. print("Thats all")
  213. break
  214. print(r[0],r[1],r[2])
  215. if(r==None):
  216. break
  217. move_on= int(input("Type 1 to see more:"))
  218. if(mode==1):
  219. follow=input("Follow ther user?[Y/N] :")
  220. if(follow=='Y'):
  221. paraf={'u_id':u_id,'new_id':new_id,'new_date':datetime.datetime.now()}
  222. curs.execute(u_follow,paraf)
  223. connection.commit()
  224. print("Follow success! ")
  225. def compose_tweet(connection,curs,u_id):
  226. unique=1
  227. while (unique == 1):
  228. newt_id = random.randint(0,1000000);
  229. par={'t_id':newt_id}
  230. curs.execute(c1,par)
  231. unique=(curs.fetchall())[0][0]
  232. text=input("Please input the text: ")
  233. s=text.split(" ")
  234. hashtags=[]
  235. for i in s:
  236. if('#'in i):
  237. hashtags.append(i);
  238. for p in hashtags:
  239. p=p[1:]
  240. parah={'h_t':p}
  241. curs.execute(hashq,parah)
  242. exist=(curs.fetchall())[0][0]
  243. if(exist==0):
  244. curs.execute(hasha,parah)
  245. connection.commit();
  246. parat={'t_id':newt_id,'u_id':u_id,'t_date':datetime.datetime.now(),'t_text':text,'r_to':None}
  247. curs.execute(c2,parat);
  248. connection.commit()
  249. for m in hashtags:
  250. m=m[1:]
  251. param={'t_id':newt_id,'m_term':m}
  252. curs.execute(m1,param)
  253. connection.commit()
  254. print("Compose success in id: ",newt_id)
  255. def list_follower(connection,curs,u_id):
  256. paralf={'u_id':u_id}
  257. curs.execute(list_follow,paralf)
  258. rows= curs.fetchall()
  259. for row in rows:
  260. print(row[0],row[1])
  261. check=input("Check any users?[Y/N] :")
  262. if(check=='Y'):
  263. check_user(connection,curs,u_id,0)
  264.  
  265.  
  266. def check_tweet(connection,curs,u_id):
  267. t_id=input("Please input the id of the tweet: ");
  268. param={'t_id':t_id}
  269. curs.execute(reply_number,param)
  270. r=curs.fetchall();
  271. print("")
  272. print("replies:",r[0][0])
  273. curs.execute(retweets_number,param)
  274. r=curs.fetchall();
  275. print("retweets:",r[0][0])
  276. print("")
  277. selection=input("Do you want reply/retweets it? [retweets[1]/reply[2]]:");
  278. if(selection=='2'):
  279. unique=1
  280. while (unique == 1):
  281. newt_id = random.randint(0,1000000);
  282. par={'t_id':newt_id}
  283. curs.execute(c1,par)
  284. unique=(curs.fetchall())[0][0]
  285. text=input("Please input the text: ")
  286. s=text.split(" ")
  287. hashtags=[]
  288. for i in s:
  289. if('#'in i):
  290. hashtags.append(i);
  291. for p in hashtags:
  292. p=p[1:]
  293. parah={'h_t':p}
  294. curs.execute(hashq,parah)
  295. exist=(curs.fetchall())[0][0]
  296. if(exist==0):
  297. curs.execute(hasha,parah)
  298. connection.commit();
  299. parat={'t_id':newt_id,'u_id':u_id,'t_date':datetime.datetime.now(),'t_text':text,'r_to':t_id}
  300. curs.execute(c2,parat);
  301. connection.commit()
  302. for m in hashtags:
  303. m=m[1:]
  304. param={'t_id':newt_id,'m_term':m}
  305. curs.execute(m1,param)
  306. connection.commit()
  307. print("Reply success in id: ",newt_id)
  308. elif(selection=='1'):
  309. parar={'u_id':u_id,'t_id':t_id,'r_date':datetime.datetime.now()}
  310. curs.execute(re1,parar)
  311. print("retweets success!")
  312. connection.commit()
  313. def main():
  314. # my code here
  315. connection = cx_Oracle.connect("zijian1", "hzj961103", 'gwynne.cs.ualberta.ca:1521/CRS')
  316. curs = connection.cursor()
  317. loop=True
  318. while(loop==True):
  319. loop = login(connection,curs)
  320. curs.close
  321. connection.close()
  322. sys.exit()
  323.  
  324. if __name__ == "__main__":
  325. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement