pipelines.py 140 KB

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