pipelines.py 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. import json
  2. import time
  3. import redis
  4. import logging
  5. import requests
  6. # from twisted.enterprise import adbapi
  7. from scrapy.conf import settings
  8. import psycopg2
  9. import psycopg2.extras
  10. from .ball_func import zqone_intodb, get_pcode, zqtwo_intodb, new_time, out_time, zqone_competition, zqtwo_competition, \
  11. zqthree_competition, two_intodb, one_intodb, three_intodb, wqone_intodb, hash_func, r_func, bqone_intodb, gjz_into, \
  12. gjl_into, gjw_into, gjb_into, new_times, zqone_intodbs, zqtwo_intodbs, zqone_competitions
  13. # 足球 赔率 赛事---------------------------------------------------------------------------------------------------------
  14. class Zuqiupipeline(object):
  15. def open_spider(self, spider):
  16. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  17. password=settings["R_PASSWORD"])
  18. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  19. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  20. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  21. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  22. def process_item(self, item, spider):
  23. # 使用twisted将mysql插入变成异步执行
  24. logger = logging.getLogger(__name__)
  25. # logger.warning(query.addErrback(self.handle_error, item, spider))
  26. # 比赛日期
  27. try:
  28. data_game = item['data_game'].split("/")
  29. print(data_game)
  30. month = str(data_game[1].strip())
  31. day = str(data_game[0])
  32. except Exception as e:
  33. logger.warning(e)
  34. data_game = item['data_game'].split(" ")
  35. months = str(data_game[1].strip())
  36. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  37. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  38. month = month_dict[months]
  39. day = str(data_game[0])
  40. # 比赛时间
  41. time_game = str(item['time_game'])
  42. # 比赛时间,时间戳
  43. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  44. r_ctime = "2019" + "-" + month + "-" + day
  45. # 现在时间,时间戳
  46. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  47. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  48. # 比赛id
  49. competition_id = item['game_id']
  50. # 联赛id
  51. league_id = item['league_id']
  52. # 联赛name
  53. league_name = item['league_name']
  54. # # 主队
  55. team_home = item['team_home']
  56. # 客队
  57. team_guest = item['team_guest']
  58. # number
  59. number = item['number']
  60. pt = item['pt']
  61. corner_ball = item['corner_ball']
  62. p_code, p_id = get_pcode(corner_ball, 'goal_size')
  63. # 构建唯一索引
  64. half_size_guest = item["half_size_guest"]
  65. half_size_guest_rule = item["half_size_guest_rule"]
  66. half_size_home = item["half_size_home"]
  67. half_size_home_rule = item["half_size_home_rule"]
  68. zuqiu = item['zuqiu']
  69. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  70. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt}
  71. # 让球 数据插入数据库
  72. try:
  73. zqone_intodb(data1=half_size_home, data2=size_data, data3="half_goal_size_home", data4=half_size_home_rule,
  74. cursor=self.cursor, redis_db=self.redis_db)
  75. except Exception as e:
  76. logger.warning('足球half_size_home插入错误')
  77. logger.warning(e)
  78. # print(item)
  79. # # 让球 数据插入数据库
  80. try:
  81. zqone_intodb(data1=half_size_guest, data2=size_data, data3="half_goal_size_guest", data4=half_size_guest_rule,
  82. cursor=self.cursor, redis_db=self.redis_db)
  83. except Exception as e:
  84. logger.warning('足球half_size_guest插入错误')
  85. logger.warning(e)
  86. # 全场场大小
  87. size_guest = item["size_guest"]
  88. size_guest_rule = item["size_guest_rule"]
  89. size_home = item["size_home"]
  90. size_home_rule = item["size_home_rule"]
  91. try:
  92. zqone_intodb(data1=size_guest, data2=size_data, data3="goal_size_guest", data4=size_guest_rule,
  93. cursor=self.cursor, redis_db=self.redis_db)
  94. except Exception as e:
  95. logger.warning('足球size_guest插入错误')
  96. logger.warning(e)
  97. try:
  98. zqone_intodb(data1=size_home, data2=size_data, data3="goal_size_home", data4=size_home_rule,
  99. cursor=self.cursor, redis_db=self.redis_db)
  100. except Exception as e:
  101. logger.warning('足球size_home插入错误')
  102. logger.warning(e)
  103. p_code, p_id = get_pcode(corner_ball, 'concede')
  104. concede_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  105. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt}
  106. # 上半场大小
  107. half_concede_home_rule = item["half_concede_home_rule"]
  108. half_concede_home = item["half_concede_home"]
  109. half_concede_guest_rule = item["half_concede_guest_rule"]
  110. half_concede_guest = item["half_concede_guest"]
  111. # 上半场让球
  112. try:
  113. zqone_intodb(data1=half_concede_home, data2=concede_data, data3="half_concede_home", data4=half_concede_home_rule,
  114. cursor=self.cursor, redis_db=self.redis_db)
  115. except Exception as e:
  116. logger.warning('足球half_concede_home插入错误')
  117. logger.warning(e)
  118. try:
  119. zqone_intodb(data1=half_concede_guest, data2=concede_data, data3="half_concede_guest", data4=half_concede_guest_rule, cursor=self.cursor, redis_db=self.redis_db)
  120. except Exception as e:
  121. logger.warning('足球half_concede_guest插入错误')
  122. logger.warning(e)
  123. concede_guest = item["concede_guest"]
  124. concede_guest_rule = item["concede_guest_rule"]
  125. concede_home = item["concede_home"]
  126. concede_home_rule = item["concede_home_rule"]
  127. # 全场让球
  128. try:
  129. zqone_intodb(data1=concede_guest, data2=concede_data, data3="concede_guest", data4=concede_guest_rule,
  130. cursor=self.cursor, redis_db=self.redis_db)
  131. except Exception as e:
  132. logger.warning('足球concede_guest插入错误')
  133. logger.warning(e)
  134. try:
  135. zqone_intodb(data1=concede_home, data2=concede_data, data3="concede_home", data4=concede_home_rule,
  136. cursor=self.cursor,redis_db=self.redis_db)
  137. except Exception as e:
  138. logger.warning('足球concede_home插入错误')
  139. logger.warning(e)
  140. p_code, p_id = get_pcode(corner_ball, 'capot')
  141. # 独赢----------------------------------------------------------------------------------------------------------
  142. half_capot_home = item["half_capot_home"]
  143. half_capot_guest = item["half_capot_guest"]
  144. half_capot_dogfall = item["half_capot_dogfall"]
  145. capot_home = item["capot_home"]
  146. capot_guest = item["capot_guest"]
  147. capot_dogfall = item["capot_dogfall"]
  148. capot_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  149. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  150. # 上半场独赢 主队
  151. try:
  152. zqtwo_intodb(data1=half_capot_home, data2=capot_data, data3="half_capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  153. except Exception as e:
  154. logger.warning('足球half_capot_home插入错误')
  155. logger.warning(e)
  156. # 上半场独赢 客队
  157. try:
  158. zqtwo_intodb(data1=half_capot_guest, data2=capot_data, data3="half_capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  159. except Exception as e:
  160. logger.warning('足球half_capot_guest插入错误')
  161. logger.warning(e)
  162. # 上半场独赢 和
  163. try:
  164. zqtwo_intodb(data1=half_capot_dogfall, data2=capot_data, data3="half_capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  165. except Exception as e:
  166. logger.warning('足球half_capot_dogfall插入错误')
  167. logger.warning(e)
  168. #
  169. # 全场独赢 主队
  170. try:
  171. zqtwo_intodb(data1=capot_home, data2=capot_data, data3="capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  172. except Exception as e:
  173. logger.warning('足球capot_home插入错误')
  174. logger.warning(e)
  175. # 全场独赢 客队
  176. try:
  177. zqtwo_intodb(data1=capot_guest, data2=capot_data, data3="capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  178. except Exception as e:
  179. logger.warning('足球capot_guest插入错误')
  180. logger.warning(e)
  181. # 全场独赢 和
  182. try:
  183. zqtwo_intodb(data1=capot_dogfall, data2=capot_data, data3="capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  184. except Exception as e:
  185. logger.warning('足球capot_dogfall插入错误')
  186. logger.warning(e)
  187. #
  188. # 入球数单双-------------------------------------------------------------------------------------------------------------
  189. p_code, p_id = get_pcode(corner_ball, 'two_sides')
  190. odd_even_odd = item["odd_even_odd"]
  191. odd_even_even = item["odd_even_even"]
  192. half_odd_even_odd = item["half_odd_even_odd"]
  193. half_odd_even_even = item["half_odd_even_even"]
  194. # 全场入球数 单双
  195. two_sides_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  196. 'p_code': p_code,
  197. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  198. # 上半场入球数 单双
  199. try:
  200. zqtwo_intodb(data1=odd_even_odd, data2=two_sides_data, data3="two_sides_single", data4='单', cursor=self.cursor,redis_db=self.redis_db)
  201. except Exception as e:
  202. logger.warning('足球odd_even_odd插入错误')
  203. logger.warning(e)
  204. # url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  205. # data = {
  206. # "game_code": "zq",
  207. # "title": "odds",
  208. # "data": {
  209. # "match_id": competition_id,
  210. # "lg_id": 1123,
  211. # "odds_code": "concede_guest",
  212. # "status": 0,
  213. # "sort": 0,
  214. # "p_code": "concede_size",
  215. # "odds": 1.2333,
  216. # "condition": "+1.5",
  217. # "odds_only": "b15bad3fc8f09924874fef0e19f63d001",
  218. # "sole": "5e2225944d8cdc44cd7943330986978d2",
  219. # "source": "c22123",
  220. # "type": 0,
  221. # "team": ""
  222. # }
  223. # }
  224. # data = json.dumps(data)
  225. # print(data)
  226. # r = requests.post(url, data=data)
  227. # print(r.content)
  228. try:
  229. zqtwo_intodb(data1=odd_even_even, data2=two_sides_data, data3="two_sides_double", data4='双', cursor=self.cursor,redis_db=self.redis_db)
  230. except Exception as e:
  231. logger.warning('足球odd_even_even插入错误')
  232. logger.warning(e)
  233. # 全场入球数 单双
  234. try:
  235. zqtwo_intodb(data1=half_odd_even_odd, data2=two_sides_data, data3="half_two_sides_single", data4='单',
  236. cursor=self.cursor, redis_db=self.redis_db)
  237. except Exception as e:
  238. logger.warning('足球half_odd_even_odd插入错误')
  239. logger.warning(e)
  240. try:
  241. zqtwo_intodb(data1=half_odd_even_even, data2=two_sides_data, data3="half_two_sides_double", data4='双',
  242. cursor=self.cursor, redis_db=self.redis_db)
  243. except Exception as e:
  244. logger.warning('足球half_odd_even_even插入错误')
  245. logger.warning(e)
  246. # 总入球数 --------------------------------------------------------------------------------------------------------------
  247. p_code, p_id = get_pcode(corner_ball, 'total_goal')
  248. total_goals = item['total_goal']
  249. total_dict = {'total_goal_zero': '0-1', 'total_goal_two': '2-3', 'total_goal_four': '4-6',
  250. 'total_goal_seven': '7或以上', 'half_total_goal_zero': '0', "half_total_goal_one": '1',
  251. "half_total_goal_two": '2', "half_total_goal_three": '3或以上'}
  252. # 全场入球数 单双
  253. total_goal_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  254. 'p_code': p_code,
  255. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  256. # 上半场入球数 单双
  257. for key, value in total_goals.items():
  258. if value:
  259. try:
  260. zqtwo_intodb(data1=value, data2=total_goal_data, data3=key, data4=total_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  261. except Exception as e:
  262. logger.warning('足球total_dict插入错误')
  263. logger.warning(e)
  264. # 全场半场 --------------------------------------------------------------------------------------------------------------
  265. half_fulls = item['half_full']
  266. p_code, p_id = get_pcode(corner_ball, 'half_full')
  267. full_dict = {"half_full_home_home": "主主", "half_full_home_dogfall": "主和",
  268. "half_full_home_guest": "主客", "half_full_dogfall_home": "和主",
  269. "half_full_dogfall_dogfall": "和和", "half_full_dogfall_guest": "和客",
  270. "half_full_guest_home": "客主", "half_full_guest_dogfall": "客和",
  271. "half_full_guest_guest": "客客"}
  272. half_full_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  273. 'p_code': p_code,
  274. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  275. if half_fulls:
  276. for key, value in half_fulls.items():
  277. if value:
  278. try:
  279. zqtwo_intodb(data1=value, data2=half_full_data, data3=key, data4=full_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  280. except Exception as e:
  281. logger.warning('足球full_dict插入错误')
  282. logger.warning(e)
  283. # 波胆------------------------------------------------------------------------------------------------------------------
  284. bodan_datas = item['bodan_data']
  285. p_code, p_id = get_pcode(corner_ball, 'bodan')
  286. bodan_dict = {"bodanhome_one_zero": "1-0", "bodanhome_two_zero": "2-0",
  287. "bodanhome_two_one": "2-1", "bodanhome_three_zero": "3-0",
  288. "bodanhome_three_one": "3-1", "bodanhome_three_two": "3-2",
  289. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  290. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  291. "bodanguest_one_zero": "0-1", "bodanguest_two_zero": "0-2",
  292. "bodanguest_two_one": "1-2", "bodanguest_three_zero": "0-3",
  293. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  294. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  295. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4",
  296. "bodandogfall_zero_zero": "0-0", "bodandogfall_one_one": "1-1",
  297. "bodandogfall_two_two": "2-2", "bodandogfall_three_three": "3-3",
  298. "bodandogfall_four_four": "4-4", "bodanother": "其他",
  299. "halfbodanhome_one_zero": "1-0", "halfbodanhome_two_zero": "2-0",
  300. "halfbodanhome_two_one": "2-1", "halfbodanhome_three_zero": "3-0",
  301. "halfbodanhome_three_one": "3-1", "halfbodanhome_three_two": "3-2",
  302. "halfbodanguest_one_zero": "0-1", "halfbodanguest_two_zero": "0-2",
  303. "halfbodanguest_two_one": "1-2", "halfbodanguest_three_zero": "0-3",
  304. "halfbodanguest_three_one": "1-3", "halfbodanguest_three_two": "2-3",
  305. "halfbodandogfall_zero_zero": "0-0", "halfbodandogfall_one_one": "1-1",
  306. "halfbodandogfall_two_two": "2-2", "halfbodandogfall_three_three": "3-3",
  307. "halfbodanother": "其他"}
  308. bodan_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  309. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  310. if bodan_datas:
  311. for key, value in bodan_datas.items():
  312. try:
  313. zqtwo_intodb(data1=value, data2=bodan_data, data3=key, data4=bodan_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  314. except Exception as e:
  315. logger.warning('足球bodan_datas插入错误')
  316. logger.warning(e)
  317. # 最先进球/最后进球 ------------------------------------------------------------------------------------------------------
  318. first_last_balls = item['first_last_ball']
  319. p_code, p_id = get_pcode(corner_ball, 'first_last_ball')
  320. first_last_dict = {"first_last_ball": "最先进球", "first_ball_home": "最先进球", "first_ball_guest": "最先进球",
  321. "last_ball_home": "最后进球", "last_ball_guest": "最后进球", "not_ball": "没有进球"}
  322. first_last_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  323. 'p_code': p_code,
  324. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  325. if first_last_balls:
  326. for key, value in first_last_balls.items():
  327. # 构建唯一索引
  328. try:
  329. zqtwo_intodb(data1=value, data2=first_last_data, data3=key, data4=first_last_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  330. except Exception as e:
  331. logger.warning('足球first_last_balls插入错误')
  332. logger.warning(e)
  333. p_code, p_id = get_pcode(corner_ball, 'tema_ball')
  334. full_dicts = item['full_data']
  335. half_dicts = item['half_data']
  336. full_dict_rules = item['full_data_rule']
  337. half_dict_rules = item['half_data_rule']
  338. data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  339. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  340. if full_dicts:
  341. for key, value in full_dicts.items():
  342. try:
  343. zqtwo_intodb(data1=value, data2=data, data3=key, data4=full_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  344. except Exception as e:
  345. logger.warning('足球full_dicts插入错误')
  346. logger.warning(e)
  347. if half_dicts:
  348. for key, value in half_dicts.items():
  349. try:
  350. zqtwo_intodb(data1=value, data2=data, data3=key, data4=half_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  351. except Exception as e:
  352. logger.warning('足球half_dicts插入错误')
  353. logger.warning(e)
  354. match_date, match_time, time3 = new_time(ctime)
  355. n_time = out_time(time3, 1.5)
  356. # 插入st_zq_competition表
  357. if zuqiu == "足球":
  358. try:
  359. if pt == 1:
  360. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  361. 'game_id': competition_id,
  362. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  363. 'source': "hg3535", "expire_time": n_time, 'is_today': 1, "us_time": ctime}
  364. zqone_competition(data=data_competition, cursor=self.cursor)
  365. if pt == 2:
  366. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  367. 'game_id': competition_id,
  368. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  369. 'source': "hg3535", "expire_time": n_time, 'is_morningplate': 1, "us_time": ctime}
  370. zqtwo_competition(data=data_competition, cursor=self.cursor)
  371. if pt == 3:
  372. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  373. 'game_id': competition_id,
  374. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  375. 'source': "hg3535", "expire_time": n_time, "is_stringscene": 1, "us_time": ctime}
  376. zqthree_competition(data=data_competition, cursor=self.cursor)
  377. except Exception as e:
  378. logger.warning('足球st_zq_competition插入错误')
  379. logger.warning(e)
  380. return item
  381. def close_spider(self, spider):
  382. self.conn.commit()
  383. self.conn.close()
  384. # 篮球 让球大小----------------------------------------------------------------------------------------------------------
  385. class Lanqiupipeline(object):
  386. def open_spider(self, spider):
  387. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  388. password=settings["R_PASSWORD"])
  389. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  390. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  391. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  392. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  393. def process_item(self, item, spider):
  394. # 使用twisted将mysql插入变成异步执行
  395. logger = logging.getLogger(__name__)
  396. # # logger.warning(query.addErrback(self.handle_error, item, spider))
  397. # 联赛id
  398. league_id = item['league_id']
  399. # 联赛名
  400. league_name = item['league_name']
  401. # result = item['result']
  402. # 比赛id
  403. game_id = item['game_id']
  404. # 球队1
  405. team_home = item['team_home']
  406. # 球队2
  407. team_guest = item['team_guest']
  408. # 数量(97>)
  409. number = item['number']
  410. # 比赛状态
  411. zhuangtai = item['zhuangtai']
  412. # 日期
  413. # data_game = item['data_game']
  414. try:
  415. data_game = item['data_game'].split("/")
  416. month = str(data_game[1].strip())
  417. day = str(data_game[0])
  418. except Exception as e:
  419. logger.warning(e)
  420. data_game = item['data_game'].split(" ")
  421. months = str(data_game[1].strip())
  422. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  423. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  424. month = month_dict[months]
  425. day = str(data_game[0])
  426. # 比赛时间
  427. time_game = str(item['time_game'])
  428. # 比赛时间,时间戳
  429. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  430. r_ctime = "2019" + "-" + month + "-" + day
  431. # 现在时间,时间戳
  432. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  433. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  434. # 队1分数
  435. score_home = item['score_home']
  436. # 队2分数
  437. score_guest = item['score_guest']
  438. # 第几节
  439. jijie = item['jijie']
  440. # 球队得分
  441. qiudui = item['qiudui']
  442. pt = item['pt']
  443. concedes_dict = item['concede']
  444. concedes_dict_rule = item['concede_rule']
  445. odd_evens_dict = item['odd_even']
  446. odd_evens_dict_rule = item['odd_even_rule']
  447. total_sizes_dict = item['total_size']
  448. total_sizes_dict_rule = item['total_size_rule']
  449. last_numbers_dict = item['last_number']
  450. capots_dict = item['capot']
  451. team_scores_dict = item['team_score']
  452. team_scores_dict_rule = item['team_score_rule']
  453. # 让球
  454. lanqiu = item['lanqiu']
  455. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  456. 'source': "hg3535", 'expire_time': expire_time}
  457. # 让球 数据插入数据库
  458. try:
  459. one_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  460. except Exception as e:
  461. logger.warning('篮球concedes_dict插入错误')
  462. logger.warning(e)
  463. two_sides = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "two_sides",
  464. 'source': "hg3535", 'expire_time': expire_time}
  465. # 总分单双 数据插入数据库
  466. try:
  467. one_intodb(data1=odd_evens_dict, data2=two_sides, data3=odd_evens_dict_rule, cursor=self.cursor,
  468. redis_db=self.redis_db)
  469. except Exception as e:
  470. logger.warning('篮球odd_evens_dict插入错误')
  471. logger.warning(e)
  472. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "total_size",
  473. 'source': "hg3535", 'expire_time': expire_time}
  474. # 全场总分大小 数据插入数据库
  475. try:
  476. one_intodb(data1=total_sizes_dict, data2=total_size, data3=total_sizes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  477. except Exception as e:
  478. logger.warning('篮球total_sizes_dict插入错误')
  479. logger.warning(e)
  480. data4 = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 6, 'p_code': "team_score",
  481. 'source': "hg3535", 'expire_time': expire_time}
  482. # 全场总分大小 数据插入数据库
  483. try:
  484. one_intodb(data1=team_scores_dict, data2=data4, data3=team_scores_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  485. except Exception as e:
  486. logger.warning('篮球team_scores_dict插入错误')
  487. logger.warning(e)
  488. # 主队进球最后一位数
  489. last_number_home = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  490. 'p_code': "last_number",
  491. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  492. try:
  493. two_intodb(data1=last_numbers_dict['last_home'], data2=last_number_home, data3="last_home", cursor=self.cursor,redis_db=self.redis_db)
  494. except Exception as e:
  495. logger.warning('篮球last_numbers_dict主队插入错误')
  496. logger.warning(e)
  497. # print(last_numbers_dict['last_home'])
  498. # 客队进球最后一位数
  499. last_number_guest = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  500. 'p_code': "last_number",
  501. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  502. try:
  503. two_intodb(data1=last_numbers_dict['last_guest'], data2=last_number_guest, data3="last_guest",
  504. cursor=self.cursor, redis_db=self.redis_db)
  505. except Exception as e:
  506. logger.warning('篮球last_numbers_dict客队插入错误')
  507. logger.warning(e)
  508. # print(last_numbers_dict['last_guest'])
  509. # 独赢
  510. capot_data = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 5, 'p_code': "capot",
  511. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  512. try:
  513. three_intodb(data1=capots_dict, data2=capot_data, cursor=self.cursor, redis_db=self.redis_db)
  514. except Exception as e:
  515. logger.warning('篮球capots_dict插入错误')
  516. logger.warning(e)
  517. match_date, match_time, time3 = new_time(ctime)
  518. n_time = out_time(time3, 1.5)
  519. us_time = ctime
  520. # 插入赛事表
  521. if lanqiu == "篮球":
  522. try:
  523. if pt == 1:
  524. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_today,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_today=%s;"
  525. self.cursor.execute(Competition_sql, (
  526. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  527. n_time,1, us_time, number, n_time, 1))
  528. if pt == 2:
  529. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_morningplate,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_morningplate=%s;"
  530. self.cursor.execute(Competition_sql, (
  531. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  532. n_time,1, us_time, number, n_time, 1))
  533. if pt == 3:
  534. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_stringscene,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_stringscene=%s;"
  535. self.cursor.execute(Competition_sql, (
  536. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  537. n_time,1, us_time, number, n_time, 1))
  538. except Exception as e:
  539. logger.warning('篮球st_lq_competition插入错误')
  540. logger.warning(e)
  541. return item
  542. def close_spider(self, spider):
  543. self.conn.commit()
  544. self.conn.close()
  545. # 网球 让球&大小盘--------------------------------------------------------------------------------------------------------
  546. class Wangqiupipeline(object):
  547. def open_spider(self, spider):
  548. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  549. password=settings["R_PASSWORD"])
  550. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  551. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  552. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  553. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  554. def process_item(self, item, spider):
  555. logger = logging.getLogger(__name__)
  556. # logger.warning(query.addErrback(self.handle_error, item, spider))
  557. # 联赛id
  558. league_id = item['league_id']
  559. # 联赛名
  560. league_name = item['league_name']
  561. # result = item['result']
  562. # 比赛id
  563. game_id = item['game_id']
  564. # 球队1
  565. team_home = item['team_home']
  566. # 球队2
  567. team_guest = item['team_guest']
  568. # 数量(97>)
  569. number = item['number']
  570. # 比赛状态
  571. zhuangtai = item['zhuangtai']
  572. # 日期
  573. # data_game = item['data_game']
  574. try:
  575. data_game = item['data_game'].split("/")
  576. month = str(data_game[1].strip())
  577. day = str(data_game[0])
  578. except Exception as e:
  579. logger.warning(e)
  580. data_game = item['data_game'].split(" ")
  581. months = str(data_game[1].strip())
  582. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  583. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  584. month = month_dict[months]
  585. day = str(data_game[0])
  586. # 比赛时间
  587. time_game = str(item['time_game'])
  588. # 比赛时间,时间戳
  589. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  590. r_ctime = "2019" + "-" + month + "-" + day
  591. # 现在时间,时间戳
  592. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  593. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  594. # 队1分数
  595. score_home = item['score_home']
  596. # 队2分数
  597. score_guest = item['score_guest']
  598. # 第几节
  599. jijie = item['jijie']
  600. # 球队得分
  601. qiudui = item['qiudui']
  602. pt = item['pt']
  603. # 让盘
  604. concedes_dict = item['concedes_dict']
  605. concedes_dict_rule = item['concedes_dict_rule']
  606. # 冠军
  607. kemps_dict = item['kemps_dict']
  608. # 让局
  609. bureaus_dict = item['bureaus_dict']
  610. bureaus_dict_rule = item['bureaus_dict_rule']
  611. # 总局数大小
  612. total_number_dict = item['total_number_dict']
  613. total_number_dict_rule = item['total_number_dict_rule']
  614. # 总局数单双
  615. odd_evens_dict = item['odd_evens_dict']
  616. odd_evens_dict_rule = item['odd_evens_dict_rule']
  617. wangqiu = item['wangqiu']
  618. # 让盘
  619. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "dishes",
  620. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  621. try:
  622. wqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  623. except Exception as e:
  624. logger.warning('网球concedes_dict插入错误')
  625. logger.warning(e)
  626. # 让局
  627. bureaus = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "bureau",
  628. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  629. try:
  630. wqone_intodb(data1=bureaus_dict, data2=bureaus, data3=bureaus_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  631. except Exception as e:
  632. logger.warning('网球bureaus_dict插入错误')
  633. logger.warning(e)
  634. total_number = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  635. 'p_code': "total_number", 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  636. try:
  637. wqone_intodb(data1=total_number_dict, data2=total_number, data3=total_number_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  638. except Exception as e:
  639. logger.warning('网球total_number_dict插入错误')
  640. logger.warning(e)
  641. odd_evens = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 5, 'p_code': "two_game_sides",
  642. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  643. try:
  644. wqone_intodb(data1=odd_evens_dict, data2=odd_evens, data3=odd_evens_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  645. except Exception as e:
  646. logger.warning('网球odd_evens_dict插入错误')
  647. logger.warning(e)
  648. match_date, match_time, time3 = new_time(ctime)
  649. n_time = out_time(time3, 3)
  650. if kemps_dict:
  651. for key, value in kemps_dict.items():
  652. if value:
  653. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  654. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2, odd=value)
  655. odds_only = r_hash
  656. if self.redis_db.hexists("hg3535_wangqiu", r_hash):
  657. pass
  658. else:
  659. self.redis_db.hset("hg3535_wangqiu", r_hash, 0)
  660. try:
  661. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,expire_time) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  662. self.cursor.execute(sql1, (
  663. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", new_hash,
  664. odds_only,expire_time, utime, value, odds_only))
  665. # 更新主队st_zq_odds_record表
  666. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s);"
  667. self.cursor.execute(sql2, (
  668. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", odds_only))
  669. except Exception as e:
  670. logger.warning('网球kemps_dict插入错误')
  671. logger.warning(e)
  672. us_time = ctime
  673. # 插入赛事表
  674. if wangqiu == "网球":
  675. try:
  676. if pt is 1:
  677. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_today,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_today=%s;"
  678. self.cursor.execute(Competition_sql, (
  679. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  680. n_time,1, us_time, number, n_time, 1))
  681. if pt is 2:
  682. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_morningplate,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_morningplate=%s;"
  683. self.cursor.execute(Competition_sql, (
  684. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  685. n_time,1, us_time, number, n_time, 1))
  686. if pt is 3:
  687. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_stringscene,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_stringscene=%s;"
  688. self.cursor.execute(Competition_sql, (
  689. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  690. n_time,1, us_time, number, n_time, 1))
  691. except Exception as e:
  692. logger.warning('网球st_wq_competition插入错误')
  693. logger.warning(e)
  694. return item
  695. def close_spider(self, spider):
  696. self.conn.commit()
  697. self.conn.close()
  698. # 网球 波胆--------------------------------------------------------------------------------------------------------------
  699. class Wqbodanpipeline(object):
  700. def open_spider(self, spider):
  701. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  702. password=settings["R_PASSWORD"])
  703. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  704. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  705. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  706. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  707. def process_item(self, item, spider):
  708. # 使用twisted将mysql插入变成异步执行
  709. logger = logging.getLogger(__name__)
  710. # logger.warning(query.addErrback(self.handle_error, item, spider))
  711. # 比赛日期
  712. try:
  713. data_game = item['data_game'].split("/")
  714. month = str(data_game[1].strip())
  715. day = str(data_game[0])
  716. except Exception as e:
  717. logger.warning(e)
  718. data_game = item['data_game'].split(" ")
  719. months = str(data_game[1].strip())
  720. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  721. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  722. month = month_dict[months]
  723. day = str(data_game[0])
  724. # 比赛时间
  725. time_game = str(item['time_game'])
  726. # 比赛时间,时间戳
  727. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  728. r_ctime = "2019" + "-" + month + "-" + day
  729. # 现在时间,时间戳
  730. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  731. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  732. # 比赛id
  733. competition_id = item['game_id']
  734. # 联赛id
  735. league_id = item['league_id']
  736. # 联赛name
  737. league_name = item['league_name']
  738. # 主队
  739. team_home = item['team_home']
  740. # 客队
  741. team_guest = item['team_guest']
  742. # 主队得分
  743. score_home = item['score_home']
  744. # 客队得分
  745. score_guest = item['score_guest']
  746. # number
  747. number = item['number']
  748. corner_ball = item['corner_ball']
  749. half_way = item['half_way']
  750. # 类型早盘,今日,滚球,串场
  751. pt = item['pt']
  752. bodan_datas = item['bodan_data']
  753. p_code = "bodan"
  754. p_id = 7
  755. bodan_dict = {"bodanhome_two_zero": "2-0", "bodanhome_two_one": "2-1",
  756. "bodanhome_three_zero": "3-0", "bodanhome_three_one": "3-1",
  757. "bodanhome_three_two": "3-2",
  758. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  759. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  760. "bodanguest_two_zero": "0-2", "bodanguest_two_one": "1-2",
  761. "bodanguest_three_zero": "0-3",
  762. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  763. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  764. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4"}
  765. if bodan_datas:
  766. for key, value in bodan_datas.items():
  767. if value:
  768. new_hash = hash_func(match_id=competition_id, odds_code=key, sort=0, p_id=7)
  769. r_hash = r_func(match_id=competition_id, odds_code=key, sort=0, p_id=7, odd=value)
  770. odds_only = r_hash
  771. if self.redis_db.hexists("hg3535_wangqiu", r_hash):
  772. pass
  773. else:
  774. self.redis_db.hset("hg3535_wangqiu", r_hash, 0)
  775. try:
  776. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole, condition, expire_time,odds_only) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime=%s,odds=%s,expire_time=%s,odds_only=%s;"
  777. self.cursor.execute(sql1, (
  778. int(league_id), key, int(competition_id), utime, utime, value, p_id, p_code, 0, "hg3535",
  779. new_hash, bodan_dict[key], expire_time, odds_only,utime, value, expire_time, odds_only))
  780. # 更新主队st_zq_odds_record表
  781. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s);"
  782. self.cursor.execute(sql2, (
  783. int(league_id), key, int(competition_id), utime, utime, value, p_id, p_code, 0, "hg3535", key,odds_only))
  784. except Exception as e:
  785. logger.warning('网球波胆插入错误')
  786. logger.warning(e)
  787. return item
  788. def close_spider(self, spider):
  789. self.conn.commit()
  790. self.conn.close()
  791. # 棒球 让球&大小盘--------------------------------------------------------------------------------------------------------
  792. class Bangqiupipeline(object):
  793. def open_spider(self, spider):
  794. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  795. password=settings["R_PASSWORD"])
  796. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  797. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  798. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  799. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  800. def process_item(self, item, spider):
  801. # print(item)
  802. logger = logging.getLogger(__name__)
  803. # logger.warning(query.addErrback(self.handle_error, item, spider))
  804. # 联赛id
  805. league_id = item['league_id']
  806. # 联赛名
  807. league_name = item['league_name']
  808. # result = item['result']
  809. # 比赛id
  810. game_id = item['game_id']
  811. # 球队1
  812. team_home = item['team_home']
  813. # 球队2
  814. team_guest = item['team_guest']
  815. # 数量(97>)
  816. number = item['number']
  817. # 比赛状态
  818. zhuangtai = item['zhuangtai']
  819. # 日期
  820. # data_game = item['data_game']
  821. try:
  822. data_game = item['data_game'].split("/")
  823. month = str(data_game[1].strip())
  824. day = str(data_game[0])
  825. except Exception as e:
  826. logger.warning(e)
  827. data_game = item['data_game'].split(" ")
  828. months = str(data_game[1].strip())
  829. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  830. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  831. month = month_dict[months]
  832. day = str(data_game[0])
  833. # 比赛时间
  834. time_game = str(item['time_game'])
  835. # 比赛时间,时间戳
  836. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  837. r_ctime = "2019" + "-" + month + "-" + day
  838. # 现在时间,时间戳
  839. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  840. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  841. # 队1分数
  842. score_home = item['score_home']
  843. # 队2分数
  844. score_guest = item['score_guest']
  845. # 第几节
  846. jijie = item['jijie']
  847. # 球队得分
  848. qiudui = item['qiudui']
  849. pt = item['pt']
  850. # 让球
  851. concedes_dict = item['concedes_dict']
  852. concedes_dict_rule = item['concedes_dict_rule']
  853. # 独赢
  854. capots_dict = item['capots_dict']
  855. # 总得分大小
  856. total_size_dict = item['total_size_dict']
  857. total_size_dict_rule = item['total_size_dict_rule']
  858. # 总得分单双
  859. odd_evens_dict = item['odd_evens_dict']
  860. odd_evens_dict_rule = item['odd_evens_dict_rule']
  861. bangqiu = item['bangqiu']
  862. # 让球
  863. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  864. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  865. try:
  866. bqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  867. except Exception as e:
  868. logger.warning('棒球concede插入错误')
  869. logger.warning(e)
  870. # 总得分:大/小
  871. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "total_size",
  872. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  873. try:
  874. bqone_intodb(data1=total_size_dict, data2=total_size, data3=total_size_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  875. except Exception as e:
  876. logger.warning('棒球total_size插入错误')
  877. logger.warning(e)
  878. odd_even = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "two_sides",
  879. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt}
  880. try:
  881. bqone_intodb(data1=odd_evens_dict, data2=odd_even, data3=odd_evens_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  882. except Exception as e:
  883. logger.warning('棒球odd_even插入错误')
  884. logger.warning(e)
  885. # 赛事失效时间
  886. match_date, match_time, time3 = new_time(ctime)
  887. n_time = out_time(time3, 3.5)
  888. # 插入独赢
  889. if capots_dict:
  890. for key, value in capots_dict.items():
  891. if value:
  892. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  893. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4, odd=value)
  894. odds_only = r_hash
  895. if self.redis_db.hexists("hg3535_bangqiu", r_hash):
  896. pass
  897. else:
  898. self.redis_db.hset("hg3535_bangqiu", r_hash, 0)
  899. try:
  900. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,expire_time) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s,expire_time=%s;"
  901. self.cursor.execute(sql1, (
  902. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", new_hash,
  903. odds_only, expire_time,utime, value, odds_only, expire_time))
  904. # 更新主队st_zq_odds_record表
  905. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s);"
  906. self.cursor.execute(sql2, (
  907. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", odds_only))
  908. except Exception as e:
  909. logger.warning('棒球独赢插入错误')
  910. logger.warning(e)
  911. us_time = ctime
  912. # 插入赛事表
  913. if bangqiu == '棒球':
  914. try:
  915. if pt == 1:
  916. Competition_sql = "insert into st_bq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_today,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_today=%s;"
  917. self.cursor.execute(Competition_sql, (
  918. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  919. n_time, 1, us_time, number, n_time, 1))
  920. if pt == 2:
  921. Competition_sql = "insert into st_bq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_morningplate,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_morningplate=%s;"
  922. self.cursor.execute(Competition_sql, (
  923. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  924. n_time,1, us_time, number, n_time, 1))
  925. if pt == 3:
  926. Competition_sql = "insert into st_bq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,source,expire_time,is_stringscene,us_time) values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s) on conflict(match_id) do update set tag = %s,expire_time=%s,is_stringscene=%s;"
  927. self.cursor.execute(Competition_sql, (
  928. team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  929. n_time,1, us_time, number, n_time, 1))
  930. except Exception as e:
  931. logger.warning('棒球st_bq_competition插入错误')
  932. logger.warning(e)
  933. return item
  934. def close_spider(self, spider):
  935. self.conn.commit()
  936. self.conn.close()
  937. # 足球 篮球 网球 棒球 冠军------------------------------------------------------------------------------------------------
  938. class Guanjunpipeline(object):
  939. def open_spider(self, spider):
  940. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"],
  941. password=settings["R_PASSWORD"])
  942. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  943. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  944. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  945. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  946. def process_item(self, item, spider):
  947. logger = logging.getLogger(__name__)
  948. data_game = item['data_game'].replace('年', "-").replace('月', "-").replace('日', "")
  949. time_game = item['time_game']
  950. ctime = data_game + " " + time_game + ":00"
  951. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  952. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  953. league_name = item['league_name']
  954. tema_home = item['tema_home']
  955. league_id = item['league_id']
  956. game_id = item['game_id']
  957. new_league_name = item['new_league_name']
  958. # 冠军赔率
  959. champion_team = item['champion_team']
  960. new_champion = item['new_champion']
  961. # 构建唯一哈希索引
  962. time1 = time.mktime(time.strptime(ctime, '%Y-%m-%d %H:%M:%S')) + 43200
  963. time2 = time.localtime(time1)
  964. time3 = time.strftime('%Y-%m-%d %H:%M:%S', time2)
  965. # data_time = str(time3).split(" ")
  966. # match_date,match_time = new_time(ctime)
  967. pt = item['pt']
  968. new_hash = hash_func(match_id=champion_team, odds_code=new_champion, sort=new_league_name, p_id=tema_home)
  969. ball = item['ball']
  970. # 插入st_zq_odds表
  971. r_hash = hash_func(league_id, new_champion, tema_home, champion_team)
  972. odds_only = r_hash
  973. s_hash = hash_func(league_id, league_name, ball, 0)
  974. if ball == "足球":
  975. n_gameid = int('1' + str(league_id))
  976. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  977. 'champion_team': champion_team, "p_id": 8, 'new_league_name': new_league_name,
  978. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  979. "odds_only": odds_only,
  980. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  981. try:
  982. gjz_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  983. Competition_sql = "insert into st_zq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  984. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  985. except Exception as e:
  986. logger.warning('足球冠军插入错误')
  987. logger.warning(e)
  988. elif ball == "篮球":
  989. n_gameid = int('2' + str(league_id))
  990. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  991. 'champion_team': champion_team, "p_id": 7, 'new_league_name': new_league_name,
  992. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  993. "odds_only": odds_only,
  994. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  995. try:
  996. gjl_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  997. Competition_sql = "insert into st_lq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  998. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  999. except Exception as e:
  1000. logger.warning('篮球冠军插入错误')
  1001. logger.warning(e)
  1002. elif ball == "网球":
  1003. n_gameid = int('3' + str(league_id))
  1004. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1005. 'champion_team': champion_team, "p_id": 6, 'new_league_name': new_league_name,
  1006. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1007. "odds_only": odds_only,
  1008. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1009. try:
  1010. gjw_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1011. Competition_sql = "insert into st_wq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  1012. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1013. except Exception as e:
  1014. logger.warning('网球冠军插入错误')
  1015. logger.warning(e)
  1016. elif ball == "棒球":
  1017. n_gameid = int('4' + str(league_id))
  1018. data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1019. 'champion_team': champion_team, "p_id": 5, 'new_league_name': new_league_name,
  1020. 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1021. "odds_only": odds_only,
  1022. "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1023. try:
  1024. gjb_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1025. Competition_sql = "insert into st_bq_competition(home_team, lg_id, match_id, ctime, utime, source, us_time) values (%s, %s, %s, %s, %s, %s, %s) on conflict(match_id) do update set utime=%s;"
  1026. self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1027. except Exception as e:
  1028. logger.warning('棒球冠军插入错误')
  1029. logger.warning(e)
  1030. return item
  1031. def close_spider(self, spider):
  1032. self.conn.commit()
  1033. self.conn.close()
  1034. # 足球 篮球 网球 棒球 联赛------------------------------------------------------------------------------------------------
  1035. class Liansaipipeline(object):
  1036. def open_spider(self, spider):
  1037. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1038. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1039. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1040. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1041. def process_item(self, item, spider):
  1042. logger = logging.getLogger(__name__)
  1043. area_id = item["area_id"]
  1044. area_name = item["area_name"]
  1045. st_league = item["st_league"]
  1046. name_chinese = item["name_chinese"]
  1047. ball = item['ball']
  1048. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1049. area_dict = {"南美洲": 3, "北美洲": 4, "欧洲": 5, "大洋洲": 6, "南极洲": 7, "非洲": 1, "世界": 8, "亚洲": 2}
  1050. redis_data_dict = "hg3535_liansai"
  1051. r_hash = hash_func(st_league, name_chinese, ball, 0)
  1052. # url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1053. # if area_name in area_dict:
  1054. # data = {
  1055. # "game_code": "zq",
  1056. # "title": "league",
  1057. # "data": {
  1058. # "name_chinese": name_chinese,
  1059. # "kind": "1",
  1060. # "match_mode": "1",
  1061. # "if_stop": "0",
  1062. # "belong": area_name,
  1063. # "last_time": "2019-01-01 00:00:00",
  1064. # "lg_id": st_league,
  1065. # "source": "hg3535"
  1066. # }
  1067. # }
  1068. # else:
  1069. # if area_name == "足球":
  1070. # data = {
  1071. # "game_code": "zq",
  1072. # "title": "league",
  1073. # "data": {
  1074. # "name_chinese": name_chinese,
  1075. # "kind": "1",
  1076. # "match_mode": "1",
  1077. # "if_stop": "0",
  1078. # "belong": "中国",
  1079. # "last_time": "2019-01-01 00:00:00",
  1080. # "lg_id": st_league,
  1081. # "source": "hg3535"
  1082. # }
  1083. # }
  1084. # else:
  1085. # data = {
  1086. # "game_code": "zq",
  1087. # "title": "league",
  1088. # "data": {
  1089. # "name_chinese": name_chinese,
  1090. # "kind": "1",
  1091. # "match_mode": "1",
  1092. # "if_stop": "0",
  1093. # "belong": 'area_name',
  1094. # "last_time": "2019-01-01 00:00:00",
  1095. # "lg_id": st_league,
  1096. # "source": "hg3535"
  1097. # }
  1098. # }
  1099. # Technology = {"Technology": "Django", "Count": "50"}
  1100. # resp = requests.post('http://sports.5gogo.com/Sports/WriteSports/setSports', json=data)
  1101. # print(resp)
  1102. # print(resp.status_code)
  1103. # print(resp.content)
  1104. # data = json.dumps(data)
  1105. # print(data)
  1106. # r = requests.post(url, data)
  1107. # print(r.content)
  1108. # return item
  1109. if self.redis_db.hexists(redis_data_dict, r_hash):
  1110. pass
  1111. else:
  1112. self.redis_db.hset(redis_data_dict, r_hash, 0)
  1113. if ball == "足球":
  1114. try:
  1115. if area_name in area_dict:
  1116. sql1 = "insert into st_zq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s, %s, %s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1117. self.cursor.execute(sql1, (name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name], "hg3535"))
  1118. else:
  1119. if area_name == "足球":
  1120. sql2 = "insert into st_zq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s, %s) on conflict(lg_id) do update set source=%s;"
  1121. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1122. else:
  1123. sql2 = "insert into st_zq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s, %s, %s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1124. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1125. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1126. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1127. except Exception as e:
  1128. logger.warning("足球联赛插入错误")
  1129. logger.warning(e)
  1130. elif ball == "篮球":
  1131. try:
  1132. if area_name in area_dict:
  1133. sql1 = "insert into st_lq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1134. self.cursor.execute(sql1,
  1135. (name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name],"hg3535"))
  1136. else:
  1137. if area_name == "篮球":
  1138. sql2 = "insert into st_lq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s, %s) on conflict(lg_id) do update set source=%s;"
  1139. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1140. else:
  1141. sql2 = "insert into st_lq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s, %s, %s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1142. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1143. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1144. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1145. except Exception as e:
  1146. logger.warning("篮球联赛插入错误")
  1147. logger.warning(e)
  1148. elif ball == "网球":
  1149. try:
  1150. if area_name in area_dict:
  1151. sql1 = "insert into st_wq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1152. self.cursor.execute(sql1, (name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name],"hg3535"))
  1153. else:
  1154. if area_name == "网球":
  1155. sql2 = "insert into st_wq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s,%s) on conflict(lg_id) do update set source=%s;"
  1156. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1157. else:
  1158. sql2 = "insert into st_wq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1159. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1160. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1161. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1162. except Exception as e:
  1163. logger.warning("网球联赛插入错误")
  1164. logger.warning(e)
  1165. elif ball == "棒球":
  1166. try:
  1167. if area_name in area_dict:
  1168. sql1 = "insert into st_bq_league(name_chinese, lg_id, area_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set area_id = %s,source=%s;"
  1169. self.cursor.execute(sql1,(name_chinese, st_league, area_dict[area_name], "hg3535",utime, area_dict[area_name],"hg3535"))
  1170. else:
  1171. if area_name == "棒球":
  1172. sql2 = "insert into st_bq_league(name_chinese, lg_id, country_id,source) values (%s, %s, %s,%s) on conflict(lg_id) do update set source=%s;"
  1173. self.cursor.execute(sql2, (name_chinese, st_league, 0, "hg3535", "hg3535"))
  1174. else:
  1175. sql2 = "insert into st_bq_league(name_chinese, lg_id, country_id,source,utime) values (%s, %s, %s,%s,%s) on conflict(lg_id) do update set country_id = %s,source=%s;"
  1176. self.cursor.execute(sql2, (name_chinese, st_league, area_id, "hg3535",utime, area_id, "hg3535"))
  1177. sql3 = "insert into st_country(country_id, name_chinese, country_area, country_order,recommend_order, update_time,source) values (%s, %s, %s,%s, %s, %s,%s) on conflict(country_id) do update set update_time =%s,source=%s;"
  1178. self.cursor.execute(sql3, (area_id, area_name, 0, 0, 0, utime, "hg3535", utime, "hg3535"))
  1179. except Exception as e:
  1180. logger.warning("棒球联赛插入错误")
  1181. logger.warning(e)
  1182. def close_spider(self, spider):
  1183. self.conn.commit()
  1184. self.conn.close()
  1185. # ---------------------------------------------------------滚球---------------------------------------------------------
  1186. # 滚球足球 插入
  1187. class Roll_Zuqiupipeline(object):
  1188. def open_spider(self, spider):
  1189. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1190. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1191. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1192. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1193. def process_item(self, item, spider):
  1194. logger = logging.getLogger(__name__)
  1195. # 比赛日期
  1196. try:
  1197. data_game = item['data_game'].split("/")
  1198. month = str(data_game[1].strip())
  1199. day = str(data_game[0])
  1200. except Exception as e:
  1201. logger.warning(e)
  1202. data_game = item['data_game'].split(" ")
  1203. months = str(data_game[1].strip())
  1204. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1205. month = month_dict[months]
  1206. day = str(data_game[0])
  1207. # 比赛时间
  1208. time_game = str(item['time_game'])
  1209. print(time_game)
  1210. # 比赛时间,时间戳
  1211. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  1212. r_ctime = "2019" + "-" + month + "-" + day
  1213. # 现在时间,时间戳
  1214. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1215. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1216. # 比赛id
  1217. competition_id = item['game_id']
  1218. # 联赛id
  1219. league_id = item['league_id']
  1220. # 联赛name
  1221. league_name = item['league_name']
  1222. # 主队
  1223. team_home = item['team_home']
  1224. # 客队
  1225. team_guest = item['team_guest']
  1226. # number
  1227. number = item['number']
  1228. score_home = item['score_home']
  1229. score_guest = item['score_guest']
  1230. half_way = item['half_way']
  1231. pt = item['pt']
  1232. match_score = "{}:{}".format(score_home,score_guest)
  1233. corner_ball = item['corner_ball']
  1234. p_code, p_id = get_pcode(corner_ball, 'concede_size')
  1235. # 构建唯一索引
  1236. half_size_guest = item["half_size_guest"]
  1237. half_size_guest_rule = item["half_size_guest_rule"]
  1238. half_size_home = item["half_size_home"]
  1239. half_size_home_rule = item["half_size_home_rule"]
  1240. try:
  1241. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1242. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1243. # 让球 数据插入数据库
  1244. zqone_intodbs(data1=half_size_home, data2=size_data, data3="half_size_home", data4=half_size_home_rule,
  1245. cursor=self.cursor, redis_db=self.redis_db)
  1246. # 让球 数据插入数据库
  1247. zqone_intodbs(data1=half_size_guest, data2=size_data, data3="half_size_guest", data4=half_size_guest_rule,
  1248. cursor=self.cursor, redis_db=self.redis_db)
  1249. # 全场场大小
  1250. size_guest = item["size_guest"]
  1251. size_guest_rule = item["size_guest_rule"]
  1252. size_home = item["size_home"]
  1253. size_home_rule = item["size_home_rule"]
  1254. zqone_intodbs(data1=size_guest, data2=size_data, data3="size_guest", data4=size_guest_rule,
  1255. cursor=self.cursor, redis_db=self.redis_db)
  1256. zqone_intodbs(data1=size_home, data2=size_data, data3="size_home", data4=size_home_rule,
  1257. cursor=self.cursor, redis_db=self.redis_db)
  1258. # 上半场大小
  1259. half_concede_home_rule = item["half_concede_home_rule"]
  1260. half_concede_home = item["half_concede_home"]
  1261. half_concede_guest_rule = item["half_concede_guest_rule"]
  1262. half_concede_guest = item["half_concede_guest"]
  1263. # 上半场让球
  1264. zqone_intodbs(data1=half_concede_home, data2=size_data, data3="half_concede_home", data4=half_concede_home_rule,
  1265. cursor=self.cursor, redis_db=self.redis_db)
  1266. zqone_intodbs(data1=half_concede_guest, data2=size_data, data3="half_concede_guest", data4=half_concede_guest_rule, cursor=self.cursor, redis_db=self.redis_db)
  1267. concede_guest = item["concede_guest"]
  1268. concede_guest_rule = item["concede_guest_rule"]
  1269. concede_home = item["concede_home"]
  1270. concede_home_rule = item["concede_home_rule"]
  1271. # 全场让球
  1272. zqone_intodbs(data1=concede_guest, data2=size_data, data3="concede_guest", data4=concede_guest_rule,
  1273. cursor=self.cursor, redis_db=self.redis_db)
  1274. zqone_intodbs(data1=concede_home, data2=size_data, data3="concede_home", data4=concede_home_rule,
  1275. cursor=self.cursor,redis_db=self.redis_db)
  1276. #
  1277. p_code, p_id = get_pcode(corner_ball, 'capot')
  1278. # 独赢-------------------------------------------------------------------------------------------------------------------
  1279. half_capot_home = item["half_capot_home"]
  1280. half_capot_guest = item["half_capot_guest"]
  1281. half_capot_dogfall = item["half_capot_dogfall"]
  1282. capot_home = item["capot_home"]
  1283. capot_guest = item["capot_guest"]
  1284. capot_dogfall = item["capot_dogfall"]
  1285. capot_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1286. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1287. # 上半场独赢 主队
  1288. zqtwo_intodbs(data1=half_capot_home, data2=capot_data, data3="half_capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  1289. # 上半场独赢 客队
  1290. zqtwo_intodbs(data1=half_capot_guest, data2=capot_data, data3="half_capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  1291. # 上半场独赢 和
  1292. zqtwo_intodbs(data1=half_capot_dogfall, data2=capot_data, data3="half_capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  1293. #
  1294. # 全场独赢 主队
  1295. zqtwo_intodbs(data1=capot_home, data2=capot_data, data3="capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  1296. # 全场独赢 客队
  1297. zqtwo_intodbs(data1=capot_guest, data2=capot_data, data3="capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  1298. # 全场独赢 和
  1299. zqtwo_intodbs(data1=capot_dogfall, data2=capot_data, data3="capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  1300. #
  1301. # 入球数单双-------------------------------------------------------------------------------------------------------------
  1302. p_code, p_id = get_pcode(corner_ball, 'two_sides')
  1303. odd_even_odd = item["odd_even_odd"]
  1304. odd_even_even = item["odd_even_even"]
  1305. half_odd_even_odd = item["half_odd_even_odd"]
  1306. half_odd_even_even = item["half_odd_even_even"]
  1307. # 全场入球数 单双
  1308. two_sides_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1309. 'p_code': p_code,
  1310. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1311. # 上半场入球数 单双
  1312. zqtwo_intodbs(data1=odd_even_odd, data2=two_sides_data, data3="two_sides_single", data4='单', cursor=self.cursor,redis_db=self.redis_db)
  1313. zqtwo_intodbs(data1=odd_even_even, data2=two_sides_data, data3="two_sides_double", data4='双', cursor=self.cursor,redis_db=self.redis_db)
  1314. # 全场入球数 单双
  1315. zqtwo_intodbs(data1=half_odd_even_odd, data2=two_sides_data, data3="half_two_sides_single", data4='单',
  1316. cursor=self.cursor, redis_db=self.redis_db)
  1317. zqtwo_intodbs(data1=half_odd_even_even, data2=two_sides_data, data3="half_two_sides_double", data4='双',
  1318. cursor=self.cursor, redis_db=self.redis_db)
  1319. # 总入球数 --------------------------------------------------------------------------------------------------------------
  1320. p_code, p_id = get_pcode(corner_ball, 'total_goal')
  1321. total_goals = item['total_goal']
  1322. total_dict = {'total_goal_zero': '0-1', 'total_goal_two': '2-3', 'total_goal_four': '4-6',
  1323. 'total_goal_seven': '7或以上', 'half_total_goal_zero': '0', "half_total_goal_one": '1',
  1324. "half_total_goal_two": '2', "half_total_goal_three": '3或以上'}
  1325. # 全场入球数 单双
  1326. total_goal_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1327. 'p_code': p_code,
  1328. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1329. # 上半场入球数 单双
  1330. for key, value in total_goals.items():
  1331. if value:
  1332. zqtwo_intodbs(data1=value, data2=total_goal_data, data3=key, data4=total_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1333. # 全场半场 --------------------------------------------------------------------------------------------------------------
  1334. half_fulls = item['half_full']
  1335. p_code, p_id = get_pcode(corner_ball, 'half_full')
  1336. full_dict = {"half_full_home_home": "主主", "half_full_home_dogfall": "主和",
  1337. "half_full_home_guest": "主客", "half_full_dogfall_home": "和主",
  1338. "half_full_dogfall_dogfall": "和和", "half_full_dogfall_guest": "和客",
  1339. "half_full_guest_home": "客主", "half_full_guest_dogfall": "客和",
  1340. "half_full_guest_guest": "客客"}
  1341. half_full_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1342. 'p_code': p_code,
  1343. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1344. if half_fulls:
  1345. for key, value in half_fulls.items():
  1346. if value:
  1347. zqtwo_intodbs(data1=value, data2=half_full_data, data3=key, data4=full_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1348. # 波胆------------------------------------------------------------------------------------------------------------------
  1349. bodan_datas = item['bodan_data']
  1350. p_code, p_id = get_pcode(corner_ball, 'bodan')
  1351. bodan_dict = {"bodanhome_one_zero": "1-0", "bodanhome_two_zero": "2-0",
  1352. "bodanhome_two_one": "2-1", "bodanhome_three_zero": "3-0",
  1353. "bodanhome_three_one": "3-1", "bodanhome_three_two": "3-2",
  1354. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  1355. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  1356. "bodanguest_one_zero": "0-1", "bodanguest_two_zero": "0-2",
  1357. "bodanguest_two_one": "1-2", "bodanguest_three_zero": "0-3",
  1358. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  1359. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  1360. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4",
  1361. "bodandogfall_zero_zero": "0-0", "bodandogfall_one_one": "1-1",
  1362. "bodandogfall_two_two": "2-2", "bodandogfall_three_three": "3-3",
  1363. "bodandogfall_four_four": "4-4", "bodanother": "其他",
  1364. "halfbodanhome_one_zero": "1-0", "halfbodanhome_two_zero": "2-0",
  1365. "halfbodanhome_two_one": "2-1", "halfbodanhome_three_zero": "3-0",
  1366. "halfbodanhome_three_one": "3-1", "halfbodanhome_three_two": "3-2",
  1367. "halfbodanguest_one_zero": "0-1", "halfbodanguest_two_zero": "0-2",
  1368. "halfbodanguest_two_one": "1-2", "halfbodanguest_three_zero": "0-3",
  1369. "halfbodanguest_three_one": "1-3", "halfbodanguest_three_two": "2-3",
  1370. "halfbodandogfall_zero_zero": "0-0", "halfbodandogfall_one_one": "1-1",
  1371. "halfbodandogfall_two_two": "2-2", "halfbodandogfall_three_three": "3-3",
  1372. "halfbodanother": "其他"}
  1373. bodan_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1374. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1375. if bodan_datas:
  1376. for key, value in bodan_datas.items():
  1377. zqtwo_intodbs(data1=value, data2=bodan_data, data3=key, data4=bodan_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1378. # 最先进球/最后进球 ------------------------------------------------------------------------------------------------------
  1379. first_last_balls = item['first_last_ball']
  1380. p_code, p_id = get_pcode(corner_ball, 'first_last_ball')
  1381. first_last_dict = {"first_last_ball": "最先进球", "first_ball_home": "最先进球", "first_ball_guest": "最先进球",
  1382. "last_ball_home": "最后进球", "last_ball_guest": "最后进球", "not_ball": "没有进球"}
  1383. first_last_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  1384. 'p_code': p_code,
  1385. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1386. if first_last_balls:
  1387. for key, value in first_last_balls.items():
  1388. # 构建唯一索引
  1389. zqtwo_intodbs(data1=value, data2=first_last_data, data3=key, data4=first_last_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  1390. p_code, p_id = get_pcode(corner_ball, 'temaball')
  1391. full_dicts = item['full_data']
  1392. half_dicts = item['half_data']
  1393. full_dict_rules = item['full_data_rule']
  1394. half_dict_rules = item['half_data_rule']
  1395. data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  1396. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0, 'pt': pt,'match_score':match_score,'team_home':team_home,'team_guest':team_guest,'score_home':score_home,'score_guest':score_guest,'number':number,'time_game':time_game,'half_way':half_way}
  1397. if full_dicts:
  1398. for key, value in full_dicts.items():
  1399. zqtwo_intodbs(data1=value, data2=data, data3=key, data4=full_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  1400. if half_dicts:
  1401. for key, value in half_dicts.items():
  1402. zqtwo_intodbs(data1=value, data2=data, data3=key, data4=half_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  1403. #这日期在这里有问题 先行注释
  1404. # match_date, match_time, time3 = news_time(ctime)
  1405. # n_time = out_time(time3, 1.5)
  1406. # 插入st_zq_competition表
  1407. # if pt == 0:
  1408. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  1409. 'game_id': competition_id,
  1410. 'match_date': r_ctime, 'match_time': '00:00:00', 'utime': utime, 'number': number,
  1411. 'source': "hg3535", "expire_time": expire_time, 'is_rollball': 1, "us_time":'2019-01-01 00:00:00','time_game':time_game}
  1412. zqone_competitions(data=data_competition, cursor=self.cursor)
  1413. except Exception as e:
  1414. logger.warning(e)
  1415. return item
  1416. def close_spider(self, spider):
  1417. # self.conn.close()
  1418. self.conn.commit()
  1419. self.conn.close()
  1420. # 滚球篮球 插入
  1421. class Roll_Lanqiupipeline(object):
  1422. def open_spider(self, spider):
  1423. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1424. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1425. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1426. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1427. def process_item(self, item, spider):
  1428. # 使用twisted将mysql插入变成异步执行
  1429. logger = logging.getLogger(__name__)
  1430. # 联赛id
  1431. league_id = item['league_id']
  1432. # 联赛名
  1433. league_name = item['league_name']
  1434. # result = item['result']
  1435. # 比赛id
  1436. game_id = item['game_id']
  1437. # 球队1 #home_team
  1438. team_home = item['team_home']
  1439. # 球队2 # guest_team
  1440. team_guest = item['team_guest']
  1441. # 数量(97>)
  1442. number = item['number']
  1443. # 比赛状态
  1444. zhuangtai = item['zhuangtai']
  1445. # 日期
  1446. # data_game = item['data_game']
  1447. try:
  1448. data_game = item['data_game'].split("/")
  1449. month = str(data_game[1].strip())
  1450. day = str(data_game[0])
  1451. except Exception as e:
  1452. logger.warning(e)
  1453. data_game = item['data_game'].split(" ")
  1454. months = str(data_game[1].strip())
  1455. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1456. month = month_dict[months]
  1457. day = str(data_game[0])
  1458. # 比赛时间
  1459. time_game = str(item['time_game'])
  1460. # 比赛时间,时间戳
  1461. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  1462. r_ctime = "2019" + "-" + month + "-" + day
  1463. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))#过期时间
  1464. # 现在时间,时间戳
  1465. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1466. # 队1分数
  1467. score_home = item['score_home']
  1468. # 队2分数
  1469. score_guest = item['score_guest']
  1470. # 第几节
  1471. jijie = item['jijie']
  1472. # 球队得分
  1473. qiudui = item['qiudui']
  1474. pt = item['pt']
  1475. match_score = "{}:{}".format(score_home,score_guest)
  1476. concedes_dict = item['concede']
  1477. concedes_dict_rule = item['concede_rule']
  1478. odd_evens_dict = item['odd_even']
  1479. odd_evens_dict_rule = item['odd_even_rule']
  1480. total_sizes_dict = item['total_size']
  1481. total_sizes_dict_rule = item['total_size_rule']
  1482. last_numbers_dict = item['last_number']
  1483. capots_dict = item['capot']
  1484. team_scores_dict = item['team_score']
  1485. team_scores_dict_rule = item['team_score_rule']
  1486. try:
  1487. if concedes_dict:
  1488. for key, value in concedes_dict.items():
  1489. if value:
  1490. for x, y in enumerate(value):
  1491. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=1)
  1492. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=1,odd=y)
  1493. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1494. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime, y, 1, "concede",x, "hg3535", new_hash, concedes_dict_rule[key][x],odds_only,1,utime, y,odds_only))
  1495. # res = cursor.fetchone()
  1496. # 更新主队st_zq_odds_record表
  1497. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1498. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime,y, 1, "concede", x, "hg3535", concedes_dict_rule[key][x],odds_only,1))
  1499. #插入结果表
  1500. sql3 = "insert into st_lq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,status) values (%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1501. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, number, '', '',match_score,'',utime,time_game,jijie,number,int(game_id),0,0,'hg3535',1,utime,match_score,time_game,jijie,score_home,score_guest))
  1502. #插入记录表
  1503. sql4 = "insert into st_lq_result_record(lg_id, home_team, guest_team, home_score, guest_score, first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1504. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '',match_score,'',utime,time_game,jijie,number,int(game_id),'hg3535',1))
  1505. if odd_evens_dict:
  1506. for key, value in odd_evens_dict.items():
  1507. if value:
  1508. for x, y in enumerate(value):
  1509. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=2)
  1510. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=2,odd=y)
  1511. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1512. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime,y, 2, "two_sides",x, "hg3535", new_hash, odd_evens_dict_rule[key][x],odds_only,1,utime, y,odds_only))
  1513. # 更新主队st_zq_odds_record表
  1514. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s);"
  1515. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime,y, 2, "two_sides", x, "hg3535", odd_evens_dict_rule[key][x],odds_only,1))
  1516. if total_sizes_dict:
  1517. for key, value in total_sizes_dict.items():
  1518. if value:
  1519. for x, y in enumerate(value):
  1520. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=3)
  1521. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=3,odd=y)
  1522. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1523. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime,y, 3, "total_size",x, "hg3535", new_hash, total_sizes_dict_rule[key][x],odds_only,1,utime, y,odds_only))
  1524. # 更新主队st_zq_odds_record表
  1525. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1526. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime, y, 3, "total_size", x, "hg3535", total_sizes_dict_rule[key][x],odds_only,1))
  1527. last_home = last_numbers_dict['last_home']
  1528. if last_home:
  1529. for key, value in last_home.items():
  1530. if value:
  1531. new_hash = hash_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4)
  1532. odds_only = r_func(match_id=game_id, odds_code="last_home", sort=0, p_id=4,odd=value)
  1533. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1534. self.cursor.execute(sql1, (int(league_id), "last_home", int(game_id), utime, utime, value, 4, "last_number",0, "hg3535", new_hash, key,odds_only,1,utime, value,odds_only))
  1535. # 更新主队st_zq_odds_record表
  1536. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1537. self.cursor.execute(sql2, (int(league_id), "last_home", int(game_id), utime, utime,value, 4, "last_number", 0, "hg3535", key,odds_only,1))
  1538. last_guest = last_numbers_dict['last_guest']
  1539. if last_guest:
  1540. for key, value in last_guest.items():
  1541. if value:
  1542. new_hash = hash_func(match_id=game_id, odds_code="last_guest" + key, sort=0, p_id=4)
  1543. odds_only = r_func(match_id=game_id, odds_code="last_guest", sort=0, p_id=4,odd=value)
  1544. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1545. self.cursor.execute(sql1, (int(league_id), "last_guest", int(game_id), utime, utime, value, 4, "last_number",0, "hg3535", new_hash, key,odds_only,1,utime, value,odds_only))
  1546. # 更新主队st_zq_odds_record表
  1547. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1548. self.cursor.execute(sql2, (int(league_id), "last_guest", int(game_id), utime, utime,value, 4, "last_number", 0, "hg3535", key,odds_only,1))
  1549. if capots_dict:
  1550. for key, value in capots_dict.items():
  1551. if value:
  1552. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  1553. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5,odd=value)
  1554. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,is_rollball) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1555. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime, value, 5, "capot",0, "hg3535", new_hash, odds_only,1,utime, value,odds_only))
  1556. # 更新主队st_zq_odds_record表
  1557. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only,is_rollball) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1558. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime, value, 5, "capot", 0, "hg3535",odds_only,1))
  1559. if team_scores_dict:
  1560. for key, value in team_scores_dict.items():
  1561. if value:
  1562. for x, y in enumerate(value):
  1563. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=6)
  1564. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=6,odd=y)
  1565. sql1 = "insert into st_lq_odds(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1566. self.cursor.execute(sql1, (int(league_id), key, int(game_id), utime, utime, y, 6, "team_score", x, "hg3535", new_hash,team_scores_dict_rule[key][x], odds_only,1,utime, y,odds_only))
  1567. # 更新主队st_zq_odds_record表
  1568. sql2 = "insert into st_lq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1569. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime,y, 6, "team_score", x, "hg3535",team_scores_dict_rule[key][x],odds_only,1))
  1570. # 插入赛事表
  1571. Competition_sql = "insert into st_lq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,type,source,expire_time,is_rollball,status) values (%s,%s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s,%s,%s) on conflict(match_id) do update set tag = %s,expire_time = %s;"
  1572. print(Competition_sql)
  1573. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, game_id, r_ctime, time_game, utime, utime, number, pt, "hg3535",expire_time,1,1,number,expire_time))
  1574. except Exception as e:
  1575. logger.warning(e)
  1576. return item
  1577. def close_spider(self, spider):
  1578. self.conn.commit()
  1579. self.conn.close()
  1580. # 滚球网球 插入
  1581. class Roll_Wangqiupipeline(object):
  1582. def open_spider(self, spider):
  1583. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1584. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1585. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1586. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1587. def process_item(self, item, spider):
  1588. logger = logging.getLogger(__name__)
  1589. # 联赛id
  1590. league_id = item['league_id']
  1591. # 联赛名
  1592. league_name = item['league_name']
  1593. # result = item['result']
  1594. # 比赛id
  1595. game_id = item['game_id']
  1596. # 球队1
  1597. team_home = item['team_home']
  1598. # 球队2
  1599. team_guest = item['team_guest']
  1600. # 数量(97>)
  1601. number = item['number']
  1602. # 比赛状态
  1603. zhuangtai = item['zhuangtai']
  1604. # 日期
  1605. # data_game = item['data_game']
  1606. try:
  1607. data_game = item['data_game'].split("/")
  1608. month = str(data_game[1].strip())
  1609. day = str(data_game[0])
  1610. except Exception as e:
  1611. logger.warning(e)
  1612. data_game = item['data_game'].split(" ")
  1613. months = str(data_game[1].strip())
  1614. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1615. month = month_dict[months]
  1616. day = str(data_game[0])
  1617. # 比赛时间
  1618. time_game = str(item['time_game'])
  1619. # 比赛时间,时间戳
  1620. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  1621. r_ctime = "2019" + "-" + month + "-" + day
  1622. # 现在时间,时间戳
  1623. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1624. # 队1分数
  1625. score_home = item['score_home']
  1626. # 队2分数
  1627. score_guest = item['score_guest']
  1628. # 第几节
  1629. jijie = item['jijie']
  1630. # 球队得分
  1631. qiudui = item['qiudui']
  1632. pt = item['pt']
  1633. #取不到 暂时注掉
  1634. match_date, match_time = new_times(ctime)
  1635. # 让盘
  1636. concedes_dict = item['concedes_dict']
  1637. concedes_dict_rule = item['concedes_dict_rule']
  1638. # 冠军 独赢
  1639. kemps_dict = item['kemps_dict']
  1640. # 让局
  1641. bureaus_dict = item['bureaus_dict']
  1642. bureaus_dict_rule = item['bureaus_dict_rule']
  1643. # 总局数大小
  1644. total_number_dict = item['total_number_dict']
  1645. total_number_dict_rule = item['total_number_dict_rule']
  1646. # 总局数单双
  1647. odd_evens_dict = item['odd_evens_dict']
  1648. odd_evens_dict_rule = item['odd_evens_dict_rule']
  1649. if concedes_dict:
  1650. for key, value in concedes_dict.items():
  1651. if value:
  1652. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  1653. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1,odd=value)
  1654. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1655. self.cursor.execute(sql1, (
  1656. int(league_id), key, int(game_id), utime, utime,value, 1, "dishes_home", 0, "hg3535", new_hash,
  1657. concedes_dict_rule[key],odds_only,1,utime, value,odds_only))
  1658. # 更新主队st_zq_odds_record表
  1659. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1660. self.cursor.execute(sql2, (
  1661. int(league_id), key, int(game_id), utime, utime, value, 1, "dishes_home", 0, "hg3535",
  1662. concedes_dict_rule[key],odds_only,1))
  1663. #插入结果表
  1664. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1665. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1666. # 插入记录表
  1667. sql4 = "insert into st_wq_result_record(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"
  1668. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00'))
  1669. if kemps_dict:
  1670. for key, value in kemps_dict.items():
  1671. if value:
  1672. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1673. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  1674. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1675. self.cursor.execute(sql1, (
  1676. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", new_hash,odds_only,1,utime, value,odds_only))
  1677. # 更新主队st_zq_odds_record表
  1678. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s,%s,%s);"
  1679. self.cursor.execute(sql2, (
  1680. int(league_id), key, int(game_id), utime, utime,value, 2, "kemp", 0, "hg3535",odds_only,1))
  1681. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1682. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1683. if bureaus_dict:
  1684. for key, value in bureaus_dict.items():
  1685. if value:
  1686. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  1687. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3,odd=value)
  1688. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only =%s;"
  1689. self.cursor.execute(sql1, (
  1690. int(league_id), key, int(game_id), utime, utime, value, 3, "bureau", 0, "hg3535", new_hash,
  1691. bureaus_dict_rule[key],odds_only,1,utime, value,odds_only))
  1692. # 更新主队st_zq_odds_record表
  1693. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1694. self.cursor.execute(sql2, (
  1695. int(league_id), key, int(game_id), utime, utime,value, 3, "bureau", 0, "hg3535",
  1696. bureaus_dict_rule[key],odds_only,1))
  1697. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1698. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1699. if total_number_dict:
  1700. for key, value in total_number_dict.items():
  1701. if value:
  1702. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  1703. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4,odd=value)
  1704. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1705. self.cursor.execute(sql1, (
  1706. int(league_id), key, int(game_id), utime, utime,value, 4, "total_number", 0, "hg3535", new_hash,
  1707. total_number_dict_rule[key],odds_only,1,utime, value,odds_only))
  1708. # 更新主队st_zq_odds_record表
  1709. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1710. self.cursor.execute(sql2, (
  1711. int(league_id), key, int(game_id), utime, utime, value, 4, "total_number", 0, "hg3535",
  1712. total_number_dict_rule[key],odds_only,1))
  1713. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1714. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1715. if odd_evens_dict:
  1716. for key, value in odd_evens_dict.items():
  1717. if value:
  1718. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  1719. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5,odd=value)
  1720. sql1 = "insert into st_wq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1721. self.cursor.execute(sql1, (
  1722. int(league_id), key, int(game_id), utime, utime,value, 5, "two_game_sides", 0, "hg3535", new_hash,
  1723. odd_evens_dict_rule[key],odds_only,1,utime, value,odds_only))
  1724. # 更新主队st_zq_odds_record表
  1725. sql2 = "insert into st_wq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1726. self.cursor.execute(sql2, (
  1727. int(league_id), key, int(game_id), utime, utime, value, 5, "two_game_sides", 0, "hg3535",odd_evens_dict_rule[key],odds_only,1))
  1728. sql3 = "insert into st_wq_result(lg_id, home_player_name, guest_player_name, home_player_let_plate, guest_player_let_plate, all_inning,home_player_score,guest_player_score,status,first_score_player,last_score_player,first_inning_score,second_inning_score,third_inning_score,match_winer_player,update_time,match_process,tag,match_id,source,match_time) values (%s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_process =%s,home_player_score =%s,guest_player_score =%s;"
  1729. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,int(score_home), int(score_guest), 0, score_home,score_guest,1,'','','','','','',utime,jijie,number,game_id,'hg3535','00:00',utime,jijie,score_home,score_guest))
  1730. # 插入赛事表
  1731. Competition_sql = "insert into st_wq_competition(home_team, guest_team, lg_id, match_id, match_date, match_time,ctime, utime, tag,type,source,is_rollball,status) values (%s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s,%s) on conflict(match_id) do update set tag = %s;"
  1732. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, pt, "hg3535", 1, 1, number))
  1733. return item
  1734. def close_spider(self, spider):
  1735. self.conn.commit()
  1736. self.conn.close()
  1737. # 滚球棒球 插入
  1738. class Roll_Banqiupipeline(object):
  1739. def open_spider(self, spider):
  1740. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  1741. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  1742. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1743. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1744. def process_item(self, item, spider):
  1745. # 使用twisted将mysql插入变成异步执行
  1746. # 联赛id
  1747. league_id = item['league_id']
  1748. # 联赛名
  1749. league_name = item['league_name']
  1750. # result = item['result']
  1751. # 比赛id
  1752. game_id = item['game_id']
  1753. # 球队1
  1754. team_home = item['team_home']
  1755. # 球队2
  1756. team_guest = item['team_guest']
  1757. # 数量(97>)
  1758. number = item['number']
  1759. #新增 match_score_dict
  1760. match_score_dict = item['match_score_dict']
  1761. # 比赛状态
  1762. zhuangtai = item['zhuangtai']
  1763. # 日期
  1764. # data_game = item['data_game']
  1765. data_game = item['data_game'].split("/")
  1766. month = str(data_game[1].strip())
  1767. day = str(data_game[0])
  1768. # 比赛时间
  1769. # time_game = str(item['time_game'])
  1770. # 比赛时间,时间戳
  1771. # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00".strip()
  1772. r_ctime = "2019" + "-" + month + "-" + day
  1773. # 现在时间,时间戳
  1774. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1775. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1776. # 队1分数
  1777. score_home = item['score_home']
  1778. # 队2分数
  1779. score_guest = item['score_guest']
  1780. # 第几节
  1781. jijie = item['jijie']
  1782. # 球队得分
  1783. qiudui = item['qiudui']
  1784. pt = item['pt']
  1785. #滚球这个位置获取不到这些字段
  1786. # match_date, match_time = new_time(ctime)
  1787. # 让球
  1788. concedes_dict = item['concedes_dict']
  1789. concedes_dict_rule = item['concedes_dict_rule']
  1790. # 独赢
  1791. capots_dict = item['capots_dict']
  1792. # 总得分大小
  1793. total_size_dict = item['total_size_dict']
  1794. total_size_dict_rule = item['total_size_dict_rule']
  1795. # 总得分单双
  1796. odd_evens_dict = item['odd_evens_dict']
  1797. odd_evens_dict_rule = item['odd_evens_dict_rule']
  1798. # 让球
  1799. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  1800. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1801. # bqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor)
  1802. # 总得分:大/小
  1803. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "total_size",
  1804. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1805. # bqone_intodb(data1=total_size_dict, data2=total_size, data3=total_size_dict_rule, cursor=self.cursor)
  1806. odd_even = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "two_sides",
  1807. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1808. # bqone_intodb(data1=odd_evens_dict, data2=odd_even, data3=odd_evens_dict_rule, cursor=self.cursor)
  1809. # 赛事失效时间
  1810. # n_time = out_time(ctime, 3.5)
  1811. # 插入独赢
  1812. if capots_dict:
  1813. for key, value in capots_dict.items():
  1814. if value:
  1815. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1816. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  1817. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,odds_only,expire_time,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s,expire_time=%s;"
  1818. self.cursor.execute(sql1, (
  1819. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", new_hash,
  1820. odds_only, expire_time,1,utime, value, odds_only,expire_time))
  1821. # 更新主队st_zq_odds_record表
  1822. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime,odds, p_id, p_code, sort, source,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s,%s);"
  1823. self.cursor.execute(sql2, (
  1824. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", odds_only,1))
  1825. res = json.dumps(match_score_dict)
  1826. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1827. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, number, 1,'','',qiudui,'',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,qiudui,r_ctime,jijie,score_home,score_guest))
  1828. # 插入赛事表
  1829. Competition_sql = "insert into st_bq_competition(home_team,guest_team,lg_id,status,match_id,ctime,utime,tag,source,type,is_rollball) values(%s, %s, %s, %s, %s, %s, %s,%s, %s,%s,%s) on conflict(match_id) do update set tag=%s,is_rollball=%s,utime=%s;"
  1830. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, 1,game_id,utime, utime, number,"hg3535",1,1,number,1,utime))
  1831. #让球
  1832. if concedes_dict:
  1833. for key, value in concedes_dict.items():
  1834. if value:
  1835. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  1836. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1,odd=value)
  1837. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1838. self.cursor.execute(sql1, (
  1839. int(league_id), key, int(game_id), utime, utime,value, 1, "concede", 0, "hg3535", new_hash,
  1840. concedes_dict_rule[key],odds_only,1,utime, value,odds_only))
  1841. # 更新主队st_zq_odds_record表
  1842. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1843. self.cursor.execute(sql2, (
  1844. int(league_id), key, int(game_id), utime, utime, value, 1, "concede", 0, "hg3535",
  1845. concedes_dict_rule[key],odds_only,1))
  1846. #插入结果表
  1847. res = json.dumps(match_score_dict)
  1848. # r = pymysql.escape_string(res)
  1849. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score_t = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1850. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, number, 1,'','','','',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,res,r_ctime,jijie,score_home,score_guest))
  1851. # 插入记录表
  1852. sql4 = "insert into st_bq_result_record(lg_id, home_team, guest_team, home_score, guest_score,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1853. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '','','',utime,r_ctime,jijie,number,int(game_id),'hg3535',0))
  1854. if total_size_dict:
  1855. for key, value in total_size_dict.items():
  1856. if value:
  1857. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1858. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  1859. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1860. self.cursor.execute(sql1, (
  1861. int(league_id), key, int(game_id), utime, utime,value, 2, "total_size", 0, "hg3535", new_hash,
  1862. total_size_dict_rule[key],odds_only,1,utime, value,odds_only))
  1863. # 更新主队st_zq_odds_record表
  1864. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1865. self.cursor.execute(sql2, (
  1866. int(league_id), key, int(game_id), utime, utime, value, 2, "total_size", 0, "hg3535",
  1867. total_size_dict_rule[key],odds_only,1))
  1868. #插入结果表
  1869. res = json.dumps(match_score_dict)
  1870. # r = pymysql.escape_string(res)
  1871. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score_t = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1872. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, number, 1,'','','','',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,res,r_ctime,jijie,score_home,score_guest))
  1873. # 插入记录表
  1874. sql4 = "insert into st_bq_result_record(lg_id, home_team, guest_team, home_score, guest_score,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1875. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '','','',utime,r_ctime,jijie,number,int(game_id),'hg3535',1))
  1876. if odd_evens_dict:
  1877. for key, value in odd_evens_dict.items():
  1878. if value:
  1879. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  1880. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3,odd=value)
  1881. sql1 = "insert into st_bq_odds(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, sole,condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s) on conflict(sole) do update set utime = %s,odds = %s,odds_only=%s;"
  1882. self.cursor.execute(sql1, (
  1883. int(league_id), key, int(game_id), utime, utime,value, 3, "two_sides", 0, "hg3535", new_hash,
  1884. odd_evens_dict_rule[key],odds_only,1,utime, value,odds_only))
  1885. # 更新主队st_zq_odds_record表
  1886. sql2 = "insert into st_bq_odds_record(lg_id, odds_code, match_id, ctime, utime, odds, p_id, p_code, sort, source, condition,odds_only,is_rollball) values (%s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s,%s);"
  1887. self.cursor.execute(sql2, (
  1888. int(league_id), key, int(game_id), utime, utime, value, 3, "two_sides", 0, "hg3535",
  1889. odd_evens_dict_rule[key],odds_only,1))
  1890. #插入结果表
  1891. res = json.dumps(match_score_dict)
  1892. # r = pymysql.escape_string(res)
  1893. sql3 = "insert into st_bq_result(lg_id, home_team, guest_team, home_score, guest_score, all_goal, status,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,u_home_score,u_guest_score,source,match_score_t) values (%s,%s,%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s,%s,%s) on conflict(match_id) do update set update_time = %s,match_score_t = %s,match_time =%s,match_process =%s,home_score =%s,guest_score =%s;"
  1894. self.cursor.execute(sql3, (int(league_id), team_home, team_guest,score_home, score_guest, number, 1,'','','','',utime,r_ctime,jijie,number,int(game_id),0,0,'hg3535',res,utime,res,r_ctime,jijie,score_home,score_guest))
  1895. # 插入记录表
  1896. sql4 = "insert into st_bq_result_record(lg_id, home_team, guest_team, home_score, guest_score,first_score, last_score,match_score,match_winer,update_time,match_time,match_process,tag,match_id,source,status) values (%s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s,%s,%s,%s);"
  1897. self.cursor.execute(sql4, (int(league_id), team_home, team_guest,score_home, score_guest, '', '','','',utime,r_ctime,jijie,number,int(game_id),'hg3535',1))
  1898. return item
  1899. def close_spider(self, spider):
  1900. self.conn.commit()
  1901. self.conn.close()
  1902. # 足球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1903. class Zujieshuqiupipeline(object):
  1904. def open_spider(self, spider):
  1905. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1906. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1907. def process_item(self, item, spider):
  1908. logger = logging.getLogger(__name__)
  1909. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1910. # 赛事id
  1911. match_id = item['id_score'][0]
  1912. # 全场比分
  1913. score = item['id_score'][1]
  1914. status = 0
  1915. game_code = 'zq'
  1916. try:
  1917. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  1918. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  1919. self.cursor.execute("update st_zq_result set status=2 where match_id={}".format(match_id))
  1920. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  1921. self.cursor.execute("update st_zq_competition set status=2 where match_id={}".format(match_id))
  1922. # url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1923. # pass
  1924. # data = {
  1925. # "game_code": "zq",
  1926. # "title": "match_result",
  1927. # "data": {
  1928. # "lg_id": 27686,
  1929. # "home_team": " ",
  1930. # "guest_team": " ",
  1931. # "home_rate": " ",
  1932. # "guest_rate": " ",
  1933. # "home_score": " ",
  1934. # "guest_score": " ",
  1935. # "all_goal": " ",
  1936. # "status": "2",
  1937. # "first_score": " ",
  1938. # "last_score": " ",
  1939. # "match_score": " ",
  1940. # "match_winer": " ",
  1941. # "match_time": " ",
  1942. # "match_process": " ",
  1943. # "tag": "111",
  1944. # "match_id": match_id,
  1945. # "u_home_score": "111",
  1946. # "u_guest_score": "11",
  1947. # "update_time": ctime,
  1948. # "p_code": "111",
  1949. # "source": "hg3535",
  1950. # }
  1951. # }
  1952. # data = json.dumps(data)
  1953. # print(data)
  1954. # r = requests.post(url, data=data)
  1955. # print(r.content)
  1956. except Exception as e:
  1957. logger.warning('足球 赛事结束 结果插入错误')
  1958. logger.warning(e)
  1959. return item
  1960. def close_spider(self, spider):
  1961. self.conn.commit()
  1962. self.conn.close()
  1963. # 篮球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1964. class Lanjieshuqiupipeline(object):
  1965. def open_spider(self, spider):
  1966. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1967. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1968. def process_item(self, item, spider):
  1969. logger = logging.getLogger(__name__)
  1970. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1971. # 赛事id
  1972. match_id = item['id_score'][0]
  1973. # 全场比分
  1974. score = item['id_score'][1]
  1975. status = 0
  1976. game_code = 'lq'
  1977. try:
  1978. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  1979. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  1980. self.cursor.execute("update st_lq_result set status=2 where match_id={}".format(match_id))
  1981. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  1982. self.cursor.execute("update st_lq_competition set status=2 where match_id={}".format(match_id))
  1983. except Exception as e:
  1984. logger.warning('篮球 赛事结束 结果插入错误')
  1985. logger.warning(e)
  1986. return item
  1987. def close_spider(self, spider):
  1988. self.conn.commit()
  1989. self.conn.close()
  1990. # 棒球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  1991. class Bangjieshuqiupipeline(object):
  1992. def open_spider(self, spider):
  1993. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  1994. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  1995. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  1996. def process_item(self, item, spider):
  1997. logger = logging.getLogger(__name__)
  1998. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1999. # 赛事id
  2000. match_id = item['id_score'][0]
  2001. # 全场比分
  2002. score = item['id_score'][1]
  2003. status = 0
  2004. game_code = 'lq'
  2005. try:
  2006. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  2007. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  2008. self.cursor.execute("update st_bq_result set status=2 where match_id={}".format(match_id))
  2009. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  2010. self.cursor.execute("update st_bq_competition set status=2 where match_id={}".format(match_id))
  2011. except Exception as e:
  2012. logger.warning('棒球 赛事结束 结果插入错误')
  2013. logger.warning(e)
  2014. return item
  2015. def close_spider(self, spider):
  2016. self.conn.commit()
  2017. self.conn.close()
  2018. # 网球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  2019. class Wangjieshuqiupipeline(object):
  2020. def open_spider(self, spider):
  2021. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  2022. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2023. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2024. def process_item(self, item, spider):
  2025. logger = logging.getLogger(__name__)
  2026. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2027. # 赛事id
  2028. match_id = item['id_score'][0]
  2029. # 全场比分
  2030. score = item['id_score'][1]
  2031. status = 0
  2032. game_code = 'lq'
  2033. try:
  2034. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  2035. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  2036. self.cursor.execute("update st_wq_result set status=2 where match_id={}".format(match_id))
  2037. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  2038. self.cursor.execute("update st_wq_competition set status=2 where match_id={}".format(match_id))
  2039. except Exception as e:
  2040. logger.warning('网球 赛事结束 结果插入错误')
  2041. logger.warning(e)
  2042. return item
  2043. def close_spider(self, spider):
  2044. self.conn.commit()
  2045. self.conn.close()