Advertisement
Guest User

Untitled

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