Skip to content

Commit 191fce6

Browse files
author
ilyasProgrammer
committed
ir config
1 parent 3869ffb commit 191fce6

File tree

5 files changed

+75
-51
lines changed

5 files changed

+75
-51
lines changed

gsmarena/__openerp__.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"data/ir_action_server.xml",
2323
"data/product_category.xml",
2424
"data/cron.xml",
25+
"data/ir_config_parameter.xml",
2526
],
2627
"qweb": [
2728
],

gsmarena/data/ir_config_parameter.xml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<openerp>
3+
<data noupdate="1">
4+
<record model="ir.config_parameter" id="gsmarena_url">
5+
<field name="key">gsmarena.url</field>
6+
<field name="value">http://www.gsmarena.com/results.php3</field>
7+
</record>
8+
</data>
9+
</openerp>

gsmarena/gsmarena.py

+28-26
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
_logger = logging.getLogger("# " + __name__)
1414
_logger.setLevel(logging.DEBUG)
1515

16-
gsmarena_url = 'http://www.gsmarena.com/results.php3' # TODO place in ir config parameter
1716
brands = ['Nokia'] # TODO load brands from somewhere
18-
# brands = ['Nokia', 'Apple', 'Acer', 'Huawei'] # TODO load brands from somewhere
17+
# brands = ['Nokia', 'Apple', 'Acer', 'Huawei']
1918

2019

2120
class ProductProduct(models.Model):
@@ -34,21 +33,21 @@ class GsmArena(models.Model):
3433
@api.model
3534
def action_load_from_gsmarena(self):
3635
_logger.info('Loading of gsmarena.com mobiles database started')
37-
res = self.check_brands()
38-
if res == 0:
39-
_logger.error("Please check brands parser errors")
40-
res = self.check_mobiles()
41-
if res == 0:
42-
_logger.error("Please check mobiles parser errors")
36+
all_good = True
37+
while all_good:
38+
all_good = self.check_brands()
39+
all_good = self.check_mobiles()
40+
return
41+
_logger.error("Please check gsmarena mobiles parser errors")
4342

4443
@api.model
4544
def check_mobiles(self):
4645
mobiles_dict = self.get_gsmarena_mobiles()
47-
if mobiles_dict == 0:
48-
return 0
46+
if mobiles_dict is False:
47+
return False
4948
for key, value in mobiles_dict.iteritems():
50-
found = self.env['product.product'].search([('mobile_id', '=', key)])
51-
if len(found) == 0:
49+
found_mobiles = self.env['product.product'].search([('mobile_id', '=', key)])
50+
if len(found_mobiles) == 0:
5251
# create new
5352
categ = self.env['product.category'].search([('name', '=', value['brand'])])
5453
if len(categ) != 1:
@@ -64,10 +63,10 @@ def check_mobiles(self):
6463
'categ_id': categ.id}
6564
new_mobile = self.env['product.product'].create(vals)
6665
_logger.info('New mobile added: %s' % new_mobile.name)
67-
elif len(found) == 1:
68-
# TODO check accordance of fields
69-
pass
70-
return 1
66+
elif len(found_mobiles) == 1:
67+
found_mobiles.update(vals)
68+
_logger.info('Old mobile updated: %s' % found_mobiles.name)
69+
return True
7170

7271
@api.model
7372
def check_brands(self):
@@ -82,11 +81,13 @@ def check_brands(self):
8281
else:
8382
# TODO check accordance
8483
pass
85-
return 1
84+
return True
8685

8786
@api.model
8887
def get_gsmarena_mobiles(self):
89-
ret = {} # {mobile_id:{'name':'', 'photo': ''}}
88+
# returns dict of dict {mobile_id:{'name':'', 'photo': '', ... }}
89+
gsmarena_url = self.env['ir.config_parameter'].sudo().get_param('gsmarena.url')
90+
ret = dict()
9091
simbol = ["&", "+"]
9192
kata = ["_and_", "_plus_"]
9293
values = {'sQuickSearch': 'yes'}
@@ -96,11 +97,11 @@ def get_gsmarena_mobiles(self):
9697
soup = bs(page, 'html.parser')
9798
makers = soup.find_all('div', 'makers')
9899
if len(makers) != 1:
99-
print 'Error' # TODO
100-
return
100+
print 'Error get_gsmarena_mobiles' # TODO
101+
return False
101102
mobiles = makers[0].find_all('li')
102103
for r in mobiles:
103-
mobile = {}
104+
mobile = dict()
104105
mobile['name'] = brand + ' ' + r.find('br').text
105106
mobile['mobile_tech_name'] = make_tech_name(r.find('br').text)
106107
mobile['brand'] = make_tech_name(brand)
@@ -111,15 +112,16 @@ def get_gsmarena_mobiles(self):
111112
if resp.code == 200:
112113
photo = base64.b64encode(resp.read())
113114
mobile['photo'] = photo
114-
mob_id = self.get_mobile_id('http://www.gsmarena.com/' + r.find('a').attrs['href'], mobile)
115-
if mob_id == 0:
116-
return 0
115+
mob_id = self.get_mobile_id('http://www.gsmarena.com/' + r.find('a').attrs['href'])
116+
if mob_id is False:
117+
print 'Cant parse mobile id in get_gsmarena_mobiles' # TODO
118+
return False
117119
mobile['id'] = mob_id
118120
ret[mobile['id']] = mobile
119121
return ret
120122

121123
@api.model
122-
def get_mobile_id(self, url, mobile):
124+
def get_mobile_id(self, url):
123125
# get gsmarena mobile ID that placed in script field
124126
page = get_http_page(url)
125127
soup = bs(page, 'html.parser')
@@ -134,7 +136,7 @@ def get_mobile_id(self, url, mobile):
134136
break
135137
except:
136138
_logger.error("Mobile ID parsing error !")
137-
return 0
139+
return False
138140
return int(mobile_id)
139141

140142

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<openerp>
3+
<data noupdate="1">
4+
<record model="ir.config_parameter" id="unlockbase_url">
5+
<field name="key">unlockbase.url</field>
6+
<field name="value">http://www.unlockbase.com/xml/api/v3</field>
7+
</record>
8+
</data>
9+
<data noupdate="1">
10+
<record model="ir.config_parameter" id="unlockbase_key">
11+
<field name="key">unlockbase.key</field>
12+
<field name="value">(C8C7-4533-06AE-3151)</field>
13+
</record>
14+
</data>
15+
</openerp>

unlockbase/unlockbase.py

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
# -*- coding: utf-8 -*-
22

33
from openerp import api, fields, models
4-
import threading
54
import openerp
65
import logging
76
import urllib
87
import urllib2
98
import xml.etree.ElementTree
10-
import base64
119
import re
1210
import string
13-
from bs4 import BeautifulSoup as bs
14-
from openerp import SUPERUSER_ID
1511

1612

1713
_logger = logging.getLogger("# " + __name__)
1814
_logger.setLevel(logging.DEBUG)
1915

20-
unlockbase_url = 'http://www.unlockbase.com/xml/api/v3' # TODO place in ir config parameter
21-
unlockbase_key = '(C8C7-4533-06AE-3151)' # TODO place in ir config parameter
22-
2316

2417
class ProductProduct(models.Model):
2518
_inherit = 'product.product'
@@ -69,8 +62,8 @@ def action_load_from_unlockbase(self):
6962
_logger.info('Loading of unlockbase.com mobiles database started')
7063
all_good = True
7164
while all_good:
72-
# all_good = self.create_brands_and_mobiles() # create, brand, mobile and mobile tools category
73-
# all_good = self.create_tools() # create tools with bound mobiles to it
65+
all_good = self.create_brands_and_mobiles() # create, brand, mobile and mobile tools category
66+
all_good = self.create_tools() # create tools with bound mobiles to it
7467
all_good = self.create_mobiles_tools() # for each mobile we create available unlock tools
7568
_logger.info('Data from unlockbase.com loaded successfully')
7669
return
@@ -91,7 +84,7 @@ def create_brands_and_mobiles(self):
9184
all_brand_mobiles = [r.mobile_tech_name for r in self.env['product.product'].search([('mobile_brand', '=', brand_name)])]
9285
old_brand = self.env['product.category'].search([('name', '=', brand_name)])
9386
if brand_name not in all_brands_names:
94-
# TEMP
87+
# TODO TEMP
9588
continue
9689
vals = {'brand_id': brand_id, 'name': brand_name, 'parent_id': mobiles_cat.id}
9790
# Create brand
@@ -107,12 +100,12 @@ def create_brands_and_mobiles(self):
107100
mobile_name = make_tech_name(mobile.find('Name').text)
108101
mobile_photo = mobile.find('Photo').text.replace('https', 'http')
109102
if mobile_name not in all_brand_mobiles:
110-
# TEMP
103+
# TODO TEMP
111104
continue
112105
resp = urllib.urlopen(mobile_photo)
113106
photo = None
114-
# if resp.code == 200:
115-
# photo = base64.b64encode(resp.read())
107+
if resp.code == 200:
108+
photo = base64.b64encode(resp.read())
116109
vals = {'unlock_mobile_id': unlock_mobile_id,
117110
'name': brand_name_orig + ' ' + mobile.find('Name').text,
118111
'image': photo,
@@ -179,6 +172,7 @@ def create_mobiles_tools(self):
179172
vals = {'name': mobile.name + ' ' + tool.name,
180173
'unlockbase_tool_ids': [(4, tool.id,)],
181174
'type': 'service',
175+
'list_price': tool.credits,
182176
'image': open(image_path, 'rb').read().encode('base64'),
183177
'categ_id': mobile_tools_cat.id}
184178
if len(found_tools) == 0:
@@ -189,17 +183,7 @@ def create_mobiles_tools(self):
189183
found_tools.update(vals)
190184
_logger.info('Old unlockbase tool product updated: %s' % found_tools.name)
191185

192-
def send_action(self, values):
193-
values['Key'] = unlockbase_key
194-
data = urllib.urlencode(values)
195-
req = urllib2.Request(unlockbase_url, data)
196-
response = urllib2.urlopen(req)
197-
the_page = response.read()
198-
if 'Unauthorized IP address' in the_page:
199-
_logger.error('Unauthorized IP address ERROR. Please check security configuration in unlockbase.com settings.')
200-
return False
201-
res = xml.etree.ElementTree.fromstring(the_page)
202-
return res
186+
""" unlockbase.com API v3 representation """
203187

204188
def get_all_data(self):
205189
values = {'Action': 'GetMobiles'}
@@ -216,6 +200,19 @@ def get_tool_mobiles(self, tool_id):
216200
res = self.send_action(values)
217201
return res
218202

203+
def send_action(self, values):
204+
unlockbase_url = self.env['ir.config_parameter'].sudo().get_param('unlockbase.url')
205+
values['Key'] = self.env['ir.config_parameter'].sudo().get_param('unlockbase.key')
206+
data = urllib.urlencode(values)
207+
req = urllib2.Request(unlockbase_url, data)
208+
response = urllib2.urlopen(req)
209+
the_page = response.read()
210+
if 'Unauthorized IP address' in the_page:
211+
_logger.error('Unauthorized IP address ERROR. Please check security configuration in unlockbase.com settings.')
212+
return False
213+
res = xml.etree.ElementTree.fromstring(the_page)
214+
return res
215+
219216

220217
class UnlockBaseTool(models.Model):
221218
_name = 'unlockbase.tool'
@@ -279,4 +276,4 @@ def dumpclean(obj):
279276
else:
280277
print v
281278
else:
282-
print obj
279+
print obj

0 commit comments

Comments
 (0)