浏览代码

更新修改

shawenguan 1 年之前
父节点
当前提交
e9ca96c1b9
共有 4 个文件被更改,包括 194 次插入85 次删除
  1. 57 42
      addons/elife/OfpayGrab.py
  2. 58 42
      addons/elife/OfpayGrab0.py
  3. 4 1
      addons/utils/Utils.py
  4. 75 0
      pac/all.pac

+ 57 - 42
addons/elife/OfpayGrab.py

@@ -8,34 +8,7 @@ from datetime import datetime
 from cachetools import TTLCache
 import pickle
 import multiprocessing
-
-cache = None;
-cache_file_path = 'elife.cache.pickle';
-def init_cache():
-    global cache;
-    if os.path.exists(cache_file_path):
-        # 从文件中加载缓存对象
-        with open(cache_file_path, 'rb') as file:
-            cache = pickle.load(file);
-    else:
-        cache = TTLCache(maxsize=100, ttl=60*60*24);
-        # 将缓存对象持久化到文件
-        with open(cache_file_path, 'wb') as file:
-            pickle.dump(cache, file);
-    return cache;
-
-def save_cache():
-    global cache;
-    with open(cache_file_path, 'wb') as file:
-        pickle.dump(cache, file);
-
-def set_cache(key, value):
-    global cache;
-    cache[key] = value;
-
-def get_cache(key):
-    global cache;
-    return cache.get(key);
+from multiprocessing import Manager, Pool
 
 def read_csv(filename='results.csv'):
     data = []
@@ -59,6 +32,37 @@ def read_json(filename='results.json'):
     # print(data);
     return data;
 
+g_shared_cache = None;
+class SharedCache:
+    cache_file_path = 'elife.cache.pickle';
+    def __init__(self):
+        self.init();
+
+    def init(self):
+        cache = None;
+        cache_file_path = SharedCache.cache_file_path;
+        if os.path.exists(cache_file_path):
+            # 从文件中加载缓存对象
+            with open(cache_file_path, 'rb') as file:
+                cache = pickle.load(file);
+        else:
+            cache = TTLCache(maxsize=100, ttl=60*60*24);
+            # 将缓存对象持久化到文件
+            with open(cache_file_path, 'wb') as file:
+                pickle.dump(cache, file);
+        self.cache = cache;
+
+    def save(self):
+        cache_file_path = SharedCache.cache_file_path;
+        with open(cache_file_path, 'wb') as file:
+            pickle.dump(self.cache, file);
+
+    def set(self, key, value):
+        self.cache[key] = value;
+
+    def get(self, key):
+        return self.cache.get(key);
+
 class OfpayGrabber:
     CheckBuyRepeatEnable = True;
     FastModeEnable = True;
@@ -96,7 +100,6 @@ class OfpayGrabber:
             'Referer': 'https://market-web.ofpay.com/h5/union/standard/interactiveIGoChoose/index',
             'Cookie': cookies_str,
         };
-        # print(self.headers);
 
     def start(self):
         ret_code = self.check_refresh_token();
@@ -126,7 +129,7 @@ class OfpayGrabber:
             all_ret_list.extend(buy_ret_list);
             sort_num += 1;
         if len(all_ret_list):
-            save_cache();
+            g_shared_cache.save();
         return all_ret_list;
 
     def check_refresh_token(self, force_refresh=False):
@@ -250,7 +253,7 @@ class OfpayGrabber:
         now_string = datetime.now().strftime('%Y-%m-%d %H:%M:%S');
 
         if OfpayGrabber.CheckBuyRepeatEnable:
-            last_buy_succ_date = get_cache(check_buy_repeat_key);
+            last_buy_succ_date = g_shared_cache.get(check_buy_repeat_key);
             if now_string == last_buy_succ_date:
                 print(f"商品[{item_name}]今日已抢购成功过,跳过~~");
                 return one_ret;
@@ -263,14 +266,14 @@ class OfpayGrabber:
                             one_ret = self.item_buy_fast(activity_id, sub_activity_id, sub_login_type, award_data);
                             if one_ret:
                                 # 抢购成功
-                                set_cache(check_buy_repeat_key, now_string);
+                                g_shared_cache.set(check_buy_repeat_key, now_string);
                         else:
                             print("库存不足,跳过~");
                     else:
                         one_ret = self.item_buy_fast(activity_id, sub_activity_id, sub_login_type, award_data);
                         if one_ret:
                             # 抢购成功
-                            set_cache(check_buy_repeat_key, now_string);
+                            g_shared_cache.set(check_buy_repeat_key, now_string);
                     break;
         else:
             for award_data in award_list:
@@ -280,7 +283,7 @@ class OfpayGrabber:
                         one_ret = self.item_buy_normal(activity_id, sub_activity_id, sub_login_type, award_data);
                         if one_ret:
                             # 抢购成功
-                            set_cache(check_buy_repeat_key, now_string);
+                            g_shared_cache.set(check_buy_repeat_key, now_string);
                     else:
                         print("库存不足,跳过~");
                     break;
@@ -306,6 +309,7 @@ class OfpayGrabber:
     def get_pay_info(self, activity_id, award_id, goods_id, invitation_code, game_account, event_visitor_id):
         url = f'https://{self.host}/h5/union/api/draw/interactiveIGoChoose/{activity_id}?awardId={award_id}&goodsId={goods_id}&invitationCode={invitation_code}&gameAccount={game_account}&eventVisitorId={event_visitor_id}';
         print("请求商品预支付数据");
+        print(url);
         response = self.get_request(url);
         if response.status_code == 200:
             try:
@@ -487,9 +491,13 @@ class OfpayGrabber:
         response = requests.post(url, data=data, headers=self.headers, cookies=self.cookies);
         return response;
 
-def award_grab_worker(account_info, activities_data):
-    global cache;
-    init_cache();
+def init_data_pre_excute():
+    print('init_shared_data');
+
+def thread_worker_func(index, shared_namespace, account_info, activities_data):
+    print(f'thread_worker_func#{index}');
+    global g_shared_cache;
+    g_shared_cache = shared_namespace.g_shared_cache;
     print('########账号[%s]开始抢券工作########' % account_info['account']);
     grabber = OfpayGrabber(account_info, activities_data);
     results = grabber.start();
@@ -497,18 +505,27 @@ def award_grab_worker(account_info, activities_data):
     return f"账号[{account_info['account']}]任务完成";
 
 def main():
+    global g_shared_cache;
+    g_shared_cache = SharedCache();
+    g_shared_cache.set('test', 10);
     cpu_threads = multiprocessing.cpu_count();
     print(f"CPU支持 {cpu_threads} 个线程/核心");
     activities_data = read_json('elife_activities_data.json');
     accout_data = read_csv('elife_accout_data.csv');
-    cpu_threads = 0;
     if cpu_threads > 1:
         # 创建进程池
-        pool = multiprocessing.Pool(cpu_threads);
+        manager = Manager();
+        # 创建共享命名空间
+        shared_namespace = manager.Namespace();
+        shared_namespace.g_shared_cache = g_shared_cache;
+
+        pool = multiprocessing.Pool(cpu_threads, initializer=init_data_pre_excute);
         results = [];
+        index = 0;
         for item in accout_data:
-            result = pool.apply_async(award_grab_worker,(item, activities_data,));
+            result = pool.apply_async(thread_worker_func,(index, shared_namespace, item, activities_data,));
             results.append(result);
+            index += 1;
         # 关闭进程池,不再接受新的任务
         pool.close();
         # 等待所有任务完成
@@ -516,8 +533,6 @@ def main():
         # for result in results:
         #     print(result.get());
     else:
-        global cache;
-        init_cache();
         for item in accout_data:
             print('########账号[%s]开始抢券工作########' % item['account']);
             grabber = OfpayGrabber(item, activities_data);

+ 58 - 42
addons/elife/OfpayGrab0.py

@@ -8,34 +8,7 @@ from datetime import datetime
 from cachetools import TTLCache
 import pickle
 import multiprocessing
-
-cache = None;
-cache_file_path = 'elife.cache.pickle';
-def init_cache():
-    global cache;
-    if os.path.exists(cache_file_path):
-        # 从文件中加载缓存对象
-        with open(cache_file_path, 'rb') as file:
-            cache = pickle.load(file);
-    else:
-        cache = TTLCache(maxsize=100, ttl=60*60*24);
-        # 将缓存对象持久化到文件
-        with open(cache_file_path, 'wb') as file:
-            pickle.dump(cache, file);
-    return cache;
-
-def save_cache():
-    global cache;
-    with open(cache_file_path, 'wb') as file:
-        pickle.dump(cache, file);
-
-def set_cache(key, value):
-    global cache;
-    cache[key] = value;
-
-def get_cache(key):
-    global cache;
-    return cache.get(key);
+from multiprocessing import Manager, Pool
 
 def read_csv(filename='results.csv'):
     data = []
@@ -59,6 +32,37 @@ def read_json(filename='results.json'):
     # print(data);
     return data;
 
+g_shared_cache = None;
+class SharedCache:
+    cache_file_path = 'elife.cache.pickle';
+    def __init__(self):
+        self.init();
+
+    def init(self):
+        cache = None;
+        cache_file_path = SharedCache.cache_file_path;
+        if os.path.exists(cache_file_path):
+            # 从文件中加载缓存对象
+            with open(cache_file_path, 'rb') as file:
+                cache = pickle.load(file);
+        else:
+            cache = TTLCache(maxsize=100, ttl=60*60*24);
+            # 将缓存对象持久化到文件
+            with open(cache_file_path, 'wb') as file:
+                pickle.dump(cache, file);
+        self.cache = cache;
+
+    def save(self):
+        cache_file_path = SharedCache.cache_file_path;
+        with open(cache_file_path, 'wb') as file:
+            pickle.dump(self.cache, file);
+
+    def set(self, key, value):
+        self.cache[key] = value;
+
+    def get(self, key):
+        return self.cache.get(key);
+
 class OfpayGrabber:
     CheckBuyRepeatEnable = True;
     FastModeEnable = True;
@@ -96,10 +100,10 @@ class OfpayGrabber:
             'Referer': 'https://market-web.ofpay.com/h5/union/standard/interactiveIGoChoose/index',
             'Cookie': cookies_str,
         };
-        # print(self.headers);
 
     def start(self):
         ret_code = self.check_refresh_token();
+        print(f"账号[{self.accout_id}]token数据状态:{ret_code}");
         if ret_code < 0:
             print(f"账号[{self.accout_id}]token已失效");
             return;
@@ -125,7 +129,7 @@ class OfpayGrabber:
             all_ret_list.extend(buy_ret_list);
             sort_num += 1;
         if len(all_ret_list):
-            save_cache();
+            g_shared_cache.save();
         return all_ret_list;
 
     def check_refresh_token(self, force_refresh=False):
@@ -249,7 +253,7 @@ class OfpayGrabber:
         now_string = datetime.now().strftime('%Y-%m-%d %H:%M:%S');
 
         if OfpayGrabber.CheckBuyRepeatEnable:
-            last_buy_succ_date = get_cache(check_buy_repeat_key);
+            last_buy_succ_date = g_shared_cache.get(check_buy_repeat_key);
             if now_string == last_buy_succ_date:
                 print(f"商品[{item_name}]今日已抢购成功过,跳过~~");
                 return one_ret;
@@ -262,14 +266,14 @@ class OfpayGrabber:
                             one_ret = self.item_buy_fast(activity_id, sub_activity_id, sub_login_type, award_data);
                             if one_ret:
                                 # 抢购成功
-                                set_cache(check_buy_repeat_key, now_string);
+                                g_shared_cache.set(check_buy_repeat_key, now_string);
                         else:
                             print("库存不足,跳过~");
                     else:
                         one_ret = self.item_buy_fast(activity_id, sub_activity_id, sub_login_type, award_data);
                         if one_ret:
                             # 抢购成功
-                            set_cache(check_buy_repeat_key, now_string);
+                            g_shared_cache.set(check_buy_repeat_key, now_string);
                     break;
         else:
             for award_data in award_list:
@@ -279,7 +283,7 @@ class OfpayGrabber:
                         one_ret = self.item_buy_normal(activity_id, sub_activity_id, sub_login_type, award_data);
                         if one_ret:
                             # 抢购成功
-                            set_cache(check_buy_repeat_key, now_string);
+                            g_shared_cache.set(check_buy_repeat_key, now_string);
                     else:
                         print("库存不足,跳过~");
                     break;
@@ -305,6 +309,7 @@ class OfpayGrabber:
     def get_pay_info(self, activity_id, award_id, goods_id, invitation_code, game_account, event_visitor_id):
         url = f'https://{self.host}/h5/union/api/draw/interactiveIGoChoose/{activity_id}?awardId={award_id}&goodsId={goods_id}&invitationCode={invitation_code}&gameAccount={game_account}&eventVisitorId={event_visitor_id}';
         print("请求商品预支付数据");
+        print(url);
         response = self.get_request(url);
         if response.status_code == 200:
             try:
@@ -486,9 +491,13 @@ class OfpayGrabber:
         response = requests.post(url, data=data, headers=self.headers, cookies=self.cookies);
         return response;
 
-def award_grab_worker(account_info, activities_data):
-    global cache;
-    init_cache();
+def init_data_pre_excute():
+    print('init_shared_data');
+
+def thread_worker_func(index, shared_namespace, account_info, activities_data):
+    print(f'thread_worker_func#{index}');
+    global g_shared_cache;
+    g_shared_cache = shared_namespace.g_shared_cache;
     print('########账号[%s]开始抢券工作########' % account_info['account']);
     grabber = OfpayGrabber(account_info, activities_data);
     results = grabber.start();
@@ -496,18 +505,27 @@ def award_grab_worker(account_info, activities_data):
     return f"账号[{account_info['account']}]任务完成";
 
 def main():
+    global g_shared_cache;
+    g_shared_cache = SharedCache();
+    g_shared_cache.set('test', 10);
     cpu_threads = multiprocessing.cpu_count();
     print(f"CPU支持 {cpu_threads} 个线程/核心");
     activities_data = read_json('elife_activities_data.json');
     accout_data = read_csv('elife_accout_data.csv');
-    cpu_threads = 0;
     if cpu_threads > 1:
         # 创建进程池
-        pool = multiprocessing.Pool(cpu_threads);
+        manager = Manager();
+        # 创建共享命名空间
+        shared_namespace = manager.Namespace();
+        shared_namespace.g_shared_cache = g_shared_cache;
+
+        pool = multiprocessing.Pool(cpu_threads, initializer=init_data_pre_excute);
         results = [];
+        index = 0;
         for item in accout_data:
-            result = pool.apply_async(award_grab_worker,(item, activities_data,));
+            result = pool.apply_async(thread_worker_func,(index, shared_namespace, item, activities_data,));
             results.append(result);
+            index += 1;
         # 关闭进程池,不再接受新的任务
         pool.close();
         # 等待所有任务完成
@@ -515,8 +533,6 @@ def main():
         # for result in results:
         #     print(result.get());
     else:
-        global cache;
-        init_cache();
         for item in accout_data:
             print('########账号[%s]开始抢券工作########' % item['account']);
             grabber = OfpayGrabber(item, activities_data);

+ 4 - 1
addons/utils/Utils.py

@@ -86,7 +86,10 @@ def parse_set_cookie(cookies_str):
         else:
             a_key = pstr;
         if a_key in attrib_keys:
-            ret_dict[main_key][a_key] = a_value;
+            if main_key in ret_dict:
+                ret_dict[main_key][a_key] = a_value;
+                attribs = ret_dict[main_key]['attribs'];
+                attribs[a_key] = a_value;
         else:
             main_key = a_key;
             attribs = {};

+ 75 - 0
pac/all.pac

@@ -0,0 +1,75 @@
+function FindProxyForURL(url, host) {
+    var host = host.toLowerCase();
+    var url = url.toLowerCase();
+    var http_proxy = 'PROXY 47.106.225.136:50000';
+    var list = [
+        'mitm.it',
+        'yunbusiness.ccb.com',
+        'piaocmb.o2o.cmbchina.com',
+        'icbc.opapp.cn',
+        'piaorush.o2o.cmbchina.com',
+        'piao-json-cdn.o2o.cmbchina.com',
+        'fpwx.o2o.cmbchina.com',
+        'ccpay.cib.com.cn',
+        'psbc.huajifen.com',
+        'gdecard.jiahuaming.com',
+        'mall.abchina.com',
+        'm.ujia007.com',
+        'creditcardapp.bankcomm.com',
+        'mall.creditcard.gzcb.com.cn',
+        'vtravel.link2shops.com',
+        'uop.amexpressnetwork.com',
+        'mapi-seckill.bestpay.com.cn',
+        'piaorush.o2o.cmbchina.com',
+        'piao-json-cdn.o2o.cmbchina.com',
+        'fpwx.o2o.cmbchina.com',
+        'm.client.10010.com',
+        'aaph.nbcb.com.cn',
+        'api.shoppingm.cn',
+        'wzapi.oywanhao.com',
+        'hemi.lianhaikeji.com',
+        'static.lianhaikeji.com',
+        'hz.qyfw168.cn',
+        'appsmall.rtmap.com',
+        'www.mcearnmore.com',
+        'm.mallcoo.cn',
+        'mall.nbcb.com.cn',
+        'delivery.aeonbuy.com',
+        'g.qll-times.com',
+        'api.jifenfu.net',
+        'h5.schengle.com',
+        'bnb.rongdatamall.com',
+        'chuxinget.fpsd.unionpay.com',
+        'marketing.bulk-delivery.com',
+        'wypwfw.xsy365.com.cn',
+        'fw.qyfw168.cn',
+        '94828.activity-35.m.duiba.com.cn',
+        'aaph.nbcb.com.cn',
+        'market-web.ofpay.com',
+        'service.jifenfu.net',
+        'vip.octharbour.com',
+        'crm.scpgroup.com.cn',
+        'mtp.creditcard.ecitic.com',
+        'rushbuy.creditcard.ecitic.com',
+        'mrp.creditcard.hxb.com.cn',
+        'api.m.jd.com',
+        'prodev.m.jd.com',
+        'promotion.waimai.meituan.com',
+        'activitymanual.epaynfc.com',
+        'ebusiness.bzh001.com',
+        'yx.all-pay.cn',
+        'w.all-pay.cn',
+        'icbc.rellux.com.cn',
+        'ilike-cloud-mapi.zmaxfilm.com',
+        'coin.abchina.com',
+        'ldp.creditcard.ecitic.com',
+        'wxapi.m.taopiaopiao.com',
+        'hbp-prod.fenlekeji.com',
+    ];
+    for(var i=0, l=list.length; i<l; i++) {
+        if (dnsDomainIs(host, list[i])) {
+            return http_proxy;
+        }
+    }
+    return 'DIRECT';
+}