pipelines.py 172 KB

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