pipelines.py 173 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071
  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, fuhao, one_post
  13. # 足球 赔率 赛事---------------------------------------------------------------------------------------------------------
  14. class Zuqiupipeline(object):
  15. def process_item(self, item, spider):
  16. # 使用twisted将mysql插入变成异步执行
  17. logger = logging.getLogger(__name__)
  18. # logger.warning(query.addErrback(self.handle_error, item, spider))
  19. # 比赛日期
  20. try:
  21. data_game = item['data_game'].split("/")
  22. print(data_game)
  23. month = str(data_game[1].strip())
  24. day = str(data_game[0])
  25. except Exception as e:
  26. logger.warning(e)
  27. data_game = item['data_game'].split(" ")
  28. months = str(data_game[1].strip())
  29. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  30. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  31. month = month_dict[months]
  32. day = str(data_game[0])
  33. # 比赛时间
  34. time_game = str(item['time_game'])
  35. # 比赛时间,时间戳
  36. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  37. r_ctime = "2019" + "-" + month + "-" + day
  38. # 现在时间,时间戳
  39. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  40. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  41. # 比赛id
  42. competition_id = item['game_id']
  43. # 联赛id
  44. league_id = item['league_id']
  45. # 联赛name
  46. league_name = item['league_name']
  47. # # 主队
  48. team_home = item['team_home']
  49. # 客队
  50. team_guest = item['team_guest']
  51. # number
  52. number = item['number']
  53. pt = item['pt']
  54. corner_ball = item['corner_ball']
  55. p_code, p_id = get_pcode(corner_ball, 'goal_size')
  56. # 构建唯一索引
  57. half_size_guest = item["half_size_guest"]
  58. half_size_guest_rule = item["half_size_guest_rule"]
  59. half_size_home = item["half_size_home"]
  60. half_size_home_rule = item["half_size_home_rule"]
  61. zuqiu = item['zuqiu']
  62. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  63. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt}
  64. # 让球 数据插入数据库
  65. # try:
  66. # zqone_intodb(data1=half_size_home, data2=size_data, data3="half_goal_size_home", data4=half_size_home_rule,
  67. # cursor=self.cursor, redis_db=self.redis_db)
  68. # except Exception as e:
  69. # logger.warning('足球half_size_home插入错误')
  70. # logger.warning(e)
  71. if half_size_home:
  72. for index, value in enumerate(half_size_home):
  73. if value:
  74. new_hash = hash_func(match_id=competition_id, odds_code='half_goal_size_big', sort=index, p_id=p_id)
  75. r_hash = r_func(match_id=competition_id, odds_code='half_goal_size_big', sort=index, p_id=p_id, odd=value)
  76. odds_only = r_hash
  77. f1 = fuhao(str(half_size_home_rule[index]))
  78. payload_concede = {
  79. "game_code": "bq",
  80. "title": "odds",
  81. "data": {
  82. "match_id": competition_id,
  83. "lg_id": league_id,
  84. "odds_code": "half_goal_size_big",
  85. "status": 0,
  86. "sort": 0,
  87. "p_code": p_code,
  88. "odds": value,
  89. "condition": f1,
  90. "odds_only": new_hash,
  91. "sole": odds_only,
  92. "source": "hg3535",
  93. "type": 0,
  94. "team": ""
  95. }
  96. }
  97. # if redis_db.hexists("hg3535_zuqiu", r_hash):
  98. # pass
  99. # else:
  100. # redis_db.hset("hg3535_zuqiu", r_hash, 0)
  101. # sql1 = "insert into st_zq_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,condition=%s;"
  102. # cursor.execute(sql1, (
  103. # int(data2['league_id']), data3, int(data2['game_id']), data2['utime'], data2['utime'],
  104. # float(value),
  105. # data2['p_id'], data2["p_code"], index, data2["source"], new_hash,
  106. # f1, data2['expire_time'], odds_only, data2['utime'], value, data2['expire_time'],
  107. # odds_only, f1))
  108. #
  109. # # 更新主队st_zq_odds_record表
  110. # sql2 = "insert into st_zq_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);"
  111. # cursor.execute(sql2, (
  112. # int(data2['league_id']), data3, int(data2['game_id']), data2['utime'], data2['utime'],
  113. # float(value),
  114. # data2['p_id'], data2["p_code"], index, data2["source"], f1, odds_only))
  115. # print(item)
  116. # # 让球 数据插入数据库
  117. # try:
  118. # zqone_intodb(data1=half_size_guest, data2=size_data, data3="half_goal_size_guest", data4=half_size_guest_rule,
  119. # cursor=self.cursor, redis_db=self.redis_db)
  120. # except Exception as e:
  121. # logger.warning('足球half_size_guest插入错误')
  122. # logger.warning(e)
  123. if half_size_guest:
  124. for index, value in enumerate(half_size_guest):
  125. if value:
  126. new_hash = hash_func(match_id=competition_id, odds_code='half_goal_size_small', sort=index, p_id=p_id)
  127. r_hash = r_func(match_id=competition_id, odds_code='half_goal_size_small', sort=index, p_id=p_id, odd=value)
  128. odds_only = r_hash
  129. f1 = fuhao(str(half_size_guest_rule[index]))
  130. payload_concede = {
  131. "game_code": "zq",
  132. "title": "odds",
  133. "data": {
  134. "match_id": competition_id,
  135. "lg_id": league_id,
  136. "odds_code": "half_goal_size_small",
  137. "status": 0,
  138. "sort": 0,
  139. "p_code": p_code,
  140. "odds": value,
  141. "condition": f1,
  142. "odds_only": new_hash,
  143. "sole": odds_only,
  144. "source": "hg3535",
  145. "type": 0,
  146. "team": ""
  147. }
  148. }
  149. # 全场场大小
  150. size_guest = item["size_guest"]
  151. size_guest_rule = item["size_guest_rule"]
  152. size_home = item["size_home"]
  153. size_home_rule = item["size_home_rule"]
  154. # try:
  155. # zqone_intodb(data1=size_guest, data2=size_data, data3="goal_size_guest", data4=size_guest_rule,
  156. # cursor=self.cursor, redis_db=self.redis_db)
  157. # except Exception as e:
  158. # logger.warning('足球size_guest插入错误')
  159. # logger.warning(e)
  160. if size_guest:
  161. for index, value in enumerate(size_guest):
  162. if value:
  163. new_hash = hash_func(match_id=competition_id, odds_code='goal_size_small', sort=index, p_id=p_id)
  164. r_hash = r_func(match_id=competition_id, odds_code='goal_size_small', sort=index, p_id=p_id, odd=value)
  165. odds_only = r_hash
  166. f1 = fuhao(str(size_guest_rule[index]))
  167. payload_concede = {
  168. "game_code": "zq",
  169. "title": "odds",
  170. "data": {
  171. "match_id": competition_id,
  172. "lg_id": league_id,
  173. "odds_code": "goal_size_small",
  174. "status": 0,
  175. "sort": 0,
  176. "p_code": p_code,
  177. "odds": value,
  178. "condition": f1,
  179. "odds_only": r_hash,
  180. "sole": new_hash,
  181. "source": "hg3535",
  182. "type": 0,
  183. "team": ""
  184. }
  185. }
  186. # try:
  187. # zqone_intodb(data1=size_home, data2=size_data, data3="goal_size_home", data4=size_home_rule,
  188. # cursor=self.cursor, redis_db=self.redis_db)
  189. # except Exception as e:
  190. # logger.warning('足球size_home插入错误')
  191. # logger.warning(e)
  192. if size_home:
  193. for index, value in enumerate(size_home):
  194. if value:
  195. new_hash = hash_func(match_id=competition_id, odds_code='goal_size_big', sort=index, p_id=p_id)
  196. r_hash = r_func(match_id=competition_id, odds_code='goal_size_big', sort=index, p_id=p_id, odd=value)
  197. odds_only = r_hash
  198. f1 = fuhao(str(size_home_rule[index]))
  199. payload_concede = {
  200. "game_code": "zq",
  201. "title": "odds",
  202. "data": {
  203. "match_id": competition_id,
  204. "lg_id": league_id,
  205. "odds_code": "goal_size_big",
  206. "status": 0,
  207. "sort": 0,
  208. "p_code": p_code,
  209. "odds": value,
  210. "condition": f1,
  211. "odds_only": new_hash,
  212. "sole": r_hash,
  213. "source": "hg3535",
  214. "type": 0,
  215. "team": ""
  216. }
  217. }
  218. p_code, p_id = get_pcode(corner_ball, 'concede')
  219. concede_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  220. 'source': "hg3535", 'expire_time': expire_time, 'pt': pt}
  221. # 上半场大小
  222. half_concede_home_rule = item["half_concede_home_rule"]
  223. half_concede_home = item["half_concede_home"]
  224. half_concede_guest_rule = item["half_concede_guest_rule"]
  225. half_concede_guest = item["half_concede_guest"]
  226. # 上半场让球
  227. # try:
  228. # zqone_intodb(data1=half_concede_home, data2=concede_data, data3="half_concede_home", data4=half_concede_home_rule,
  229. # cursor=self.cursor, redis_db=self.redis_db)
  230. # except Exception as e:
  231. # logger.warning('足球half_concede_home插入错误')
  232. # logger.warning(e)
  233. if half_concede_home:
  234. for index, value in enumerate(half_concede_home):
  235. if value:
  236. new_hash = hash_func(match_id=competition_id, odds_code='half_concede_home', sort=index, p_id=p_id)
  237. r_hash = r_func(match_id=competition_id, odds_code='half_concede_home', sort=index, p_id=p_id, odd=value)
  238. # odds_only = r_hash
  239. f1 = fuhao(str(half_concede_home_rule[index]))
  240. payload_concede = {
  241. "game_code": "zq",
  242. "title": "odds",
  243. "data": {
  244. "match_id": competition_id,
  245. "lg_id": league_id,
  246. "odds_code": "half_concede_home",
  247. "status": 0,
  248. "sort": 0,
  249. "p_code": p_code,
  250. "odds": value,
  251. "condition": f1,
  252. "odds_only": new_hash,
  253. "sole": r_hash,
  254. "source": "hg3535",
  255. "type": 0,
  256. "team": ""
  257. }
  258. }
  259. # try:
  260. # 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)
  261. # except Exception as e:
  262. # logger.warning('足球half_concede_guest插入错误')
  263. # logger.warning(e)
  264. if half_concede_guest:
  265. for index, value in enumerate(half_concede_guest):
  266. if value:
  267. new_hash = hash_func(match_id=competition_id, odds_code='half_concede_home', sort=index, p_id=p_id)
  268. r_hash = r_func(match_id=competition_id, odds_code='half_concede_home', sort=index, p_id=p_id, odd=value)
  269. # odds_only = r_hash
  270. f1 = fuhao(str(half_concede_guest_rule[index]))
  271. payload_concede = {
  272. "game_code": "zq",
  273. "title": "odds",
  274. "data": {
  275. "match_id": competition_id,
  276. "lg_id": league_id,
  277. "odds_code": "half_concede_guest",
  278. "status": 0,
  279. "sort": 0,
  280. "p_code": p_code,
  281. "odds": value,
  282. "condition": f1,
  283. "odds_only": r_hash,
  284. "sole": new_hash,
  285. "source": "hg3535",
  286. "type": 0,
  287. "team": ""
  288. }
  289. }
  290. concede_guest = item["concede_guest"]
  291. concede_guest_rule = item["concede_guest_rule"]
  292. concede_home = item["concede_home"]
  293. concede_home_rule = item["concede_home_rule"]
  294. # 全场让球
  295. # try:
  296. # zqone_intodb(data1=concede_guest, data2=concede_data, data3="concede_guest", data4=concede_guest_rule,
  297. # cursor=self.cursor, redis_db=self.redis_db)
  298. # except Exception as e:
  299. # logger.warning('足球concede_guest插入错误')
  300. # logger.warning(e)
  301. if half_concede_guest:
  302. for index, value in enumerate(half_concede_guest):
  303. if value:
  304. new_hash = hash_func(match_id=competition_id, odds_code='half_concede_home', sort=index, p_id=p_id)
  305. r_hash = r_func(match_id=competition_id, odds_code='half_concede_home', sort=index, p_id=p_id, odd=value)
  306. # odds_only = r_hash
  307. f1 = fuhao(str(half_concede_guest_rule[index]))
  308. payload_concede = {
  309. "game_code": "zq",
  310. "title": "odds",
  311. "data": {
  312. "match_id": competition_id,
  313. "lg_id": league_id,
  314. "odds_code": "half_concede_guest",
  315. "status": 0,
  316. "sort": 0,
  317. "p_code": p_code,
  318. "odds": value,
  319. "condition": f1,
  320. "odds_only": r_hash,
  321. "sole": new_hash,
  322. "source": "hg3535",
  323. "type": 0,
  324. "team": ""
  325. }
  326. }
  327. # try:
  328. # zqone_intodb(data1=concede_home, data2=concede_data, data3="concede_home", data4=concede_home_rule,
  329. # cursor=self.cursor,redis_db=self.redis_db)
  330. # except Exception as e:
  331. # logger.warning('足球concede_home插入错误')
  332. # logger.warning(e)
  333. p_code, p_id = get_pcode(corner_ball, 'capot')
  334. # 独赢----------------------------------------------------------------------------------------------------------
  335. half_capot_home = item["half_capot_home"]
  336. half_capot_guest = item["half_capot_guest"]
  337. half_capot_dogfall = item["half_capot_dogfall"]
  338. capot_home = item["capot_home"]
  339. capot_guest = item["capot_guest"]
  340. capot_dogfall = item["capot_dogfall"]
  341. capot_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  342. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  343. # 上半场独赢 主队
  344. # try:
  345. # zqtwo_intodb(data1=half_capot_home, data2=capot_data, data3="half_capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  346. # except Exception as e:
  347. # logger.warning('足球half_capot_home插入错误')
  348. # logger.warning(e)
  349. if half_capot_home:
  350. new_hash = hash_func(match_id=competition_id, odds_code='half_capot_home', sort=0, p_id=p_id)
  351. r_hash = r_func(match_id=competition_id, odds_code='half_capot_home', sort=0, p_id=p_id, odd=half_capot_home)
  352. payload_concede = {
  353. "game_code": "zq",
  354. "title": "odds",
  355. "data": {
  356. "match_id": competition_id,
  357. "lg_id": league_id,
  358. "odds_code": "half_capot_home",
  359. "status": 0,
  360. "sort": 0,
  361. "p_code": p_code,
  362. "odds": half_capot_home,
  363. "condition": 1,
  364. "odds_only": r_hash,
  365. "sole": new_hash,
  366. "source": "hg3535",
  367. "type": 0,
  368. "team": ""
  369. }
  370. }
  371. # odds_only = r_hash
  372. # if redis_db.hexists("hg3535_zuqiu", r_hash):
  373. # pass
  374. # else:
  375. # redis_db.hset("hg3535_zuqiu", r_hash, 0)
  376. # sql1 = "insert into st_zq_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,condition=%s;"
  377. # cursor.execute(sql1, (
  378. # int(data2['league_id']), data3, int(data2['game_id']), data2['utime'], data2['utime'], data1,
  379. # data2['p_id'], data2["p_code"], data2['sort'], data2["source"], new_hash,
  380. # data4, data2['expire_time'], odds_only, data2['utime'], data1, data2['expire_time'], odds_only,
  381. # data4))
  382. # # 更新主队st_zq_odds_record表
  383. # sql2 = "insert into st_zq_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);"
  384. # cursor.execute(sql2, (
  385. # int(data2['league_id']), data3, int(data2['game_id']), data2['utime'], data2['utime'], data1,
  386. # data2['p_id'], data2["p_code"], data2['sort'], data2["source"], data4, odds_only))
  387. # 上半场独赢 客队
  388. # try:
  389. # zqtwo_intodb(data1=half_capot_guest, data2=capot_data, data3="half_capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  390. # except Exception as e:
  391. # logger.warning('足球half_capot_guest插入错误')
  392. # logger.warning(e)
  393. if half_capot_guest:
  394. new_hash = hash_func(match_id=competition_id, odds_code='half_capot_guest', sort=0, p_id=p_id)
  395. r_hash = r_func(match_id=competition_id, odds_code='half_capot_guest', sort=0, p_id=p_id, odd=half_capot_guest)
  396. payload_concede = {
  397. "game_code": "zq",
  398. "title": "odds",
  399. "data": {
  400. "match_id": competition_id,
  401. "lg_id": league_id,
  402. "odds_code": "half_capot_guest",
  403. "status": 0,
  404. "sort": 0,
  405. "p_code": p_code,
  406. "odds": half_capot_guest,
  407. "condition": 2,
  408. "odds_only": r_hash,
  409. "sole": new_hash,
  410. "source": "hg3535",
  411. "type": 0,
  412. "team": ""
  413. }
  414. }
  415. # 上半场独赢 和
  416. # try:
  417. # zqtwo_intodb(data1=half_capot_dogfall, data2=capot_data, data3="half_capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  418. # except Exception as e:
  419. # logger.warning('足球half_capot_dogfall插入错误')
  420. # logger.warning(e)
  421. #
  422. if half_capot_dogfall:
  423. new_hash = hash_func(match_id=competition_id, odds_code='half_capot_dogfall', sort=0, p_id=p_id)
  424. r_hash = r_func(match_id=competition_id, odds_code='half_capot_dogfall', sort=0, p_id=p_id, odd=half_capot_dogfall)
  425. payload_concede = {
  426. "game_code": "zq",
  427. "title": "odds",
  428. "data": {
  429. "match_id": competition_id,
  430. "lg_id": league_id,
  431. "odds_code": "half_capot_dogfall",
  432. "status": 0,
  433. "sort": 0,
  434. "p_code": p_code,
  435. "odds": half_capot_dogfall,
  436. "condition": 'x',
  437. "odds_only": r_hash,
  438. "sole": new_hash,
  439. "source": "hg3535",
  440. "type": 0,
  441. "team": ""
  442. }
  443. }
  444. # 全场独赢 主队
  445. # try:
  446. # zqtwo_intodb(data1=capot_home, data2=capot_data, data3="capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  447. # except Exception as e:
  448. # logger.warning('足球capot_home插入错误')
  449. # logger.warning(e)
  450. if capot_home:
  451. new_hash = hash_func(match_id=competition_id, odds_code='half_capot_dogfall', sort=0, p_id=p_id)
  452. r_hash = r_func(match_id=competition_id, odds_code='half_capot_dogfall', sort=0, p_id=p_id, odd=capot_home)
  453. payload_concede = {
  454. "game_code": "zq",
  455. "title": "odds",
  456. "data": {
  457. "match_id": competition_id,
  458. "lg_id": league_id,
  459. "odds_code": "capot_home",
  460. "status": 0,
  461. "sort": 0,
  462. "p_code": p_code,
  463. "odds": capot_home,
  464. "condition": 1,
  465. "odds_only": r_hash,
  466. "sole": new_hash,
  467. "source": "hg3535",
  468. "type": 0,
  469. "team": ""
  470. }
  471. }
  472. # 全场独赢 客队
  473. # try:
  474. # zqtwo_intodb(data1=capot_guest, data2=capot_data, data3="capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  475. # except Exception as e:
  476. # logger.warning('足球capot_guest插入错误')
  477. # logger.warning(e)
  478. if capot_guest:
  479. new_hash = hash_func(match_id=competition_id, odds_code='capot_guest', sort=0, p_id=p_id)
  480. r_hash = r_func(match_id=competition_id, odds_code='capot_guest', sort=0, p_id=p_id, odd=capot_guest)
  481. payload_concede = {
  482. "game_code": "zq",
  483. "title": "odds",
  484. "data": {
  485. "match_id": competition_id,
  486. "lg_id": league_id,
  487. "odds_code": "capot_guest",
  488. "status": 0,
  489. "sort": 0,
  490. "p_code": p_code,
  491. "odds": capot_guest,
  492. "condition": 1,
  493. "odds_only": r_hash,
  494. "sole": new_hash,
  495. "source": "hg3535",
  496. "type": 0,
  497. "team": ""
  498. }
  499. }
  500. # 全场独赢 和
  501. # try:
  502. # zqtwo_intodb(data1=capot_dogfall, data2=capot_data, data3="capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  503. # except Exception as e:
  504. # logger.warning('足球capot_dogfall插入错误')
  505. # logger.warning(e)
  506. if capot_dogfall:
  507. new_hash = hash_func(match_id=competition_id, odds_code='capot_dogfall', sort=0, p_id=p_id)
  508. r_hash = r_func(match_id=competition_id, odds_code='capot_dogfall', sort=0, p_id=p_id, odd=capot_dogfall)
  509. payload_concede = {
  510. "game_code": "zq",
  511. "title": "odds",
  512. "data": {
  513. "match_id": competition_id,
  514. "lg_id": league_id,
  515. "odds_code": "capot_dogfall",
  516. "status": 0,
  517. "sort": 0,
  518. "p_code": p_code,
  519. "odds": capot_dogfall,
  520. "condition": 1,
  521. "odds_only": r_hash,
  522. "sole": new_hash,
  523. "source": "hg3535",
  524. "type": 0,
  525. "team": ""
  526. }
  527. }
  528. #
  529. # 入球数单双-------------------------------------------------------------------------------------------------------------
  530. p_code, p_id = get_pcode(corner_ball, 'two_sides')
  531. odd_even_odd = item["odd_even_odd"]
  532. odd_even_even = item["odd_even_even"]
  533. half_odd_even_odd = item["half_odd_even_odd"]
  534. half_odd_even_even = item["half_odd_even_even"]
  535. # 全场入球数 单双
  536. two_sides_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  537. 'p_code': p_code,
  538. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  539. # 上半场入球数 单双
  540. # try:
  541. # zqtwo_intodb(data1=odd_even_odd, data2=two_sides_data, data3="two_sides_single", data4='单', cursor=self.cursor,redis_db=self.redis_db)
  542. # except Exception as e:
  543. # logger.warning('足球odd_even_odd插入错误')
  544. # logger.warning(e)
  545. one_post(data=odd_even_odd, match_id=competition_id, odds_code='two_sides_single', p_id=p_id, league_id=league_id, p_code=p_code, condition='单')
  546. # url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  547. # data = {
  548. # "game_code": "zq",
  549. # "title": "odds",
  550. # "data": {
  551. # "match_id": competition_id,
  552. # "lg_id": 1123,
  553. # "odds_code": "concede_guest",
  554. # "status": 0,
  555. # "sort": 0,
  556. # "p_code": "concede_size",
  557. # "odds": 1.2333,
  558. # "condition": "+1.5",
  559. # "odds_only": "b15bad3fc8f09924874fef0e19f63d001",
  560. # "sole": "5e2225944d8cdc44cd7943330986978d2",
  561. # "source": "c22123",
  562. # "type": 0,
  563. # "team": ""
  564. # }
  565. # }
  566. # data = json.dumps(data)
  567. # print(data)
  568. # r = requests.post(url, data=data)
  569. # print(r.content)
  570. # try:
  571. # zqtwo_intodb(data1=odd_even_even, data2=two_sides_data, data3="two_sides_double", data4='双', cursor=self.cursor,redis_db=self.redis_db)
  572. # except Exception as e:
  573. # logger.warning('足球odd_even_even插入错误')
  574. # logger.warning(e)
  575. one_post(data=odd_even_even, match_id=competition_id, odds_code='two_sides_double', p_id=p_id,
  576. league_id=league_id, p_code=p_code, condition='双')
  577. # 全场入球数 单双
  578. # try:
  579. # zqtwo_intodb(data1=half_odd_even_odd, data2=two_sides_data, data3="half_two_sides_single", data4='单',
  580. # cursor=self.cursor, redis_db=self.redis_db)
  581. # except Exception as e:
  582. # logger.warning('足球half_odd_even_odd插入错误')
  583. # logger.warning(e)
  584. one_post(data=half_odd_even_odd, match_id=competition_id, odds_code='half_two_sides_single', p_id=p_id,
  585. league_id=league_id, p_code=p_code, condition='单')
  586. # try:
  587. # zqtwo_intodb(data1=half_odd_even_even, data2=two_sides_data, data3="half_two_sides_double", data4='双',
  588. # cursor=self.cursor, redis_db=self.redis_db)
  589. # except Exception as e:
  590. # logger.warning('足球half_odd_even_even插入错误')
  591. # logger.warning(e)
  592. one_post(data=half_odd_even_even, match_id=competition_id, odds_code='half_two_sides_double', p_id=p_id,
  593. league_id=league_id, p_code=p_code, condition='双')
  594. # 总入球数 --------------------------------------------------------------------------------------------------------------
  595. p_code, p_id = get_pcode(corner_ball, 'total_goal')
  596. total_goals = item['total_goal']
  597. total_dict = {'total_goal_zero': '0-1', 'total_goal_two': '2-3', 'total_goal_four': '4-6',
  598. 'total_goal_seven': '7或以上', 'half_total_goal_zero': '0', "half_total_goal_one": '1',
  599. "half_total_goal_two": '2', "half_total_goal_three": '3或以上'}
  600. # 全场入球数 单双
  601. total_goal_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  602. 'p_code': p_code,
  603. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  604. # 上半场入球数 单双
  605. for key, value in total_goals.items():
  606. if value:
  607. s_hash = hash_func(match_id=competition_id, odds_code=key, sort=0, p_id=p_id)
  608. o_hash = r_func(match_id=competition_id, odds_code=key, sort=0, p_id=p_id, odd=value)
  609. payload_concede = {
  610. "game_code": "zq",
  611. "title": "odds",
  612. "data": {
  613. "match_id": competition_id,
  614. "lg_id": league_id,
  615. "odds_code": key,
  616. "status": 0,
  617. "sort": 0,
  618. "p_code": p_code,
  619. "odds": value,
  620. "condition": 1,
  621. "odds_only": o_hash,
  622. "sole": s_hash,
  623. "source": "hg3535",
  624. "type": 0,
  625. "team": ""
  626. }
  627. }
  628. # try:
  629. # zqtwo_intodb(data1=value, data2=total_goal_data, data3=key, data4=total_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  630. # except Exception as e:
  631. # logger.warning('足球total_dict插入错误')
  632. # logger.warning(e)
  633. # 全场半场 --------------------------------------------------------------------------------------------------------------
  634. half_fulls = item['half_full']
  635. p_code, p_id = get_pcode(corner_ball, 'half_full')
  636. full_dict = {"half_full_home_home": "主主", "half_full_home_dogfall": "主和",
  637. "half_full_home_guest": "主客", "half_full_dogfall_home": "和主",
  638. "half_full_dogfall_dogfall": "和和", "half_full_dogfall_guest": "和客",
  639. "half_full_guest_home": "客主", "half_full_guest_dogfall": "客和",
  640. "half_full_guest_guest": "客客"}
  641. half_full_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  642. 'p_code': p_code,
  643. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  644. if half_fulls:
  645. for key, value in half_fulls.items():
  646. if value:
  647. try:
  648. zqtwo_intodb(data1=value, data2=half_full_data, data3=key, data4=full_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  649. except Exception as e:
  650. logger.warning('足球full_dict插入错误')
  651. logger.warning(e)
  652. # 波胆------------------------------------------------------------------------------------------------------------------
  653. bodan_datas = item['bodan_data']
  654. p_code, p_id = get_pcode(corner_ball, 'bodan')
  655. bodan_dict = {"bodanhome_one_zero": "1-0", "bodanhome_two_zero": "2-0",
  656. "bodanhome_two_one": "2-1", "bodanhome_three_zero": "3-0",
  657. "bodanhome_three_one": "3-1", "bodanhome_three_two": "3-2",
  658. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  659. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  660. "bodanguest_one_zero": "0-1", "bodanguest_two_zero": "0-2",
  661. "bodanguest_two_one": "1-2", "bodanguest_three_zero": "0-3",
  662. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  663. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  664. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4",
  665. "bodandogfall_zero_zero": "0-0", "bodandogfall_one_one": "1-1",
  666. "bodandogfall_two_two": "2-2", "bodandogfall_three_three": "3-3",
  667. "bodandogfall_four_four": "4-4", "bodanother": "其他",
  668. "halfbodanhome_one_zero": "1-0", "halfbodanhome_two_zero": "2-0",
  669. "halfbodanhome_two_one": "2-1", "halfbodanhome_three_zero": "3-0",
  670. "halfbodanhome_three_one": "3-1", "halfbodanhome_three_two": "3-2",
  671. "halfbodanguest_one_zero": "0-1", "halfbodanguest_two_zero": "0-2",
  672. "halfbodanguest_two_one": "1-2", "halfbodanguest_three_zero": "0-3",
  673. "halfbodanguest_three_one": "1-3", "halfbodanguest_three_two": "2-3",
  674. "halfbodandogfall_zero_zero": "0-0", "halfbodandogfall_one_one": "1-1",
  675. "halfbodandogfall_two_two": "2-2", "halfbodandogfall_three_three": "3-3",
  676. "halfbodanother": "其他"}
  677. bodan_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  678. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  679. if bodan_datas:
  680. for key, value in bodan_datas.items():
  681. try:
  682. zqtwo_intodb(data1=value, data2=bodan_data, data3=key, data4=bodan_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  683. except Exception as e:
  684. logger.warning('足球bodan_datas插入错误')
  685. logger.warning(e)
  686. # 最先进球/最后进球 ------------------------------------------------------------------------------------------------------
  687. first_last_balls = item['first_last_ball']
  688. p_code, p_id = get_pcode(corner_ball, 'first_last_ball')
  689. first_last_dict = {"first_last_ball": "最先进球", "first_ball_home": "最先进球", "first_ball_guest": "最先进球",
  690. "last_ball_home": "最后进球", "last_ball_guest": "最后进球", "not_ball": "没有进球"}
  691. first_last_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  692. 'p_code': p_code,
  693. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  694. if first_last_balls:
  695. for key, value in first_last_balls.items():
  696. # 构建唯一索引
  697. try:
  698. zqtwo_intodb(data1=value, data2=first_last_data, data3=key, data4=first_last_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  699. except Exception as e:
  700. logger.warning('足球first_last_balls插入错误')
  701. logger.warning(e)
  702. p_code, p_id = get_pcode(corner_ball, 'team_score')
  703. full_dicts = item['full_data']
  704. half_dicts = item['half_data']
  705. full_dict_rules = item['full_data_rule']
  706. half_dict_rules = item['half_data_rule']
  707. data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  708. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  709. if full_dicts:
  710. for key, value in full_dicts.items():
  711. try:
  712. zqtwo_intodb(data1=value, data2=data, data3=key, data4=full_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  713. except Exception as e:
  714. logger.warning('足球full_dicts插入错误')
  715. logger.warning(e)
  716. if half_dicts:
  717. for key, value in half_dicts.items():
  718. try:
  719. zqtwo_intodb(data1=value, data2=data, data3=key, data4=half_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  720. except Exception as e:
  721. logger.warning('足球half_dicts插入错误')
  722. logger.warning(e)
  723. match_date, match_time, time3 = new_time(ctime)
  724. n_time = out_time(time3, 1.5)
  725. # 插入st_zq_competition表
  726. if zuqiu == "足球":
  727. try:
  728. if pt == 1:
  729. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  730. 'game_id': competition_id,
  731. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  732. 'source': "hg3535", "expire_time": n_time, 'is_today': 1, "us_time": ctime}
  733. zqone_competition(data=data_competition, cursor=self.cursor)
  734. if pt == 2:
  735. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  736. 'game_id': competition_id,
  737. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  738. 'source': "hg3535", "expire_time": n_time, 'is_morningplate': 1, "us_time": ctime}
  739. zqtwo_competition(data=data_competition, cursor=self.cursor)
  740. if pt == 3:
  741. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  742. 'game_id': competition_id,
  743. 'match_date': match_date, 'match_time': match_time, 'utime': utime, 'number': number,
  744. 'source': "hg3535", "expire_time": n_time, "is_stringscene": 1, "us_time": ctime}
  745. zqthree_competition(data=data_competition, cursor=self.cursor)
  746. except Exception as e:
  747. logger.warning('足球st_zq_competition插入错误')
  748. logger.warning(e)
  749. return item
  750. def close_spider(self, spider):
  751. self.conn.commit()
  752. self.conn.close()
  753. # 篮球 让球大小----------------------------------------------------------------------------------------------------------
  754. class Lanqiupipeline(object):
  755. def process_item(self, item, spider):
  756. # 使用twisted将mysql插入变成异步执行
  757. logger = logging.getLogger(__name__)
  758. # # logger.warning(query.addErrback(self.handle_error, item, spider))
  759. # 联赛id
  760. league_id = item['league_id']
  761. # 联赛名
  762. league_name = item['league_name']
  763. # result = item['result']
  764. # 比赛id
  765. game_id = item['game_id']
  766. # 球队1
  767. team_home = item['team_home']
  768. # 球队2
  769. team_guest = item['team_guest']
  770. # 数量(97>)
  771. number = item['number']
  772. # 比赛状态
  773. zhuangtai = item['zhuangtai']
  774. # 日期
  775. # data_game = item['data_game']
  776. try:
  777. data_game = item['data_game'].split("/")
  778. month = str(data_game[1].strip())
  779. day = str(data_game[0])
  780. except Exception as e:
  781. logger.warning(e)
  782. data_game = item['data_game'].split(" ")
  783. months = str(data_game[1].strip())
  784. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  785. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  786. month = month_dict[months]
  787. day = str(data_game[0])
  788. # 比赛时间
  789. time_game = str(item['time_game'])
  790. # 比赛时间,时间戳
  791. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  792. r_ctime = "2019" + "-" + month + "-" + day
  793. # 现在时间,时间戳
  794. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  795. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  796. # 队1分数
  797. score_home = item['score_home']
  798. # 队2分数
  799. score_guest = item['score_guest']
  800. # 第几节
  801. jijie = item['jijie']
  802. # 球队得分
  803. qiudui = item['qiudui']
  804. pt = item['pt']
  805. concedes_dict = item['concede']
  806. concedes_dict_rule = item['concede_rule']
  807. odd_evens_dict = item['odd_even']
  808. odd_evens_dict_rule = item['odd_even_rule']
  809. total_sizes_dict = item['total_size']
  810. total_sizes_dict_rule = item['total_size_rule']
  811. last_numbers_dict = item['last_number']
  812. capots_dict = item['capot']
  813. team_scores_dict = item['team_score']
  814. team_scores_dict_rule = item['team_score_rule']
  815. # 让球
  816. lanqiu = item['lanqiu']
  817. # concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  818. # 'source': "hg3535", 'expire_time': expire_time}
  819. # 让球 数据插入数据库
  820. if concedes_dict:
  821. for key, value in concedes_dict.items():
  822. if value:
  823. for x, y in enumerate(value):
  824. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=1)
  825. r_hash = r_func(match_id=game_id, odds_code=key, sort=x, p_id=1, odd=y)
  826. odds_only = r_hash
  827. l_f = fuhao(str(concedes_dict_rule[key][x]))
  828. payload_bodan = {
  829. "game_code": "lq",
  830. "title": "odds",
  831. "data": {
  832. "match_id": game_id,
  833. "lg_id": league_id,
  834. "odds_code": key,
  835. "status": 0,
  836. "sort": 0,
  837. "p_code": "concede",
  838. "odds": y,
  839. "condition": l_f,
  840. "odds_only": r_hash,
  841. "sole": new_hash,
  842. "source": "hg3535",
  843. "type": 0,
  844. "team": ""
  845. }
  846. }
  847. two_sides = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "two_sides",
  848. 'source': "hg3535", 'expire_time': expire_time}
  849. if odd_evens_dict:
  850. for key, value in odd_evens_dict.items():
  851. if value:
  852. for x, y in enumerate(value):
  853. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=2)
  854. r_hash = r_func(match_id=game_id, odds_code=key, sort=x, p_id=2, odd=y)
  855. odds_only = r_hash
  856. l_f = fuhao(str(odd_evens_dict_rule[key][x]))
  857. payload_bodan = {
  858. "game_code": "lq",
  859. "title": "odds",
  860. "data": {
  861. "match_id": game_id,
  862. "lg_id": league_id,
  863. "odds_code": key,
  864. "status": 0,
  865. "sort": 0,
  866. "p_code": "two_sides",
  867. "odds": y,
  868. "condition": l_f,
  869. "odds_only": r_hash,
  870. "sole": new_hash,
  871. "source": "hg3535",
  872. "type": 0,
  873. "team": ""
  874. }
  875. }
  876. # 总分单双 数据插入数据库
  877. # try:
  878. # one_intodb(data1=odd_evens_dict, data2=two_sides, data3=odd_evens_dict_rule, cursor=self.cursor,
  879. # redis_db=self.redis_db)
  880. # except Exception as e:
  881. # logger.warning('篮球odd_evens_dict插入错误')
  882. # logger.warning(e)
  883. # total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "total_size",
  884. # 'source': "hg3535", 'expire_time': expire_time}
  885. if total_sizes_dict:
  886. for key, value in total_sizes_dict.items():
  887. if value:
  888. for x, y in enumerate(value):
  889. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=3)
  890. r_hash = r_func(match_id=game_id, odds_code=key, sort=x, p_id=3, odd=y)
  891. odds_only = r_hash
  892. l_f = fuhao(str(total_sizes_dict_rule[key][x]))
  893. payload_bodan = {
  894. "game_code": "lq",
  895. "title": "odds",
  896. "data": {
  897. "match_id": game_id,
  898. "lg_id": league_id,
  899. "odds_code": key,
  900. "status": 0,
  901. "sort": 0,
  902. "p_code": "total_size",
  903. "odds": y,
  904. "condition": l_f,
  905. "odds_only": r_hash,
  906. "sole": new_hash,
  907. "source": "hg3535",
  908. "type": 0,
  909. "team": ""
  910. }
  911. }
  912. # 全场总分大小 数据插入数据库
  913. # try:
  914. # one_intodb(data1=total_sizes_dict, data2=total_size, data3=total_sizes_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  915. # except Exception as e:
  916. # logger.warning('篮球total_sizes_dict插入错误')
  917. # logger.warning(e)
  918. #
  919. # data4 = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 6, 'p_code': "team_score",
  920. # 'source': "hg3535", 'expire_time': expire_time}
  921. # 全场总分大小 数据插入数据库
  922. if team_scores_dict:
  923. for key, value in team_scores_dict.items():
  924. if value:
  925. for x, y in enumerate(value):
  926. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=6)
  927. r_hash = r_func(match_id=game_id, odds_code=key, sort=x, p_id=6, odd=y)
  928. odds_only = r_hash
  929. l_f = fuhao(str(team_scores_dict_rule[key][x]))
  930. payload_bodan = {
  931. "game_code": "lq",
  932. "title": "odds",
  933. "data": {
  934. "match_id": game_id,
  935. "lg_id": league_id,
  936. "odds_code": key,
  937. "status": 0,
  938. "sort": 0,
  939. "p_code": "team_score",
  940. "odds": y,
  941. "condition": l_f,
  942. "odds_only": r_hash,
  943. "sole": new_hash,
  944. "source": "hg3535",
  945. "type": 0,
  946. "team": ""
  947. }
  948. }
  949. # try:
  950. # one_intodb(data1=team_scores_dict, data2=data4, data3=team_scores_dict_rule, cursor=self.cursor, redis_db=self.redis_db)
  951. # except Exception as e:
  952. # logger.warning('篮球team_scores_dict插入错误')
  953. # logger.warning(e)
  954. #
  955. # # 主队进球最后一位数
  956. # last_number_home = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  957. # 'p_code': "last_number",
  958. # 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  959. # try:
  960. # two_intodb(data1=last_numbers_dict['last_home'], data2=last_number_home, data3="last_home", cursor=self.cursor,redis_db=self.redis_db)
  961. # except Exception as e:
  962. # logger.warning('篮球last_numbers_dict主队插入错误')
  963. # logger.warning(e)
  964. if last_numbers_dict['last_home']:
  965. for key, value in last_numbers_dict['last_home'].items():
  966. if value:
  967. new_hash = hash_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4)
  968. r_hash = r_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4, odd=value)
  969. # odds_only = r_hash
  970. payload_bodan = {
  971. "game_code": "lq",
  972. "title": "odds",
  973. "data": {
  974. "match_id": game_id,
  975. "lg_id": league_id,
  976. "odds_code": "last_home" + key,
  977. "status": 0,
  978. "sort": 0,
  979. "p_code": "team_score",
  980. "odds": value,
  981. "condition": key,
  982. "odds_only": r_hash,
  983. "sole": new_hash,
  984. "source": "hg3535",
  985. "type": 0,
  986. "team": ""
  987. }
  988. }
  989. # print(last_numbers_dict['last_home'])
  990. # 客队进球最后一位数
  991. # last_number_guest = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 4,
  992. # 'p_code': "last_number",
  993. # 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  994. # try:
  995. # two_intodb(data1=last_numbers_dict['last_guest'], data2=last_number_guest, data3="last_guest",
  996. # cursor=self.cursor, redis_db=self.redis_db)
  997. # except Exception as e:
  998. # logger.warning('篮球last_numbers_dict客队插入错误')
  999. # logger.warning(e)
  1000. if last_numbers_dict['last_guest']:
  1001. for key, value in last_numbers_dict['last_guest'].items():
  1002. if value:
  1003. new_hash = hash_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4)
  1004. r_hash = r_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4, odd=value)
  1005. # odds_only = r_hash
  1006. payload_bodan = {
  1007. "game_code": "lq",
  1008. "title": "odds",
  1009. "data": {
  1010. "match_id": game_id,
  1011. "lg_id": league_id,
  1012. "odds_code": "last_home" + key,
  1013. "status": 0,
  1014. "sort": 0,
  1015. "p_code": "team_score",
  1016. "odds": value,
  1017. "condition": key,
  1018. "odds_only": r_hash,
  1019. "sole": new_hash,
  1020. "source": "hg3535",
  1021. "type": 0,
  1022. "team": ""
  1023. }
  1024. }
  1025. # print(last_numbers_dict['last_guest'])
  1026. # 独赢
  1027. # capot_data = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 5, 'p_code': "capot",
  1028. # 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  1029. # try:
  1030. # three_intodb(data1=capots_dict, data2=capot_data, cursor=self.cursor, redis_db=self.redis_db)
  1031. # except Exception as e:
  1032. # logger.warning('篮球capots_dict插入错误')
  1033. # logger.warning(e)
  1034. match_date, match_time, time3 = new_time(ctime)
  1035. n_time = out_time(time3, 1.5)
  1036. us_time = ctime
  1037. if lanqiu == "篮球":
  1038. # if pt == 1:
  1039. # 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;"
  1040. # self.cursor.execute(Competition_sql, (
  1041. # team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  1042. # n_time,1, us_time, number, n_time, 1))
  1043. # if pt == 2:
  1044. # 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;"
  1045. # self.cursor.execute(Competition_sql, (
  1046. # team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  1047. # n_time,1, us_time, number, n_time, 1))
  1048. # if pt == 3:
  1049. # 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;"
  1050. # self.cursor.execute(Competition_sql, (
  1051. # team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  1052. # n_time,1, us_time, number, n_time, 1))
  1053. if pt == 1:
  1054. payload = {
  1055. "game_code": "lq",
  1056. "title": "competition",
  1057. "data": {
  1058. "home_team": team_home,
  1059. "guest_team": team_guest,
  1060. "lg_id": league_id,
  1061. "status": 0,
  1062. "match_id": game_id,
  1063. "match_date": match_date,
  1064. "match_time": match_time,
  1065. "tag": number,
  1066. "source": "hg3535",
  1067. "is_rollball": 0,
  1068. "is_today": 1,
  1069. "is_morningplate": 0,
  1070. "is_stringscene": 0,
  1071. "us_time": us_time
  1072. }
  1073. }
  1074. # resp = requests.post(url, data={"data": json.dumps(payload)})
  1075. # print(json.dumps(payload))
  1076. # print(resp.text)
  1077. if pt == 2:
  1078. payload = {
  1079. "game_code": "lq",
  1080. "title": "competition",
  1081. "data": {
  1082. "home_team": team_home,
  1083. "guest_team": team_guest,
  1084. "lg_id": league_id,
  1085. "status": 0,
  1086. "match_id": game_id,
  1087. "match_date": match_date,
  1088. "match_time": match_time,
  1089. "tag": number,
  1090. "source": "hg3535",
  1091. "is_rollball": 0,
  1092. "is_today": 0,
  1093. "is_morningplate": 1,
  1094. "is_stringscene": 0,
  1095. "us_time": us_time
  1096. }
  1097. }
  1098. # resp = requests.post(url, data={"data": json.dumps(payload)})
  1099. # print(json.dumps(payload))
  1100. # print(resp.text)
  1101. if pt == 3:
  1102. payload = {
  1103. "game_code": "lq",
  1104. "title": "competition",
  1105. "data": {
  1106. "home_team": team_home,
  1107. "guest_team": team_guest,
  1108. "lg_id": league_id,
  1109. "status": 0,
  1110. "match_id": game_id,
  1111. "match_date": match_date,
  1112. "match_time": match_time,
  1113. "tag": number,
  1114. "source": "hg3535",
  1115. "is_rollball": 0,
  1116. "is_today": 0,
  1117. "is_morningplate": 0,
  1118. "is_stringscene": 1,
  1119. "us_time": us_time
  1120. }
  1121. }
  1122. # 插入赛事表
  1123. # if lanqiu == "篮球":
  1124. # try:
  1125. # if pt == 1:
  1126. # 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;"
  1127. # self.cursor.execute(Competition_sql, (
  1128. # team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  1129. # n_time,1, us_time, number, n_time, 1))
  1130. # if pt == 2:
  1131. # 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;"
  1132. # self.cursor.execute(Competition_sql, (
  1133. # team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  1134. # n_time,1, us_time, number, n_time, 1))
  1135. # if pt == 3:
  1136. # 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;"
  1137. # self.cursor.execute(Competition_sql, (
  1138. # team_home, team_guest, league_id, game_id, match_date, match_time, utime, utime, number, "hg3535",
  1139. # n_time,1, us_time, number, n_time, 1))
  1140. # except Exception as e:
  1141. # logger.warning('篮球st_lq_competition插入错误')
  1142. # logger.warning(e)
  1143. # return item
  1144. #
  1145. # def close_spider(self, spider):
  1146. # self.conn.commit()
  1147. # self.conn.close()
  1148. # 网球 让球&大小盘--------------------------------------------------------------------------------------------------------
  1149. class Wangqiupipeline(object):
  1150. def process_item(self, item, spider):
  1151. logger = logging.getLogger(__name__)
  1152. url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1153. # logger.warning(query.addErrback(self.handle_error, item, spider))
  1154. # 联赛id
  1155. league_id = item['league_id']
  1156. # 联赛名
  1157. league_name = item['league_name']
  1158. # result = item['result']
  1159. # 比赛id
  1160. game_id = item['game_id']
  1161. print(game_id)
  1162. # 球队1
  1163. team_home = item['team_home']
  1164. # 球队2
  1165. team_guest = item['team_guest']
  1166. # 数量(97>)
  1167. number = item['number']
  1168. # 比赛状态
  1169. zhuangtai = item['zhuangtai']
  1170. # 日期
  1171. # data_game = item['data_game']
  1172. try:
  1173. data_game = item['data_game'].split("/")
  1174. month = str(data_game[1].strip())
  1175. day = str(data_game[0])
  1176. except Exception as e:
  1177. logger.warning(e)
  1178. data_game = item['data_game'].split(" ")
  1179. months = str(data_game[1].strip())
  1180. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  1181. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1182. month = month_dict[months]
  1183. day = str(data_game[0])
  1184. # 比赛时间
  1185. time_game = str(item['time_game'])
  1186. # 比赛时间,时间戳
  1187. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  1188. r_ctime = "2019" + "-" + month + "-" + day
  1189. # 现在时间,时间戳
  1190. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1191. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1192. # 队1分数
  1193. score_home = item['score_home']
  1194. # 队2分数
  1195. score_guest = item['score_guest']
  1196. # 第几节
  1197. jijie = item['jijie']
  1198. # 球队得分
  1199. qiudui = item['qiudui']
  1200. pt = item['pt']
  1201. # 让盘
  1202. concedes_dict = item['concedes_dict']
  1203. concedes_dict_rule = item['concedes_dict_rule']
  1204. # 冠军
  1205. kemps_dict = item['kemps_dict']
  1206. # 让局
  1207. bureaus_dict = item['bureaus_dict']
  1208. bureaus_dict_rule = item['bureaus_dict_rule']
  1209. # 总局数大小
  1210. total_number_dict = item['total_number_dict']
  1211. total_number_dict_rule = item['total_number_dict_rule']
  1212. # 总局数单双
  1213. odd_evens_dict = item['odd_evens_dict']
  1214. odd_evens_dict_rule = item['odd_evens_dict_rule']
  1215. wangqiu = item['wangqiu']
  1216. us_time = ctime
  1217. match_date, match_time, time3 = new_time(ctime)
  1218. n_time = out_time(time3, 3)
  1219. # 插入赛事表
  1220. if wangqiu == "网球":
  1221. if pt == 1:
  1222. payload = {
  1223. "game_code": "wq",
  1224. "title": "competition",
  1225. "data": {
  1226. "home_team": team_home,
  1227. "guest_team": team_guest,
  1228. "lg_id": league_id,
  1229. "status": 0,
  1230. "match_id": game_id,
  1231. "match_date": match_date,
  1232. "match_time": match_time,
  1233. "tag": number,
  1234. "source": "hg3535",
  1235. "is_rollball": 0,
  1236. "is_today": 1,
  1237. "is_morningplate": 0,
  1238. "is_stringscene": 0,
  1239. "us_time": us_time
  1240. }
  1241. }
  1242. resp = requests.post(url, data={"data": json.dumps(payload)})
  1243. print(json.dumps(payload))
  1244. print(resp.text)
  1245. if pt == 2:
  1246. payload = {
  1247. "game_code": "wq",
  1248. "title": "competition",
  1249. "data": {
  1250. "home_team": team_home,
  1251. "guest_team": team_guest,
  1252. "lg_id": league_id,
  1253. "status": 0,
  1254. "match_id": game_id,
  1255. "match_date": match_date,
  1256. "match_time": match_time,
  1257. "tag": number,
  1258. "source": "hg3535",
  1259. "is_rollball": 0,
  1260. "is_today": 0,
  1261. "is_morningplate": 1,
  1262. "is_stringscene": 0,
  1263. "us_time": us_time
  1264. }
  1265. }
  1266. resp = requests.post(url, data={"data": json.dumps(payload)})
  1267. print(json.dumps(payload))
  1268. print(resp.text)
  1269. if pt == 3:
  1270. payload = {
  1271. "game_code": "wq",
  1272. "title": "competition",
  1273. "data": {
  1274. "home_team": team_home,
  1275. "guest_team": team_guest,
  1276. "lg_id": league_id,
  1277. "status": 0,
  1278. "match_id": game_id,
  1279. "match_date": match_date,
  1280. "match_time": match_time,
  1281. "tag": number,
  1282. "source": "hg3535",
  1283. "is_rollball": 0,
  1284. "is_today": 0,
  1285. "is_morningplate": 0,
  1286. "is_stringscene": 1,
  1287. "us_time": us_time
  1288. }
  1289. }
  1290. resp = requests.post(url, data={"data": json.dumps(payload)})
  1291. print(json.dumps(payload))
  1292. print(resp.text)
  1293. # 让盘
  1294. if concedes_dict:
  1295. for key, value in concedes_dict.items():
  1296. if value:
  1297. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  1298. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1, odd=value)
  1299. odds_only = r_hash
  1300. w_f = fuhao(str(concedes_dict_rule[key]))
  1301. payload_bodan = {
  1302. "game_code": "wq",
  1303. "title": "odds",
  1304. "data": {
  1305. "match_id": game_id,
  1306. "lg_id": league_id,
  1307. "odds_code": key,
  1308. "status": 0,
  1309. "sort": 0,
  1310. "p_code": "concede",
  1311. "odds": value,
  1312. "condition": 0,
  1313. "odds_only": r_hash,
  1314. "sole": new_hash,
  1315. "source": "hg3535",
  1316. "type": 0,
  1317. "team": ""
  1318. }
  1319. }
  1320. # 让局
  1321. if bureaus_dict:
  1322. for key, value in bureaus_dict.items():
  1323. if value:
  1324. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  1325. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3, odd=value)
  1326. odds_only = r_hash
  1327. w_f = fuhao(str(bureaus_dict_rule[key]))
  1328. payload_bodan = {
  1329. "game_code": "wq",
  1330. "title": "odds",
  1331. "data": {
  1332. "match_id": game_id,
  1333. "lg_id": league_id,
  1334. "odds_code": key,
  1335. "status": 0,
  1336. "sort": 0,
  1337. "p_code": "bureau",
  1338. "odds": value,
  1339. "condition": w_f,
  1340. "odds_only": r_hash,
  1341. "sole": new_hash,
  1342. "source": "hg3535",
  1343. "type": 0,
  1344. "team": ""
  1345. }
  1346. }
  1347. if total_number_dict:
  1348. for key, value in total_number_dict.items():
  1349. if value:
  1350. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  1351. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4, odd=value)
  1352. odds_only = r_hash
  1353. w_f = fuhao(str(total_number_dict_rule[key]))
  1354. payload_bodan = {
  1355. "game_code": "wq",
  1356. "title": "odds",
  1357. "data": {
  1358. "match_id": game_id,
  1359. "lg_id": league_id,
  1360. "odds_code": key,
  1361. "status": 0,
  1362. "sort": 0,
  1363. "p_code": "total_number",
  1364. "odds": value,
  1365. "condition": w_f,
  1366. "odds_only": r_hash,
  1367. "sole": new_hash,
  1368. "source": "hg3535",
  1369. "type": 0,
  1370. "team": ""
  1371. }
  1372. }
  1373. if odd_evens_dict:
  1374. for key, value in odd_evens_dict.items():
  1375. if value:
  1376. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  1377. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5, odd=value)
  1378. odds_only = r_hash
  1379. w_f = fuhao(str(odd_evens_dict_rule[key]))
  1380. payload_bodan = {
  1381. "game_code": "wq",
  1382. "title": "odds",
  1383. "data": {
  1384. "match_id": game_id,
  1385. "lg_id": league_id,
  1386. "odds_code": key,
  1387. "status": 0,
  1388. "sort": 0,
  1389. "p_code": "two_game_sides",
  1390. "odds": value,
  1391. "condition": w_f,
  1392. "odds_only": r_hash,
  1393. "sole": new_hash,
  1394. "source": "hg3535",
  1395. "type": 0,
  1396. "team": ""
  1397. }
  1398. }
  1399. # if redis_db.hexists("hg3535_wangqiu", r_hash):
  1400. # pass
  1401. # else:
  1402. # redis_db.hset("hg3535_wangqiu", r_hash, 0)
  1403. # 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,condition=%s;"
  1404. # cursor.execute(sql1, (
  1405. # int(data2['league_id']), key, int(data2['game_id']), data2['utime'], data2['utime'],
  1406. # value,
  1407. # data2['p_id'], data2["p_code"], data2['sort'], data2["source"], new_hash,
  1408. # w_f, data2['expire_time'], odds_only, data2['utime'], value, data2['expire_time'],
  1409. # odds_only, w_f))
  1410. # # 更新主队st_zq_odds_record表
  1411. # 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);"
  1412. # cursor.execute(sql2, (
  1413. # int(data2['league_id']), key, int(data2['game_id']), data2['utime'], data2['utime'],
  1414. # value,
  1415. # data2['p_id'], data2["p_code"], data2['sort'], data2["source"], w_f, odds_only))
  1416. if kemps_dict:
  1417. for key, value in kemps_dict.items():
  1418. if value:
  1419. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1420. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2, odd=value)
  1421. # odds_only = r_hash
  1422. payload_bodan = {
  1423. "game_code": "wq",
  1424. "title": "odds",
  1425. "data": {
  1426. "match_id": game_id,
  1427. "lg_id": league_id,
  1428. "odds_code": key,
  1429. "status": 0,
  1430. "sort": 0,
  1431. "p_code": "bodan",
  1432. "odds": value,
  1433. "condition": 0,
  1434. "odds_only": r_hash,
  1435. "sole": new_hash,
  1436. "source": "hg3535",
  1437. "type": 0,
  1438. "team": ""
  1439. }
  1440. }
  1441. resp = requests.post(url, data={"data": json.dumps(payload_bodan)})
  1442. print(json.dumps(payload_bodan))
  1443. print(resp.text, '网球独赢/冠军')
  1444. return item
  1445. # 网球 波胆--------------------------------------------------------------------------------------------------------------
  1446. class Wqbodanpipeline(object):
  1447. def process_item(self, item, spider):
  1448. # 使用twisted将mysql插入变成异步执行
  1449. url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1450. logger = logging.getLogger(__name__)
  1451. # logger.warning(query.addErrback(self.handle_error, item, spider))
  1452. # 比赛日期
  1453. try:
  1454. data_game = item['data_game'].split("/")
  1455. month = str(data_game[1].strip())
  1456. day = str(data_game[0])
  1457. except Exception as e:
  1458. logger.warning(e)
  1459. data_game = item['data_game'].split(" ")
  1460. months = str(data_game[1].strip())
  1461. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  1462. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1463. month = month_dict[months]
  1464. day = str(data_game[0])
  1465. # 比赛时间
  1466. time_game = str(item['time_game'])
  1467. # 比赛时间,时间戳
  1468. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  1469. r_ctime = "2019" + "-" + month + "-" + day
  1470. # 现在时间,时间戳
  1471. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1472. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1473. # 比赛id
  1474. competition_id = item['game_id']
  1475. # 联赛id
  1476. league_id = item['league_id']
  1477. # 联赛name
  1478. league_name = item['league_name']
  1479. # 主队
  1480. team_home = item['team_home']
  1481. # 客队
  1482. team_guest = item['team_guest']
  1483. # 主队得分
  1484. score_home = item['score_home']
  1485. # 客队得分
  1486. score_guest = item['score_guest']
  1487. # number
  1488. number = item['number']
  1489. corner_ball = item['corner_ball']
  1490. half_way = item['half_way']
  1491. # 类型早盘,今日,滚球,串场
  1492. pt = item['pt']
  1493. bodan_datas = item['bodan_data']
  1494. p_code = "bodan"
  1495. p_id = 7
  1496. bodan_dict = {"bodanhome_two_zero": "2-0", "bodanhome_two_one": "2-1",
  1497. "bodanhome_three_zero": "3-0", "bodanhome_three_one": "3-1",
  1498. "bodanhome_three_two": "3-2",
  1499. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  1500. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  1501. "bodanguest_two_zero": "0-2", "bodanguest_two_one": "1-2",
  1502. "bodanguest_three_zero": "0-3",
  1503. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  1504. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  1505. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4"}
  1506. if bodan_datas:
  1507. for key, value in bodan_datas.items():
  1508. if value:
  1509. new_hash = hash_func(match_id=competition_id, odds_code=key, sort=0, p_id=7)
  1510. r_hash = r_func(match_id=competition_id, odds_code=key, sort=0, p_id=7, odd=value)
  1511. odds_only = r_hash
  1512. payload_bodan = {
  1513. "game_code": "wq",
  1514. "title": "odds",
  1515. "data": {
  1516. "match_id": competition_id,
  1517. "lg_id": league_id,
  1518. "odds_code": key,
  1519. "status": 0,
  1520. "sort": 0,
  1521. "p_code": "bodan",
  1522. "odds": value,
  1523. "condition": bodan_dict[key],
  1524. "odds_only": odds_only,
  1525. "sole": new_hash,
  1526. "source": "hg3535",
  1527. "type": 0,
  1528. "team": ""
  1529. }
  1530. }
  1531. resp = requests.post(url, data={"data": json.dumps(payload_bodan)})
  1532. print(json.dumps(payload_bodan))
  1533. print(resp.text, '网球波胆')
  1534. return item
  1535. # 棒球 让球&大小盘--------------------------------------------------------------------------------------------------------
  1536. class Bangqiupipeline(object):
  1537. def process_item(self, item, spider):
  1538. # print(item)
  1539. logger = logging.getLogger(__name__)
  1540. url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1541. # logger.warning(query.addErrback(self.handle_error, item, spider))
  1542. # 联赛id
  1543. league_id = item['league_id']
  1544. # 联赛名
  1545. league_name = item['league_name']
  1546. # result = item['result']
  1547. # 比赛id
  1548. game_id = item['game_id']
  1549. # 球队1
  1550. team_home = item['team_home']
  1551. # 球队2
  1552. team_guest = item['team_guest']
  1553. # 数量(97>)
  1554. number = item['number']
  1555. # 比赛状态
  1556. zhuangtai = item['zhuangtai']
  1557. # 日期
  1558. # data_game = item['data_game']
  1559. try:
  1560. data_game = item['data_game'].split("/")
  1561. month = str(data_game[1].strip())
  1562. day = str(data_game[0])
  1563. except Exception as e:
  1564. logger.warning('data_game字段分割错误')
  1565. logger.warning(e)
  1566. data_game = item['data_game'].split(" ")
  1567. months = str(data_game[1].strip())
  1568. month_dict = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07',
  1569. 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'}
  1570. month = month_dict[months]
  1571. day = str(data_game[0])
  1572. # 比赛时间
  1573. time_game = str(item['time_game'])
  1574. # 比赛时间,时间戳
  1575. ctime = "2019" + "-" + month + "-" + day + " " + time_game + ":00"
  1576. r_ctime = "2019" + "-" + month + "-" + day
  1577. # 现在时间,时间戳
  1578. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1579. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1580. # 队1分数
  1581. score_home = item['score_home']
  1582. # 队2分数
  1583. score_guest = item['score_guest']
  1584. # 第几节
  1585. jijie = item['jijie']
  1586. # 球队得分
  1587. qiudui = item['qiudui']
  1588. pt = item['pt']
  1589. # 让球
  1590. concedes_dict = item['concedes_dict']
  1591. concedes_dict_rule = item['concedes_dict_rule']
  1592. # 独赢
  1593. capots_dict = item['capots_dict']
  1594. # 总得分大小
  1595. total_size_dict = item['total_size_dict']
  1596. total_size_dict_rule = item['total_size_dict_rule']
  1597. # 总得分单双
  1598. odd_evens_dict = item['odd_evens_dict']
  1599. odd_evens_dict_rule = item['odd_evens_dict_rule']
  1600. bangqiu = item['bangqiu']
  1601. match_date, match_time, time3 = new_time(ctime)
  1602. us_time = ctime
  1603. # 插入赛事表
  1604. if bangqiu == '棒球':
  1605. if pt == 1:
  1606. payload = {
  1607. "game_code": "bq",
  1608. "title": "competition",
  1609. "data": {
  1610. "home_team": team_home,
  1611. "guest_team": team_guest,
  1612. "lg_id": league_id,
  1613. "status": 0,
  1614. "match_id": game_id,
  1615. "match_date": match_date,
  1616. "match_time": match_time,
  1617. "tag": number,
  1618. "source": "hg3535",
  1619. "is_rollball": 0,
  1620. "is_today": 1,
  1621. "is_morningplate": 0,
  1622. "is_stringscene": 0,
  1623. "us_time": us_time
  1624. }
  1625. }
  1626. resp = requests.post(url, data={"data": json.dumps(payload)})
  1627. print(json.dumps(payload))
  1628. print(resp.text)
  1629. if pt == 2:
  1630. payload = {
  1631. "game_code": "bq",
  1632. "title": "competition",
  1633. "data": {
  1634. "home_team": team_home,
  1635. "guest_team": team_guest,
  1636. "lg_id": league_id,
  1637. "status": 0,
  1638. "match_id": game_id,
  1639. "match_date": match_date,
  1640. "match_time": match_time,
  1641. "tag": number,
  1642. "source": "hg3535",
  1643. "is_rollball": 0,
  1644. "is_today": 0,
  1645. "is_morningplate": 1,
  1646. "is_stringscene": 0,
  1647. "us_time": us_time
  1648. }
  1649. }
  1650. resp = requests.post(url, data={"data": json.dumps(payload)})
  1651. print(json.dumps(payload))
  1652. print(resp.text)
  1653. if pt == 3:
  1654. payload = {
  1655. "game_code": "bq",
  1656. "title": "competition",
  1657. "data": {
  1658. "home_team": team_home,
  1659. "guest_team": team_guest,
  1660. "lg_id": league_id,
  1661. "status": 0,
  1662. "match_id": game_id,
  1663. "match_date": match_date,
  1664. "match_time": match_time,
  1665. "tag": number,
  1666. "source": "hg3535",
  1667. "is_rollball": 0,
  1668. "is_today": 0,
  1669. "is_morningplate": 0,
  1670. "is_stringscene": 1,
  1671. "us_time": us_time
  1672. }
  1673. }
  1674. resp = requests.post(url, data={"data": json.dumps(payload)})
  1675. print(json.dumps(payload))
  1676. print(resp.text)
  1677. # 让球
  1678. if concedes_dict:
  1679. for key, value in concedes_dict.items():
  1680. if value:
  1681. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  1682. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1, odd=value)
  1683. odds_only = r_hash
  1684. b_f = fuhao(str(concedes_dict_rule[key]))
  1685. payload_concede = {
  1686. "game_code": "bq",
  1687. "title": "odds",
  1688. "data": {
  1689. "match_id": game_id,
  1690. "lg_id": league_id,
  1691. "odds_code": key,
  1692. "status": 0,
  1693. "sort": 0,
  1694. "p_code": "concede",
  1695. "odds": value,
  1696. "condition": b_f,
  1697. "odds_only": new_hash,
  1698. "sole": odds_only,
  1699. "source": "hg3535",
  1700. "type": 0,
  1701. "team": ""
  1702. }
  1703. }
  1704. resp = requests.post(url, data={"data": json.dumps(payload_concede)})
  1705. print(json.dumps(payload_concede))
  1706. print(resp.text, '棒球让球')
  1707. # 总得分:大/小
  1708. if total_size_dict:
  1709. for key, value in total_size_dict.items():
  1710. if value:
  1711. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  1712. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2, odd=value)
  1713. odds_only = r_hash
  1714. b_f = fuhao(str(total_size_dict_rule[key]))
  1715. payload_total_size = {
  1716. "game_code": "bq",
  1717. "title": "odds",
  1718. "data": {
  1719. "match_id": game_id,
  1720. "lg_id": league_id,
  1721. "odds_code": key,
  1722. "status": 0,
  1723. "sort": 0,
  1724. "p_code": "total_size",
  1725. "odds": value,
  1726. "condition": b_f,
  1727. "odds_only": new_hash,
  1728. "sole": odds_only,
  1729. "source": "hg3535",
  1730. "type": 0,
  1731. "team": ""
  1732. }
  1733. }
  1734. resp = requests.post(url, data={"data": json.dumps(payload_total_size)})
  1735. print(json.dumps(payload_total_size))
  1736. print(resp.text, '-----total_size')
  1737. # 单双
  1738. if odd_evens_dict:
  1739. for key, value in odd_evens_dict.items():
  1740. if value:
  1741. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  1742. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3, odd=value)
  1743. odds_only = r_hash
  1744. b_f = fuhao(str(odd_evens_dict_rule[key]))
  1745. payload_odd_even = {
  1746. "game_code": "bq",
  1747. "title": "odds",
  1748. "data": {
  1749. "match_id": game_id,
  1750. "lg_id": league_id,
  1751. "odds_code": key,
  1752. "status": 0,
  1753. "sort": 0,
  1754. "p_code": "two_sides",
  1755. "odds": value,
  1756. "condition": b_f,
  1757. "odds_only": new_hash,
  1758. "sole": odds_only,
  1759. "source": "hg3535",
  1760. "type": 0,
  1761. "team": ""
  1762. }
  1763. }
  1764. resp = requests.post(url, data={"data": json.dumps(payload_odd_even)})
  1765. print(json.dumps(payload_odd_even))
  1766. print(resp.text, '-----odd_even')
  1767. # 插入独赢
  1768. if capots_dict:
  1769. for key, value in capots_dict.items():
  1770. if value:
  1771. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  1772. r_hash = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4, odd=value)
  1773. odds_only = r_hash
  1774. payload_capots = {
  1775. "game_code": "bq",
  1776. "title": "odds",
  1777. "data": {
  1778. "match_id": game_id,
  1779. "lg_id": league_id,
  1780. "odds_code": key,
  1781. "status": 0,
  1782. "sort": 0,
  1783. "p_code": "capot",
  1784. "odds": value,
  1785. "condition": None,
  1786. "odds_only": odds_only,
  1787. "sole": new_hash,
  1788. "source": "hg3535",
  1789. "type": 0,
  1790. "team": ""
  1791. }
  1792. }
  1793. resp = requests.post(url, data={"data": json.dumps(payload_capots)})
  1794. print(json.dumps(payload_capots))
  1795. print(resp.text, '棒球独赢')
  1796. return item
  1797. # 足球 篮球 网球 棒球 冠军------------------------------------------------------------------------------------------------
  1798. class Guanjunpipeline(object):
  1799. def process_item(self, item, spider):
  1800. logger = logging.getLogger(__name__)
  1801. data_game = item['data_game'].replace('年', "-").replace('月', "-").replace('日', "")
  1802. time_game = item['time_game']
  1803. ctime = data_game + " " + time_game + ":00"
  1804. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1805. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  1806. league_name = item['league_name']
  1807. tema_home = item['tema_home']
  1808. league_id = item['league_id']
  1809. game_id = item['game_id']
  1810. new_league_name = item['new_league_name']
  1811. # 冠军赔率
  1812. champion_team = item['champion_team']
  1813. new_champion = item['new_champion']
  1814. # 构建唯一哈希索引
  1815. time1 = time.mktime(time.strptime(ctime, '%Y-%m-%d %H:%M:%S')) + 43200
  1816. time2 = time.localtime(time1)
  1817. time3 = time.strftime('%Y-%m-%d %H:%M:%S', time2)
  1818. # data_time = str(time3).split(" ")
  1819. # match_date,match_time = new_time(ctime)
  1820. pt = item['pt']
  1821. new_hash = hash_func(match_id=champion_team, odds_code=new_champion, sort=new_league_name, p_id=tema_home)
  1822. ball = item['ball']
  1823. # 插入st_zq_odds表
  1824. r_hash = hash_func(league_id, new_champion, tema_home, champion_team)
  1825. odds_only = r_hash
  1826. s_hash = hash_func(league_id, league_name, ball, 0)
  1827. url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1828. if ball == "足球":
  1829. n_gameid = int('1' + str(league_id))
  1830. payload = {
  1831. "game_code": "zq",
  1832. "title": "competition",
  1833. "data": {
  1834. "home_team": league_name,
  1835. "lg_id": league_id,
  1836. "status": 0,
  1837. "match_id": n_gameid,
  1838. "source": "hg3535",
  1839. "us_time": data_game
  1840. }
  1841. }
  1842. print(json.dumps(payload))
  1843. resp = requests.post(url, data={"data": json.dumps(payload)})
  1844. print(resp.text)
  1845. return item
  1846. # data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1847. # 'champion_team': champion_team, "p_id": 8, 'new_league_name': new_league_name,
  1848. # 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1849. # "odds_only": odds_only,
  1850. # "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1851. # try:
  1852. # gjz_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1853. # 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;"
  1854. # self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1855. # except Exception as e:
  1856. # logger.warning('足球冠军插入错误')
  1857. # logger.warning(e)
  1858. #
  1859. # elif ball == "篮球":
  1860. # n_gameid = int('2' + str(league_id))
  1861. # data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1862. # 'champion_team': champion_team, "p_id": 7, 'new_league_name': new_league_name,
  1863. # 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1864. # "odds_only": odds_only,
  1865. # "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1866. # try:
  1867. # gjl_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1868. # 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;"
  1869. # self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1870. # except Exception as e:
  1871. # logger.warning('篮球冠军插入错误')
  1872. # logger.warning(e)
  1873. #
  1874. # elif ball == "网球":
  1875. # n_gameid = int('3' + str(league_id))
  1876. # data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1877. # 'champion_team': champion_team, "p_id": 6, 'new_league_name': new_league_name,
  1878. # 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1879. # "odds_only": odds_only,
  1880. # "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1881. # try:
  1882. # gjw_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1883. # 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;"
  1884. # self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1885. # except Exception as e:
  1886. # logger.warning('网球冠军插入错误')
  1887. # logger.warning(e)
  1888. #
  1889. #
  1890. # elif ball == "棒球":
  1891. # n_gameid = int('4' + str(league_id))
  1892. # data_dict = {'league_id': league_id, 'new_champion': new_champion, 'utime': utime,
  1893. # 'champion_team': champion_team, "p_id": 5, 'new_league_name': new_league_name,
  1894. # 'tema_home': tema_home, 'new_hash': new_hash, 'expire_time': expire_time,
  1895. # "odds_only": odds_only,
  1896. # "league_name": league_name, 'time3': time3, 'game_id': n_gameid}
  1897. # try:
  1898. # gjb_into(data_dict, self.cursor, r_hash, s_hash, self.redis_db)
  1899. # 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;"
  1900. # self.cursor.execute(Competition_sql, (league_name, league_id, n_gameid, utime, utime, 'hg3535', data_game, utime))
  1901. # except Exception as e:
  1902. # logger.warning('棒球冠军插入错误')
  1903. # logger.warning(e)
  1904. # return item
  1905. #
  1906. # def close_spider(self, spider):
  1907. # self.conn.commit()
  1908. # self.conn.close()
  1909. # 足球 篮球 网球 棒球 联赛------------------------------------------------------------------------------------------------
  1910. class Liansaipipeline(object):
  1911. def process_item(self, item, spider):
  1912. logger = logging.getLogger(__name__)
  1913. area_id = item["area_id"]
  1914. area_name = item["area_name"]
  1915. st_league = item["st_league"]
  1916. name_chinese = item["name_chinese"]
  1917. ball = item['ball']
  1918. # utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  1919. area_dict = {"南美洲": 3, "北美洲": 4, "欧洲": 5, "大洋洲": 6, "南极洲": 7, "非洲": 1, "世界": 8, "亚洲": 2}
  1920. # redis_data_dict = "hg3535_liansai"
  1921. # r_hash = hash_func(st_league, name_chinese, ball, 0)
  1922. url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  1923. if ball == "足球":
  1924. if area_name in area_dict:
  1925. payload = {
  1926. "game_code": "zq",
  1927. "title": "league",
  1928. "data": {
  1929. "name_chinese": name_chinese,
  1930. "kind": "1",
  1931. "match_mode": "1",
  1932. "if_stop": "0",
  1933. "belong": area_name,
  1934. "last_time": "2019-01-01 00:00:00",
  1935. "lg_id": st_league,
  1936. "source": "hg3535"
  1937. }
  1938. }
  1939. else:
  1940. if area_name == "足球":
  1941. payload = {
  1942. "game_code": "zq",
  1943. "title": "league",
  1944. "data": {
  1945. "name_chinese": name_chinese,
  1946. "kind": "1",
  1947. "match_mode": "1",
  1948. "if_stop": "0",
  1949. "belong": " ",
  1950. "last_time": "2019-01-01 00:00:00",
  1951. "lg_id": st_league,
  1952. "source": "hg3535"
  1953. }
  1954. }
  1955. else:
  1956. payload = {
  1957. "game_code": "zq",
  1958. "title": "league",
  1959. "data": {
  1960. "name_chinese": name_chinese,
  1961. "kind": "1",
  1962. "match_mode": "1",
  1963. "if_stop": "0",
  1964. "belong": 'area_name',
  1965. "last_time": "2019-01-01 00:00:00",
  1966. "lg_id": st_league,
  1967. "source": "hg3535"
  1968. }}
  1969. data = {"data": json.dumps(payload)}
  1970. resp = requests.post(url, data=data)
  1971. print(resp.text)
  1972. elif ball == "篮球":
  1973. if area_name in area_dict:
  1974. payload = {
  1975. "game_code": "lq",
  1976. "title": "league",
  1977. "data": {
  1978. "name_chinese": name_chinese,
  1979. "kind": "1",
  1980. "match_mode": "1",
  1981. "if_stop": "0",
  1982. "belong": area_name,
  1983. "last_time": "2019-01-01 00:00:00",
  1984. "lg_id": st_league,
  1985. "source": "hg3535"
  1986. }
  1987. }
  1988. else:
  1989. if area_name == "篮球":
  1990. payload = {
  1991. "game_code": "lq",
  1992. "title": "league",
  1993. "data": {
  1994. "name_chinese": name_chinese,
  1995. "kind": "1",
  1996. "match_mode": "1",
  1997. "if_stop": "0",
  1998. "belong": " ",
  1999. "last_time": "2019-01-01 00:00:00",
  2000. "lg_id": st_league,
  2001. "source": "hg3535"
  2002. }
  2003. }
  2004. else:
  2005. payload = {
  2006. "game_code": "lq",
  2007. "title": "league",
  2008. "data": {
  2009. "name_chinese": name_chinese,
  2010. "kind": "1",
  2011. "match_mode": "1",
  2012. "if_stop": "0",
  2013. "belong": 'area_name',
  2014. "last_time": "2019-01-01 00:00:00",
  2015. "lg_id": st_league,
  2016. "source": "hg3535"
  2017. }}
  2018. data = {"data": json.dumps(payload)}
  2019. resp = requests.post(url, data=data)
  2020. print(resp.text)
  2021. elif ball == "网球":
  2022. if area_name in area_dict:
  2023. payload = {
  2024. "game_code": "wq",
  2025. "title": "league",
  2026. "data": {
  2027. "name_chinese": name_chinese,
  2028. "kind": "1",
  2029. "match_mode": "1",
  2030. "if_stop": "0",
  2031. "belong": area_name,
  2032. "last_time": "2019-01-01 00:00:00",
  2033. "lg_id": st_league,
  2034. "source": "hg3535"
  2035. }
  2036. }
  2037. else:
  2038. if area_name == "网球":
  2039. payload = {
  2040. "game_code": "wq",
  2041. "title": "league",
  2042. "data": {
  2043. "name_chinese": name_chinese,
  2044. "kind": "1",
  2045. "match_mode": "1",
  2046. "if_stop": "0",
  2047. "belong": " ",
  2048. "last_time": "2019-01-01 00:00:00",
  2049. "lg_id": st_league,
  2050. "source": "hg3535"
  2051. }
  2052. }
  2053. else:
  2054. payload = {
  2055. "game_code": "wq",
  2056. "title": "league",
  2057. "data": {
  2058. "name_chinese": name_chinese,
  2059. "kind": "1",
  2060. "match_mode": "1",
  2061. "if_stop": "0",
  2062. "belong": 'area_name',
  2063. "last_time": "2019-01-01 00:00:00",
  2064. "lg_id": st_league,
  2065. "source": "hg3535"
  2066. }}
  2067. data = {"data": json.dumps(payload)}
  2068. resp = requests.post(url, data=data)
  2069. print(resp.text)
  2070. elif ball == "棒球":
  2071. if area_name in area_dict:
  2072. payload = {
  2073. "game_code": "bq",
  2074. "title": "league",
  2075. "data": {
  2076. "name_chinese": name_chinese,
  2077. "kind": "1",
  2078. "match_mode": "1",
  2079. "if_stop": "0",
  2080. "belong": area_name,
  2081. "last_time": "2019-01-01 00:00:00",
  2082. "lg_id": st_league,
  2083. "source": "hg3535"
  2084. }
  2085. }
  2086. else:
  2087. if area_name == "棒球":
  2088. payload = {
  2089. "game_code": "bq",
  2090. "title": "league",
  2091. "data": {
  2092. "name_chinese": name_chinese,
  2093. "kind": "1",
  2094. "match_mode": "1",
  2095. "if_stop": "0",
  2096. "belong": " ",
  2097. "last_time": "2019-01-01 00:00:00",
  2098. "lg_id": st_league,
  2099. "source": "hg3535"
  2100. }
  2101. }
  2102. else:
  2103. payload = {
  2104. "game_code": "bq",
  2105. "title": "league",
  2106. "data": {
  2107. "name_chinese": name_chinese,
  2108. "kind": "1",
  2109. "match_mode": "1",
  2110. "if_stop": "0",
  2111. "belong": 'area_name',
  2112. "last_time": "2019-01-01 00:00:00",
  2113. "lg_id": st_league,
  2114. "source": "hg3535"
  2115. }}
  2116. data = {"data": json.dumps(payload)}
  2117. resp = requests.post(url, data=data)
  2118. print(resp.text)
  2119. return item
  2120. # ---------------------------------------------------------滚球---------------------------------------------------------
  2121. # 滚球足球 插入
  2122. class Roll_Zuqiupipeline(object):
  2123. def open_spider(self, spider):
  2124. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  2125. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  2126. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2127. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2128. def process_item(self, item, spider):
  2129. logger = logging.getLogger(__name__)
  2130. # 比赛日期
  2131. try:
  2132. data_game = item['data_game'].split("/")
  2133. month = str(data_game[1].strip())
  2134. day = str(data_game[0])
  2135. except Exception as e:
  2136. logger.warning(e)
  2137. data_game = item['data_game'].split(" ")
  2138. months = str(data_game[1].strip())
  2139. 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'}
  2140. month = month_dict[months]
  2141. day = str(data_game[0])
  2142. # 比赛时间
  2143. time_game = str(item['time_game'])
  2144. print(time_game)
  2145. # 比赛时间,时间戳
  2146. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  2147. r_ctime = "2019" + "-" + month + "-" + day
  2148. # 现在时间,时间戳
  2149. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2150. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  2151. # 比赛id
  2152. competition_id = item['game_id']
  2153. # 联赛id
  2154. league_id = item['league_id']
  2155. # 联赛name
  2156. league_name = item['league_name']
  2157. # 主队
  2158. team_home = item['team_home']
  2159. # 客队
  2160. team_guest = item['team_guest']
  2161. # number
  2162. number = item['number']
  2163. score_home = item['score_home']
  2164. score_guest = item['score_guest']
  2165. half_way = item['half_way']
  2166. pt = item['pt']
  2167. match_score = "{}:{}".format(score_home,score_guest)
  2168. corner_ball = item['corner_ball']
  2169. p_code, p_id = get_pcode(corner_ball, 'concede_size')
  2170. # 构建唯一索引
  2171. half_size_guest = item["half_size_guest"]
  2172. half_size_guest_rule = item["half_size_guest_rule"]
  2173. half_size_home = item["half_size_home"]
  2174. half_size_home_rule = item["half_size_home_rule"]
  2175. try:
  2176. size_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  2177. '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}
  2178. # 让球 数据插入数据库
  2179. zqone_intodbs(data1=half_size_home, data2=size_data, data3="half_size_home", data4=half_size_home_rule,
  2180. cursor=self.cursor, redis_db=self.redis_db)
  2181. # 让球 数据插入数据库
  2182. zqone_intodbs(data1=half_size_guest, data2=size_data, data3="half_size_guest", data4=half_size_guest_rule,
  2183. cursor=self.cursor, redis_db=self.redis_db)
  2184. # 全场场大小
  2185. size_guest = item["size_guest"]
  2186. size_guest_rule = item["size_guest_rule"]
  2187. size_home = item["size_home"]
  2188. size_home_rule = item["size_home_rule"]
  2189. zqone_intodbs(data1=size_guest, data2=size_data, data3="size_guest", data4=size_guest_rule,
  2190. cursor=self.cursor, redis_db=self.redis_db)
  2191. zqone_intodbs(data1=size_home, data2=size_data, data3="size_home", data4=size_home_rule,
  2192. cursor=self.cursor, redis_db=self.redis_db)
  2193. # 上半场大小
  2194. half_concede_home_rule = item["half_concede_home_rule"]
  2195. half_concede_home = item["half_concede_home"]
  2196. half_concede_guest_rule = item["half_concede_guest_rule"]
  2197. half_concede_guest = item["half_concede_guest"]
  2198. # 上半场让球
  2199. zqone_intodbs(data1=half_concede_home, data2=size_data, data3="half_concede_home", data4=half_concede_home_rule,
  2200. cursor=self.cursor, redis_db=self.redis_db)
  2201. 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)
  2202. concede_guest = item["concede_guest"]
  2203. concede_guest_rule = item["concede_guest_rule"]
  2204. concede_home = item["concede_home"]
  2205. concede_home_rule = item["concede_home_rule"]
  2206. # 全场让球
  2207. zqone_intodbs(data1=concede_guest, data2=size_data, data3="concede_guest", data4=concede_guest_rule,
  2208. cursor=self.cursor, redis_db=self.redis_db)
  2209. zqone_intodbs(data1=concede_home, data2=size_data, data3="concede_home", data4=concede_home_rule,
  2210. cursor=self.cursor,redis_db=self.redis_db)
  2211. #
  2212. p_code, p_id = get_pcode(corner_ball, 'capot')
  2213. # 独赢-------------------------------------------------------------------------------------------------------------------
  2214. half_capot_home = item["half_capot_home"]
  2215. half_capot_guest = item["half_capot_guest"]
  2216. half_capot_dogfall = item["half_capot_dogfall"]
  2217. capot_home = item["capot_home"]
  2218. capot_guest = item["capot_guest"]
  2219. capot_dogfall = item["capot_dogfall"]
  2220. capot_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  2221. '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}
  2222. # 上半场独赢 主队
  2223. zqtwo_intodbs(data1=half_capot_home, data2=capot_data, data3="half_capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  2224. # 上半场独赢 客队
  2225. zqtwo_intodbs(data1=half_capot_guest, data2=capot_data, data3="half_capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  2226. # 上半场独赢 和
  2227. zqtwo_intodbs(data1=half_capot_dogfall, data2=capot_data, data3="half_capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  2228. #
  2229. # 全场独赢 主队
  2230. zqtwo_intodbs(data1=capot_home, data2=capot_data, data3="capot_home", data4='1', cursor=self.cursor,redis_db=self.redis_db)
  2231. # 全场独赢 客队
  2232. zqtwo_intodbs(data1=capot_guest, data2=capot_data, data3="capot_guest", data4='2', cursor=self.cursor,redis_db=self.redis_db)
  2233. # 全场独赢 和
  2234. zqtwo_intodbs(data1=capot_dogfall, data2=capot_data, data3="capot_dogfall", data4='x', cursor=self.cursor,redis_db=self.redis_db)
  2235. #
  2236. # 入球数单双-------------------------------------------------------------------------------------------------------------
  2237. p_code, p_id = get_pcode(corner_ball, 'two_sides')
  2238. odd_even_odd = item["odd_even_odd"]
  2239. odd_even_even = item["odd_even_even"]
  2240. half_odd_even_odd = item["half_odd_even_odd"]
  2241. half_odd_even_even = item["half_odd_even_even"]
  2242. # 全场入球数 单双
  2243. two_sides_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  2244. 'p_code': p_code,
  2245. '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}
  2246. # 上半场入球数 单双
  2247. zqtwo_intodbs(data1=odd_even_odd, data2=two_sides_data, data3="two_sides_single", data4='单', cursor=self.cursor,redis_db=self.redis_db)
  2248. zqtwo_intodbs(data1=odd_even_even, data2=two_sides_data, data3="two_sides_double", data4='双', cursor=self.cursor,redis_db=self.redis_db)
  2249. # 全场入球数 单双
  2250. zqtwo_intodbs(data1=half_odd_even_odd, data2=two_sides_data, data3="half_two_sides_single", data4='单',
  2251. cursor=self.cursor, redis_db=self.redis_db)
  2252. zqtwo_intodbs(data1=half_odd_even_even, data2=two_sides_data, data3="half_two_sides_double", data4='双',
  2253. cursor=self.cursor, redis_db=self.redis_db)
  2254. # 总入球数 --------------------------------------------------------------------------------------------------------------
  2255. p_code, p_id = get_pcode(corner_ball, 'total_goal')
  2256. total_goals = item['total_goal']
  2257. total_dict = {'total_goal_zero': '0-1', 'total_goal_two': '2-3', 'total_goal_four': '4-6',
  2258. 'total_goal_seven': '7或以上', 'half_total_goal_zero': '0', "half_total_goal_one": '1',
  2259. "half_total_goal_two": '2', "half_total_goal_three": '3或以上'}
  2260. # 全场入球数 单双
  2261. total_goal_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  2262. 'p_code': p_code,
  2263. '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}
  2264. # 上半场入球数 单双
  2265. for key, value in total_goals.items():
  2266. if value:
  2267. zqtwo_intodbs(data1=value, data2=total_goal_data, data3=key, data4=total_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  2268. # 全场半场 --------------------------------------------------------------------------------------------------------------
  2269. half_fulls = item['half_full']
  2270. p_code, p_id = get_pcode(corner_ball, 'half_full')
  2271. full_dict = {"half_full_home_home": "主主", "half_full_home_dogfall": "主和",
  2272. "half_full_home_guest": "主客", "half_full_dogfall_home": "和主",
  2273. "half_full_dogfall_dogfall": "和和", "half_full_dogfall_guest": "和客",
  2274. "half_full_guest_home": "客主", "half_full_guest_dogfall": "客和",
  2275. "half_full_guest_guest": "客客"}
  2276. half_full_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  2277. 'p_code': p_code,
  2278. '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}
  2279. if half_fulls:
  2280. for key, value in half_fulls.items():
  2281. if value:
  2282. zqtwo_intodbs(data1=value, data2=half_full_data, data3=key, data4=full_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  2283. # 波胆------------------------------------------------------------------------------------------------------------------
  2284. bodan_datas = item['bodan_data']
  2285. p_code, p_id = get_pcode(corner_ball, 'bodan')
  2286. bodan_dict = {"bodanhome_one_zero": "1-0", "bodanhome_two_zero": "2-0",
  2287. "bodanhome_two_one": "2-1", "bodanhome_three_zero": "3-0",
  2288. "bodanhome_three_one": "3-1", "bodanhome_three_two": "3-2",
  2289. "bodanhome_four_zero": "4-0", "bodanhome_four_one": "4-1",
  2290. "bodanhome_four_two": "4-2", "bodanhome_four_three": "4-3",
  2291. "bodanguest_one_zero": "0-1", "bodanguest_two_zero": "0-2",
  2292. "bodanguest_two_one": "1-2", "bodanguest_three_zero": "0-3",
  2293. "bodanguest_three_one": "1-3", "bodanguest_three_two": "2-3",
  2294. "bodanguest_four_zero": "0-4", "bodanguest_four_one": "1-4",
  2295. "bodanguest_four_two": "2-4", "bodanguest_four_three": "3-4",
  2296. "bodandogfall_zero_zero": "0-0", "bodandogfall_one_one": "1-1",
  2297. "bodandogfall_two_two": "2-2", "bodandogfall_three_three": "3-3",
  2298. "bodandogfall_four_four": "4-4", "bodanother": "其他",
  2299. "halfbodanhome_one_zero": "1-0", "halfbodanhome_two_zero": "2-0",
  2300. "halfbodanhome_two_one": "2-1", "halfbodanhome_three_zero": "3-0",
  2301. "halfbodanhome_three_one": "3-1", "halfbodanhome_three_two": "3-2",
  2302. "halfbodanguest_one_zero": "0-1", "halfbodanguest_two_zero": "0-2",
  2303. "halfbodanguest_two_one": "1-2", "halfbodanguest_three_zero": "0-3",
  2304. "halfbodanguest_three_one": "1-3", "halfbodanguest_three_two": "2-3",
  2305. "halfbodandogfall_zero_zero": "0-0", "halfbodandogfall_one_one": "1-1",
  2306. "halfbodandogfall_two_two": "2-2", "halfbodandogfall_three_three": "3-3",
  2307. "halfbodanother": "其他"}
  2308. bodan_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  2309. '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}
  2310. if bodan_datas:
  2311. for key, value in bodan_datas.items():
  2312. zqtwo_intodbs(data1=value, data2=bodan_data, data3=key, data4=bodan_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  2313. # 最先进球/最后进球 ------------------------------------------------------------------------------------------------------
  2314. first_last_balls = item['first_last_ball']
  2315. p_code, p_id = get_pcode(corner_ball, 'first_last_ball')
  2316. first_last_dict = {"first_last_ball": "最先进球", "first_ball_home": "最先进球", "first_ball_guest": "最先进球",
  2317. "last_ball_home": "最后进球", "last_ball_guest": "最后进球", "not_ball": "没有进球"}
  2318. first_last_data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id,
  2319. 'p_code': p_code,
  2320. '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}
  2321. if first_last_balls:
  2322. for key, value in first_last_balls.items():
  2323. # 构建唯一索引
  2324. zqtwo_intodbs(data1=value, data2=first_last_data, data3=key, data4=first_last_dict[key], cursor=self.cursor, redis_db=self.redis_db)
  2325. p_code, p_id = get_pcode(corner_ball, 'temaball')
  2326. full_dicts = item['full_data']
  2327. half_dicts = item['half_data']
  2328. full_dict_rules = item['full_data_rule']
  2329. half_dict_rules = item['half_data_rule']
  2330. data = {'league_id': league_id, 'game_id': competition_id, 'utime': utime, 'p_id': p_id, 'p_code': p_code,
  2331. '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}
  2332. if full_dicts:
  2333. for key, value in full_dicts.items():
  2334. zqtwo_intodbs(data1=value, data2=data, data3=key, data4=full_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  2335. if half_dicts:
  2336. for key, value in half_dicts.items():
  2337. zqtwo_intodbs(data1=value, data2=data, data3=key, data4=half_dict_rules[key], cursor=self.cursor, redis_db=self.redis_db)
  2338. #这日期在这里有问题 先行注释
  2339. # match_date, match_time, time3 = news_time(ctime)
  2340. # n_time = out_time(time3, 1.5)
  2341. # 插入st_zq_competition表
  2342. # if pt == 0:
  2343. data_competition = {'team_home': team_home, 'team_guest': team_guest, 'league_id': league_id,
  2344. 'game_id': competition_id,
  2345. 'match_date': r_ctime, 'match_time': '00:00:00', 'utime': utime, 'number': number,
  2346. 'source': "hg3535", "expire_time": expire_time, 'is_rollball': 1, "us_time":'2019-01-01 00:00:00','time_game':time_game}
  2347. zqone_competitions(data=data_competition, cursor=self.cursor)
  2348. except Exception as e:
  2349. logger.warning(e)
  2350. return item
  2351. def close_spider(self, spider):
  2352. # self.conn.close()
  2353. self.conn.commit()
  2354. self.conn.close()
  2355. # 滚球篮球 插入
  2356. class Roll_Lanqiupipeline(object):
  2357. def open_spider(self, spider):
  2358. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  2359. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  2360. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2361. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2362. def process_item(self, item, spider):
  2363. # 使用twisted将mysql插入变成异步执行
  2364. logger = logging.getLogger(__name__)
  2365. # 联赛id
  2366. league_id = item['league_id']
  2367. # 联赛名
  2368. league_name = item['league_name']
  2369. # result = item['result']
  2370. # 比赛id
  2371. game_id = item['game_id']
  2372. # 球队1 #home_team
  2373. team_home = item['team_home']
  2374. # 球队2 # guest_team
  2375. team_guest = item['team_guest']
  2376. # 数量(97>)
  2377. number = item['number']
  2378. # 比赛状态
  2379. zhuangtai = item['zhuangtai']
  2380. # 日期
  2381. # data_game = item['data_game']
  2382. try:
  2383. data_game = item['data_game'].split("/")
  2384. month = str(data_game[1].strip())
  2385. day = str(data_game[0])
  2386. except Exception as e:
  2387. logger.warning(e)
  2388. data_game = item['data_game'].split(" ")
  2389. months = str(data_game[1].strip())
  2390. 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'}
  2391. month = month_dict[months]
  2392. day = str(data_game[0])
  2393. # 比赛时间
  2394. time_game = str(item['time_game'])
  2395. # 比赛时间,时间戳
  2396. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  2397. r_ctime = "2019" + "-" + month + "-" + day
  2398. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))#过期时间
  2399. # 现在时间,时间戳
  2400. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2401. # 队1分数
  2402. score_home = item['score_home']
  2403. # 队2分数
  2404. score_guest = item['score_guest']
  2405. # 第几节
  2406. jijie = item['jijie']
  2407. # 球队得分
  2408. qiudui = item['qiudui']
  2409. pt = item['pt']
  2410. match_score = "{}:{}".format(score_home,score_guest)
  2411. concedes_dict = item['concede']
  2412. concedes_dict_rule = item['concede_rule']
  2413. odd_evens_dict = item['odd_even']
  2414. odd_evens_dict_rule = item['odd_even_rule']
  2415. total_sizes_dict = item['total_size']
  2416. total_sizes_dict_rule = item['total_size_rule']
  2417. last_numbers_dict = item['last_number']
  2418. capots_dict = item['capot']
  2419. team_scores_dict = item['team_score']
  2420. team_scores_dict_rule = item['team_score_rule']
  2421. try:
  2422. if concedes_dict:
  2423. for key, value in concedes_dict.items():
  2424. if value:
  2425. for x, y in enumerate(value):
  2426. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=1)
  2427. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=1,odd=y)
  2428. 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;"
  2429. 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))
  2430. # res = cursor.fetchone()
  2431. # 更新主队st_zq_odds_record表
  2432. 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);"
  2433. 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))
  2434. #插入结果表
  2435. 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;"
  2436. 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))
  2437. #插入记录表
  2438. 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);"
  2439. 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))
  2440. if odd_evens_dict:
  2441. for key, value in odd_evens_dict.items():
  2442. if value:
  2443. for x, y in enumerate(value):
  2444. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=2)
  2445. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=2,odd=y)
  2446. 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;"
  2447. 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))
  2448. # 更新主队st_zq_odds_record表
  2449. 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);"
  2450. 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))
  2451. if total_sizes_dict:
  2452. for key, value in total_sizes_dict.items():
  2453. if value:
  2454. for x, y in enumerate(value):
  2455. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=3)
  2456. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=3,odd=y)
  2457. 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;"
  2458. 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))
  2459. # 更新主队st_zq_odds_record表
  2460. 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);"
  2461. 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))
  2462. last_home = last_numbers_dict['last_home']
  2463. if last_home:
  2464. for key, value in last_home.items():
  2465. if value:
  2466. new_hash = hash_func(match_id=game_id, odds_code="last_home" + key, sort=0, p_id=4)
  2467. odds_only = r_func(match_id=game_id, odds_code="last_home", sort=0, p_id=4,odd=value)
  2468. 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;"
  2469. 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))
  2470. # 更新主队st_zq_odds_record表
  2471. 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);"
  2472. self.cursor.execute(sql2, (int(league_id), "last_home", int(game_id), utime, utime,value, 4, "last_number", 0, "hg3535", key,odds_only,1))
  2473. last_guest = last_numbers_dict['last_guest']
  2474. if last_guest:
  2475. for key, value in last_guest.items():
  2476. if value:
  2477. new_hash = hash_func(match_id=game_id, odds_code="last_guest" + key, sort=0, p_id=4)
  2478. odds_only = r_func(match_id=game_id, odds_code="last_guest", sort=0, p_id=4,odd=value)
  2479. 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;"
  2480. 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))
  2481. # 更新主队st_zq_odds_record表
  2482. 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);"
  2483. self.cursor.execute(sql2, (int(league_id), "last_guest", int(game_id), utime, utime,value, 4, "last_number", 0, "hg3535", key,odds_only,1))
  2484. if capots_dict:
  2485. for key, value in capots_dict.items():
  2486. if value:
  2487. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  2488. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5,odd=value)
  2489. 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;"
  2490. 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))
  2491. # 更新主队st_zq_odds_record表
  2492. 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);"
  2493. self.cursor.execute(sql2, (int(league_id), key, int(game_id), utime, utime, value, 5, "capot", 0, "hg3535",odds_only,1))
  2494. if team_scores_dict:
  2495. for key, value in team_scores_dict.items():
  2496. if value:
  2497. for x, y in enumerate(value):
  2498. new_hash = hash_func(match_id=game_id, odds_code=key, sort=x, p_id=6)
  2499. odds_only = r_func(match_id=game_id, odds_code=key, sort=x, p_id=6,odd=y)
  2500. 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;"
  2501. 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))
  2502. # 更新主队st_zq_odds_record表
  2503. 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);"
  2504. 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))
  2505. # 插入赛事表
  2506. 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;"
  2507. print(Competition_sql)
  2508. 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))
  2509. except Exception as e:
  2510. logger.warning(e)
  2511. return item
  2512. def close_spider(self, spider):
  2513. self.conn.commit()
  2514. self.conn.close()
  2515. # 滚球网球 插入
  2516. class Roll_Wangqiupipeline(object):
  2517. def open_spider(self, spider):
  2518. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  2519. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  2520. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2521. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2522. def process_item(self, item, spider):
  2523. logger = logging.getLogger(__name__)
  2524. # 联赛id
  2525. league_id = item['league_id']
  2526. # 联赛名
  2527. league_name = item['league_name']
  2528. # result = item['result']
  2529. # 比赛id
  2530. game_id = item['game_id']
  2531. # 球队1
  2532. team_home = item['team_home']
  2533. # 球队2
  2534. team_guest = item['team_guest']
  2535. # 数量(97>)
  2536. number = item['number']
  2537. # 比赛状态
  2538. zhuangtai = item['zhuangtai']
  2539. # 日期
  2540. # data_game = item['data_game']
  2541. try:
  2542. data_game = item['data_game'].split("/")
  2543. month = str(data_game[1].strip())
  2544. day = str(data_game[0])
  2545. except Exception as e:
  2546. logger.warning(e)
  2547. data_game = item['data_game'].split(" ")
  2548. months = str(data_game[1].strip())
  2549. 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'}
  2550. month = month_dict[months]
  2551. day = str(data_game[0])
  2552. # 比赛时间
  2553. time_game = str(item['time_game'])
  2554. # 比赛时间,时间戳
  2555. ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00"
  2556. r_ctime = "2019" + "-" + month + "-" + day
  2557. # 现在时间,时间戳
  2558. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2559. # 队1分数
  2560. score_home = item['score_home']
  2561. # 队2分数
  2562. score_guest = item['score_guest']
  2563. # 第几节
  2564. jijie = item['jijie']
  2565. # 球队得分
  2566. qiudui = item['qiudui']
  2567. pt = item['pt']
  2568. #取不到 暂时注掉
  2569. match_date, match_time = new_times(ctime)
  2570. # 让盘
  2571. concedes_dict = item['concedes_dict']
  2572. concedes_dict_rule = item['concedes_dict_rule']
  2573. # 冠军 独赢
  2574. kemps_dict = item['kemps_dict']
  2575. # 让局
  2576. bureaus_dict = item['bureaus_dict']
  2577. bureaus_dict_rule = item['bureaus_dict_rule']
  2578. # 总局数大小
  2579. total_number_dict = item['total_number_dict']
  2580. total_number_dict_rule = item['total_number_dict_rule']
  2581. # 总局数单双
  2582. odd_evens_dict = item['odd_evens_dict']
  2583. odd_evens_dict_rule = item['odd_evens_dict_rule']
  2584. if concedes_dict:
  2585. for key, value in concedes_dict.items():
  2586. if value:
  2587. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  2588. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1,odd=value)
  2589. 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;"
  2590. self.cursor.execute(sql1, (
  2591. int(league_id), key, int(game_id), utime, utime,value, 1, "dishes_home", 0, "hg3535", new_hash,
  2592. concedes_dict_rule[key],odds_only,1,utime, value,odds_only))
  2593. # 更新主队st_zq_odds_record表
  2594. 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);"
  2595. self.cursor.execute(sql2, (
  2596. int(league_id), key, int(game_id), utime, utime, value, 1, "dishes_home", 0, "hg3535",
  2597. concedes_dict_rule[key],odds_only,1))
  2598. #插入结果表
  2599. 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;"
  2600. 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))
  2601. # 插入记录表
  2602. 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);"
  2603. 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'))
  2604. if kemps_dict:
  2605. for key, value in kemps_dict.items():
  2606. if value:
  2607. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  2608. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  2609. 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;"
  2610. self.cursor.execute(sql1, (
  2611. int(league_id), key, int(game_id), utime, utime, value, 2, "kemp", 0, "hg3535", new_hash,odds_only,1,utime, value,odds_only))
  2612. # 更新主队st_zq_odds_record表
  2613. 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);"
  2614. self.cursor.execute(sql2, (
  2615. int(league_id), key, int(game_id), utime, utime,value, 2, "kemp", 0, "hg3535",odds_only,1))
  2616. 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;"
  2617. 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))
  2618. if bureaus_dict:
  2619. for key, value in bureaus_dict.items():
  2620. if value:
  2621. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  2622. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3,odd=value)
  2623. 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;"
  2624. self.cursor.execute(sql1, (
  2625. int(league_id), key, int(game_id), utime, utime, value, 3, "bureau", 0, "hg3535", new_hash,
  2626. bureaus_dict_rule[key],odds_only,1,utime, value,odds_only))
  2627. # 更新主队st_zq_odds_record表
  2628. 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);"
  2629. self.cursor.execute(sql2, (
  2630. int(league_id), key, int(game_id), utime, utime,value, 3, "bureau", 0, "hg3535",
  2631. bureaus_dict_rule[key],odds_only,1))
  2632. 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;"
  2633. 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))
  2634. if total_number_dict:
  2635. for key, value in total_number_dict.items():
  2636. if value:
  2637. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=4)
  2638. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=4,odd=value)
  2639. 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;"
  2640. self.cursor.execute(sql1, (
  2641. int(league_id), key, int(game_id), utime, utime,value, 4, "total_number", 0, "hg3535", new_hash,
  2642. total_number_dict_rule[key],odds_only,1,utime, value,odds_only))
  2643. # 更新主队st_zq_odds_record表
  2644. 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);"
  2645. self.cursor.execute(sql2, (
  2646. int(league_id), key, int(game_id), utime, utime, value, 4, "total_number", 0, "hg3535",
  2647. total_number_dict_rule[key],odds_only,1))
  2648. 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;"
  2649. 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))
  2650. if odd_evens_dict:
  2651. for key, value in odd_evens_dict.items():
  2652. if value:
  2653. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=5)
  2654. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=5,odd=value)
  2655. 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;"
  2656. self.cursor.execute(sql1, (
  2657. int(league_id), key, int(game_id), utime, utime,value, 5, "two_game_sides", 0, "hg3535", new_hash,
  2658. odd_evens_dict_rule[key],odds_only,1,utime, value,odds_only))
  2659. # 更新主队st_zq_odds_record表
  2660. 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);"
  2661. self.cursor.execute(sql2, (
  2662. int(league_id), key, int(game_id), utime, utime, value, 5, "two_game_sides", 0, "hg3535",odd_evens_dict_rule[key],odds_only,1))
  2663. 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;"
  2664. 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))
  2665. # 插入赛事表
  2666. 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;"
  2667. 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))
  2668. return item
  2669. def close_spider(self, spider):
  2670. self.conn.commit()
  2671. self.conn.close()
  2672. # 滚球棒球 插入
  2673. class Roll_Banqiupipeline(object):
  2674. def open_spider(self, spider):
  2675. self.pool = redis.ConnectionPool(host=settings["R_HOST"], port=settings["R_POST"], password=settings["R_PASSWORD"])
  2676. self.redis_db = redis.StrictRedis(connection_pool=self.pool)
  2677. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2678. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2679. def process_item(self, item, spider):
  2680. # 使用twisted将mysql插入变成异步执行
  2681. # 联赛id
  2682. league_id = item['league_id']
  2683. # 联赛名
  2684. league_name = item['league_name']
  2685. # result = item['result']
  2686. # 比赛id
  2687. game_id = item['game_id']
  2688. # 球队1
  2689. team_home = item['team_home']
  2690. # 球队2
  2691. team_guest = item['team_guest']
  2692. # 数量(97>)
  2693. number = item['number']
  2694. #新增 match_score_dict
  2695. match_score_dict = item['match_score_dict']
  2696. # 比赛状态
  2697. zhuangtai = item['zhuangtai']
  2698. # 日期
  2699. # data_game = item['data_game']
  2700. data_game = item['data_game'].split("/")
  2701. month = str(data_game[1].strip())
  2702. day = str(data_game[0])
  2703. # 比赛时间
  2704. # time_game = str(item['time_game'])
  2705. # 比赛时间,时间戳
  2706. # ctime = "2019" + "-" + month + "-" + day + "" + time_game + ":00".strip()
  2707. r_ctime = "2019" + "-" + month + "-" + day
  2708. # 现在时间,时间戳
  2709. utime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2710. expire_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time() + 60))
  2711. # 队1分数
  2712. score_home = item['score_home']
  2713. # 队2分数
  2714. score_guest = item['score_guest']
  2715. # 第几节
  2716. jijie = item['jijie']
  2717. # 球队得分
  2718. qiudui = item['qiudui']
  2719. pt = item['pt']
  2720. #滚球这个位置获取不到这些字段
  2721. # match_date, match_time = new_time(ctime)
  2722. # 让球
  2723. concedes_dict = item['concedes_dict']
  2724. concedes_dict_rule = item['concedes_dict_rule']
  2725. # 独赢
  2726. capots_dict = item['capots_dict']
  2727. # 总得分大小
  2728. total_size_dict = item['total_size_dict']
  2729. total_size_dict_rule = item['total_size_dict_rule']
  2730. # 总得分单双
  2731. odd_evens_dict = item['odd_evens_dict']
  2732. odd_evens_dict_rule = item['odd_evens_dict_rule']
  2733. # 让球
  2734. concede = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 1, 'p_code': "concede",
  2735. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  2736. # bqone_intodb(data1=concedes_dict, data2=concede, data3=concedes_dict_rule, cursor=self.cursor)
  2737. # 总得分:大/小
  2738. total_size = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 2, 'p_code': "total_size",
  2739. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  2740. # bqone_intodb(data1=total_size_dict, data2=total_size, data3=total_size_dict_rule, cursor=self.cursor)
  2741. odd_even = {'league_id': league_id, 'game_id': game_id, 'utime': utime, 'p_id': 3, 'p_code': "two_sides",
  2742. 'source': "hg3535", 'expire_time': expire_time, 'sort': 0}
  2743. # bqone_intodb(data1=odd_evens_dict, data2=odd_even, data3=odd_evens_dict_rule, cursor=self.cursor)
  2744. # 赛事失效时间
  2745. # n_time = out_time(ctime, 3.5)
  2746. # 插入独赢
  2747. if capots_dict:
  2748. for key, value in capots_dict.items():
  2749. if value:
  2750. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  2751. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  2752. 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;"
  2753. self.cursor.execute(sql1, (
  2754. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", new_hash,
  2755. odds_only, expire_time,1,utime, value, odds_only,expire_time))
  2756. # 更新主队st_zq_odds_record表
  2757. 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);"
  2758. self.cursor.execute(sql2, (
  2759. int(league_id), key, int(game_id), utime, utime, value, 4, "capot", 0, "hg3535", odds_only,1))
  2760. res = json.dumps(match_score_dict)
  2761. 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;"
  2762. 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))
  2763. # 插入赛事表
  2764. 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;"
  2765. self.cursor.execute(Competition_sql, (team_home, team_guest, league_id, 1,game_id,utime, utime, number,"hg3535",1,1,number,1,utime))
  2766. #让球
  2767. if concedes_dict:
  2768. for key, value in concedes_dict.items():
  2769. if value:
  2770. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=1)
  2771. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=1,odd=value)
  2772. 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;"
  2773. self.cursor.execute(sql1, (
  2774. int(league_id), key, int(game_id), utime, utime,value, 1, "concede", 0, "hg3535", new_hash,
  2775. concedes_dict_rule[key],odds_only,1,utime, value,odds_only))
  2776. # 更新主队st_zq_odds_record表
  2777. 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);"
  2778. self.cursor.execute(sql2, (
  2779. int(league_id), key, int(game_id), utime, utime, value, 1, "concede", 0, "hg3535",
  2780. concedes_dict_rule[key],odds_only,1))
  2781. #插入结果表
  2782. res = json.dumps(match_score_dict)
  2783. # r = pymysql.escape_string(res)
  2784. 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;"
  2785. 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))
  2786. # 插入记录表
  2787. 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);"
  2788. 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))
  2789. if total_size_dict:
  2790. for key, value in total_size_dict.items():
  2791. if value:
  2792. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=2)
  2793. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=2,odd=value)
  2794. 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;"
  2795. self.cursor.execute(sql1, (
  2796. int(league_id), key, int(game_id), utime, utime,value, 2, "total_size", 0, "hg3535", new_hash,
  2797. total_size_dict_rule[key],odds_only,1,utime, value,odds_only))
  2798. # 更新主队st_zq_odds_record表
  2799. 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);"
  2800. self.cursor.execute(sql2, (
  2801. int(league_id), key, int(game_id), utime, utime, value, 2, "total_size", 0, "hg3535",
  2802. total_size_dict_rule[key],odds_only,1))
  2803. #插入结果表
  2804. res = json.dumps(match_score_dict)
  2805. # r = pymysql.escape_string(res)
  2806. 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;"
  2807. 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))
  2808. # 插入记录表
  2809. 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);"
  2810. 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))
  2811. if odd_evens_dict:
  2812. for key, value in odd_evens_dict.items():
  2813. if value:
  2814. new_hash = hash_func(match_id=game_id, odds_code=key, sort=0, p_id=3)
  2815. odds_only = r_func(match_id=game_id, odds_code=key, sort=0, p_id=3,odd=value)
  2816. 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;"
  2817. self.cursor.execute(sql1, (
  2818. int(league_id), key, int(game_id), utime, utime,value, 3, "two_sides", 0, "hg3535", new_hash,
  2819. odd_evens_dict_rule[key],odds_only,1,utime, value,odds_only))
  2820. # 更新主队st_zq_odds_record表
  2821. 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);"
  2822. self.cursor.execute(sql2, (
  2823. int(league_id), key, int(game_id), utime, utime, value, 3, "two_sides", 0, "hg3535",
  2824. odd_evens_dict_rule[key],odds_only,1))
  2825. #插入结果表
  2826. res = json.dumps(match_score_dict)
  2827. # r = pymysql.escape_string(res)
  2828. 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;"
  2829. 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))
  2830. # 插入记录表
  2831. 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);"
  2832. 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))
  2833. return item
  2834. def close_spider(self, spider):
  2835. self.conn.commit()
  2836. self.conn.close()
  2837. # 足球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  2838. class Zujieshuqiupipeline(object):
  2839. def open_spider(self, spider):
  2840. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2841. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2842. def process_item(self, item, spider):
  2843. logger = logging.getLogger(__name__)
  2844. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2845. # 赛事id
  2846. match_id = item['id_score'][0]
  2847. # 全场比分
  2848. score = item['id_score'][1]
  2849. status = 0
  2850. game_code = 'zq'
  2851. try:
  2852. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  2853. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  2854. self.cursor.execute("update st_zq_result set status=2 where match_id={}".format(match_id))
  2855. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  2856. self.cursor.execute("update st_zq_competition set status=2 where match_id={}".format(match_id))
  2857. # url = 'http://sports.5gogo.com/Sports/WriteSports/setSports'
  2858. # pass
  2859. # data = {
  2860. # "game_code": "zq",
  2861. # "title": "match_result",
  2862. # "data": {
  2863. # "lg_id": 27686,
  2864. # "home_team": " ",
  2865. # "guest_team": " ",
  2866. # "home_rate": " ",
  2867. # "guest_rate": " ",
  2868. # "home_score": " ",
  2869. # "guest_score": " ",
  2870. # "all_goal": " ",
  2871. # "status": "2",
  2872. # "first_score": " ",
  2873. # "last_score": " ",
  2874. # "match_score": " ",
  2875. # "match_winer": " ",
  2876. # "match_time": " ",
  2877. # "match_process": " ",
  2878. # "tag": "111",
  2879. # "match_id": match_id,
  2880. # "u_home_score": "111",
  2881. # "u_guest_score": "11",
  2882. # "update_time": ctime,
  2883. # "p_code": "111",
  2884. # "source": "hg3535",
  2885. # }
  2886. # }
  2887. # data = json.dumps(data)
  2888. # print(data)
  2889. # r = requests.post(url, data=data)
  2890. # print(r.content)
  2891. except Exception as e:
  2892. logger.warning('足球 赛事结束 结果插入错误')
  2893. logger.warning(e)
  2894. return item
  2895. def close_spider(self, spider):
  2896. self.conn.commit()
  2897. self.conn.close()
  2898. # 篮球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  2899. class Lanjieshuqiupipeline(object):
  2900. def open_spider(self, spider):
  2901. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"], password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2902. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2903. def process_item(self, item, spider):
  2904. logger = logging.getLogger(__name__)
  2905. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2906. # 赛事id
  2907. match_id = item['id_score'][0]
  2908. # 全场比分
  2909. score = item['id_score'][1]
  2910. status = 0
  2911. game_code = 'lq'
  2912. try:
  2913. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  2914. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  2915. self.cursor.execute("update st_lq_result set status=2 where match_id={}".format(match_id))
  2916. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  2917. self.cursor.execute("update st_lq_competition set status=2 where match_id={}".format(match_id))
  2918. except Exception as e:
  2919. logger.warning('篮球 赛事结束 结果插入错误')
  2920. logger.warning(e)
  2921. return item
  2922. def close_spider(self, spider):
  2923. self.conn.commit()
  2924. self.conn.close()
  2925. # 棒球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  2926. class Bangjieshuqiupipeline(object):
  2927. def open_spider(self, spider):
  2928. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  2929. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2930. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2931. def process_item(self, item, spider):
  2932. logger = logging.getLogger(__name__)
  2933. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2934. # 赛事id
  2935. match_id = item['id_score'][0]
  2936. # 全场比分
  2937. score = item['id_score'][1]
  2938. status = 0
  2939. game_code = 'lq'
  2940. try:
  2941. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  2942. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  2943. self.cursor.execute("update st_bq_result set status=2 where match_id={}".format(match_id))
  2944. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  2945. self.cursor.execute("update st_bq_competition set status=2 where match_id={}".format(match_id))
  2946. except Exception as e:
  2947. logger.warning('棒球 赛事结束 结果插入错误')
  2948. logger.warning(e)
  2949. return item
  2950. def close_spider(self, spider):
  2951. self.conn.commit()
  2952. self.conn.close()
  2953. # 网球 赛事结束 结果插入--------------------------------------------------------------------------------------------------
  2954. class Wangjieshuqiupipeline(object):
  2955. def open_spider(self, spider):
  2956. self.conn = psycopg2.connect(host=settings["POST_HOST"], port=settings['POST_PORT'], user=settings["POST_USER"],
  2957. password=settings["POST_PASSWORD"], database=settings["POST_DATABASE"])
  2958. self.cursor = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
  2959. def process_item(self, item, spider):
  2960. logger = logging.getLogger(__name__)
  2961. ctime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  2962. # 赛事id
  2963. match_id = item['id_score'][0]
  2964. # 全场比分
  2965. score = item['id_score'][1]
  2966. status = 0
  2967. game_code = 'lq'
  2968. try:
  2969. sql = "insert into comendnotice(status, game_code, match_id, ctime) values (%s, %s, %s, %s) on conflict(match_id) do update set ctime = %s"
  2970. self.cursor.execute(sql, (status, game_code, match_id, ctime, ctime))
  2971. self.cursor.execute("update st_wq_result set status=2 where match_id={}".format(match_id))
  2972. # self.cursor.execute("update st_zq_result_record set status=2 where match_id={}".format(match_id))
  2973. self.cursor.execute("update st_wq_competition set status=2 where match_id={}".format(match_id))
  2974. except Exception as e:
  2975. logger.warning('网球 赛事结束 结果插入错误')
  2976. logger.warning(e)
  2977. return item
  2978. def close_spider(self, spider):
  2979. self.conn.commit()
  2980. self.conn.close()