Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.71 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. list_list=("select * "
  59. "FROM lists "
  60. "Where lists.owner =:u_id")
  61. list_add=("insert into includes values (:l_name,:new_id) ")
  62. list_del=("DELETE from includes "
  63. "WHERE member =:de_id "
  64. "AND lname =:l_name ")
  65. list_list_in=("SELECT i.lname "
  66. "FROM includes i "
  67. "WHERE i.member =:u_id ")
  68. list_create=("insert into lists values (:l_name, :u_id) ")
  69. def login(connection,curs):
  70. reg=input("Login as register?[Y/N](exit:E):")
  71. flag=True
  72. if reg=="E":
  73.  
  74.  
  75. flag=False
  76. elif reg=="Y":
  77. u_id=input("Please enter your user id:")
  78. psw=input("Please enter your psw:")
  79. para={'u_id':u_id,'psw':psw}
  80.  
  81. curs.execute(logq,para);
  82. vaild=(curs.fetchall())[0][0]
  83. if(vaild==1):
  84. print("Aprove")
  85. flag=menu(connection,curs,u_id)
  86.  
  87. else:
  88. print("Invaild password or u_id")
  89.  
  90. elif reg == "N":
  91. unique=1
  92. while (unique == 1):
  93. u_id = random.randint(0,1000000);
  94. par={'u_id':u_id}
  95. curs.execute(r1,par)
  96. unique=(curs.fetchall())[0][0]
  97.  
  98. u_name=input("Please enter your name : ")
  99. u_psw=input("Please enter your password : ")
  100. u_email=input("Please enter your email : ")
  101. u_city=input("Please enter your city : ")
  102. u_timezone=input("Please enter your timezone : ")
  103. 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};
  104. curs.execute(r2,param);
  105. print("Your id is :",u_id);
  106. connection.commit();
  107.  
  108. return flag
  109. def menu(connection,curs,u_id):
  110. flag=True
  111. print("The recent tweets:[ID] [Writer] [Time] [TexT]")
  112. param={'u_id':u_id}
  113. curs.execute(tweet_menu,param)
  114. move_on=1
  115. while (move_on==1):
  116.  
  117. for i in range(0,5):
  118. r=curs.fetchone()
  119.  
  120.  
  121. if (r==None):
  122. print("Thats all")
  123. break
  124. print(r[0],r[1],r[2],r[3])
  125. if(r==None):
  126. break
  127. move_on= int(input("Type 1 to see more:"))
  128. print("")
  129.  
  130.  
  131. while(flag==True):
  132. print("")
  133. action=input("Please select one of the action:1.check the tweet;2.Search for a tweet;"
  134. "3.Search for user;4.Compose a tweet;5.List followers;6.manage lists;E.exit: ");
  135. print("")
  136. if(action=='1'):
  137. check_tweet(connection,curs,u_id)
  138. elif(action=='2'):
  139. search_for_tweet(connection,curs,u_id)
  140. elif(action=='3'):
  141. search_for_user(connection,curs,u_id)
  142.  
  143. elif(action=='4'):
  144.  
  145. compose_tweet(connection,curs,u_id)
  146. elif(action=='5'):
  147. list_follower(connection,curs,u_id)
  148. elif(action=='6'):
  149. manage_list(connection,curs,u_id)
  150. elif(action=='E'):
  151. flag=False
  152. return flag
  153. def search_for_tweet(connection,curs,u_id):
  154. keyword=input("Please enter the keyword for search: ")
  155. keys=keyword.split(" ")
  156. for key in keys:
  157. paras={'key_word':'%'+key+'%'}
  158. curs.execute(search_t,paras)
  159. move_on=1
  160. while (move_on==1):
  161.  
  162. for i in range(0,5):
  163. r=curs.fetchone()
  164.  
  165.  
  166. if (r==None):
  167. print("Thats all")
  168. break
  169. print(r[0],r[1],r[2],r[3])
  170. if(r==None):
  171. break
  172. move_on= int(input("Type 1 to see more:"))
  173. print("")
  174. check=input("Check any tweet?[Y/N] :")
  175. if(check=='Y'):
  176. check_tweet(connection,curs,u_id)
  177. def manage_list(connection,curs,u_id):
  178. selection=input("Please choose the action: 1.check lists I owned 2.check lists I am in 3.Create a list 4.Manage list: ")
  179. if(selection=='1'):
  180. paral={'u_id':u_id}
  181. curs.execute(list_list,paral)
  182. rows= curs.fetchall()
  183. for row in rows:
  184. print(row[0])
  185. elif(selection=='2'):
  186. paral={'u_id':u_id}
  187. curs.execute(list_list_in,paral)
  188. rows= curs.fetchall()
  189. for row in rows:
  190. print(row[0])
  191. elif(selection=='3'):
  192. lname=input("Please input the list name you want to create:")
  193. parac={'u_id':u_id,'l_name':lname}
  194. curs.execute(list_create,parac)
  195. connection.commit()
  196. print("List created!")
  197. elif(selection=='4'):
  198. lname=input("Please input the list name you want to manage:")
  199. ad=input("Please input the action (D) Delete member (A) Add Member: ")
  200. if(ad=='A'):
  201. new_id=int(input("Please input the Member id:"))
  202. paraa={'l_name':lname,'new_id':new_id}
  203. curs.execute(list_add,paraa);
  204. connection.commit()
  205. print("Add success")
  206. elif(ad=='D'):
  207. de_id =int(input("Please input the Member id:"))
  208. paradele={'de_id':de_id,'l_name':lname}
  209. curs.execute(list_del,paradele);
  210. connection.commit()
  211. print("Delete success")
  212. def search_for_user(connection,curs,u_id):
  213. keyword=input("Please enter the keyword for search: ")
  214. parasun={'key_word':'%'+keyword+'%'}
  215. curs.execute(search_u_n,parasun)
  216. move_on=1
  217. while (move_on==1):
  218.  
  219. for i in range(0,5):
  220. r=curs.fetchone()
  221.  
  222.  
  223. if (r==None):
  224.  
  225. break
  226. print(r[0],r[1],r[2])
  227. if(r==None):
  228. break
  229. move_on= int(input("Type 1 to see more:"))
  230. curs.execute(search_u_c,parasun)
  231. move_on=1
  232. while (move_on==1):
  233. for i in range(0,5):
  234. r=curs.fetchone()
  235.  
  236. if (r==None):
  237. print("Thats all")
  238. break
  239. print(r[0],r[1],r[2])
  240. if(r==None):
  241. break
  242. move_on= int(input("Type 1 to see more:"))
  243. check=input("Check any users?[Y/N] :")
  244. if(check=='Y'):
  245. check_user(connection,curs,u_id,1)
  246. def check_user(connection,curs,u_id,mode):
  247. new_id=input("Please input the id of the user: ");
  248. param={'new_id':new_id}
  249. curs.execute(u_number,param)
  250. r=curs.fetchall();
  251.  
  252. print("Tweets: ",r[0][0]," Flowee: ",r[0][1],"Floweer ", r[0][2])
  253. curs.execute(u_tweets,param);
  254. print("Resent tweets: ")
  255. print("")
  256. move_on=1
  257. while (move_on==1):
  258. for i in range(0,3):
  259. r=curs.fetchone()
  260.  
  261. if (r==None):
  262. print("Thats all")
  263. break
  264. print(r[0],r[1],r[2])
  265. if(r==None):
  266. break
  267. move_on= int(input("Type 1 to see more:"))
  268. if(mode==1):
  269. follow=input("Follow ther user?[Y/N] :")
  270. if(follow=='Y'):
  271. paraf={'u_id':u_id,'new_id':new_id,'new_date':datetime.datetime.now()}
  272. curs.execute(u_follow,paraf)
  273. connection.commit()
  274. print("Follow success! ")
  275. def compose_tweet(connection,curs,u_id):
  276. unique=1
  277. while (unique == 1):
  278. newt_id = random.randint(0,1000000);
  279. par={'t_id':newt_id}
  280. curs.execute(c1,par)
  281. unique=(curs.fetchall())[0][0]
  282. text=input("Please input the text: ")
  283. s=text.split(" ")
  284. hashtags=[]
  285. for i in s:
  286. if('#'in i):
  287. hashtags.append(i);
  288. for p in hashtags:
  289. p=p[1:]
  290. parah={'h_t':p}
  291. curs.execute(hashq,parah)
  292. exist=(curs.fetchall())[0][0]
  293. if(exist==0):
  294. curs.execute(hasha,parah)
  295. connection.commit();
  296. parat={'t_id':newt_id,'u_id':u_id,'t_date':datetime.datetime.now(),'t_text':text,'r_to':None}
  297. curs.execute(c2,parat);
  298. connection.commit()
  299. for m in hashtags:
  300. m=m[1:]
  301. param={'t_id':newt_id,'m_term':m}
  302. curs.execute(m1,param)
  303. connection.commit()
  304. print("Compose success in id: ",newt_id)
  305. def list_follower(connection,curs,u_id):
  306. paralf={'u_id':u_id}
  307. curs.execute(list_follow,paralf)
  308. rows= curs.fetchall()
  309. for row in rows:
  310. print(row[0],row[1])
  311. check=input("Check any users?[Y/N] :")
  312. if(check=='Y'):
  313. check_user(connection,curs,u_id,0)
  314.  
  315.  
  316. def check_tweet(connection,curs,u_id):
  317. t_id=input("Please input the id of the tweet: ");
  318. param={'t_id':t_id}
  319. curs.execute(reply_number,param)
  320. r=curs.fetchall();
  321. print("")
  322. print("replies:",r[0][0])
  323. curs.execute(retweets_number,param)
  324. r=curs.fetchall();
  325. print("retweets:",r[0][0])
  326. print("")
  327. selection=input("Do you want reply/retweets it? [retweets[1]/reply[2]]:");
  328. if(selection=='2'):
  329. unique=1
  330. while (unique == 1):
  331. newt_id = random.randint(0,1000000);
  332. par={'t_id':newt_id}
  333. curs.execute(c1,par)
  334. unique=(curs.fetchall())[0][0]
  335. text=input("Please input the text: ")
  336. s=text.split(" ")
  337. hashtags=[]
  338. for i in s:
  339. if('#'in i):
  340. hashtags.append(i);
  341. for p in hashtags:
  342. p=p[1:]
  343. parah={'h_t':p}
  344. curs.execute(hashq,parah)
  345. exist=(curs.fetchall())[0][0]
  346. if(exist==0):
  347. curs.execute(hasha,parah)
  348. connection.commit();
  349. parat={'t_id':newt_id,'u_id':u_id,'t_date':datetime.datetime.now(),'t_text':text,'r_to':t_id}
  350. curs.execute(c2,parat);
  351. connection.commit()
  352. for m in hashtags:
  353. m=m[1:]
  354. param={'t_id':newt_id,'m_term':m}
  355. curs.execute(m1,param)
  356. connection.commit()
  357. print("Reply success in id: ",newt_id)
  358. elif(selection=='1'):
  359. parar={'u_id':u_id,'t_id':t_id,'r_date':datetime.datetime.now()}
  360. curs.execute(re1,parar)
  361. print("retweets success!")
  362. connection.commit()
  363. def main():
  364. # my code here
  365. name=input("Plesase enter the name for Oracle:")
  366. pw=input("Please enter the password:")
  367. connection = cx_Oracle.connect(name, pw, 'gwynne.cs.ualberta.ca:1521/CRS')
  368. curs = connection.cursor()
  369. loop=True
  370. while(loop==True):
  371. loop = login(connection,curs)
  372. curs.close
  373. connection.close()
  374. sys.exit()
  375.  
  376. if __name__ == "__main__":
  377. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement