Skip to content

Commit

Permalink
LocalBitcoin places added
Browse files Browse the repository at this point in the history
  • Loading branch information
darekw committed Oct 11, 2013
1 parent 4914a20 commit bb78890
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 51 deletions.
1 change: 1 addition & 0 deletions coinmap-icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,4 @@ var icon_transport_tram_stop = L.icon({iconUrl: 'icons/transport_tram_stop.n.24.
var icon_transport_turning_circle = L.icon({iconUrl: 'icons/transport_turning_circle.n.24.png', iconSize: [24, 24] });
var icon_transport_weir = L.icon({iconUrl: 'icons/transport_weir.n.24.png', iconSize: [24, 24] });
var icon_transport_zebracrossing = L.icon({iconUrl: 'icons/transport_zebracrossing.n.24.png', iconSize: [24, 24] });
var icon_local_bitcoins = L.icon({iconUrl:'icons/local_bitcoins.n.24.png', iconSize:[24,24]});
Binary file added icons/local_bitcoins.n.24.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added lb/__init__.py
Empty file.
Binary file added lb/__init__.pyc
Binary file not shown.
118 changes: 118 additions & 0 deletions lb/public.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'''
Created on 1 Aug 2013
@author: Jamie
'''

import requests
import json

import logging
logging.basicConfig(format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
level=logging.DEBUG)

root = 'https://localbitcoins.com/'

def countrycodes():
r = requests.get(root+'api/countrycodes/').json()
return r['data']['cc_list']

def payment_methods(countrycode=''):
r = requests.get(root+'api/payment_methods/%s' % countrycode).json()
return r['data']['methods']

def currencies():
r = requests.get(root+'api/currencies').json()
return r['data']['currencies']

def get_buy_ads(currency=None, countrycode=None, payment_method=None):
return _get_ads('buy-bitcoins-online/', currency, countrycode, payment_method)

def get_sell_ads(currency=None, countrycode=None, payment_method=None):
return _get_ads('sell-bitcoins-online/', currency, countrycode, payment_method)

def get_local_buy_ads(location):
geocode = _geocode(location)
places = _get_places(geocode['lat'], geocode['lng'])
url = places[0]['buy_local_url']
ads = _get_local_ads(url)
return ads

def get_local_sell_ads(location):
geocode = _geocode(location)
places = _get_places(geocode['lat'], geocode['lng'])
url = places[0]['sell_local_url']
ads = _get_local_ads(url)
return ads

def _get_local_ads(url):
r = requests.get(url)
ads = json.loads(r.text)
return ads['data']['ad_list']

def _get_places(lat, lng, countrycode=None, location_string=None):
r = requests.get(root+'/api/places?',
params={'lat': lat, 'lon': lng,
'countrycode': countrycode,
'location_string': location_string}).json()
places = r['data']['places']
return places


def _get_ads(trade_type, currency=None, countrycode=None, payment_method=None):
url = root + trade_type
country_ads = []
currency_ads = []

if not countrycode == None:
url1 = url + countrycode + '/name/'
if not payment_method == None:
url1 += payment_method + '/'
url1 += '.json'
r = requests.get(url1).json()
country_ads = r['data']['ad_list']

if not currency == None:
url2 = url + currency + '/'
if not payment_method == None:
url2 += payment_method + '/'
url2 += '.json'
r = requests.get(url2).json()
currency_ads = r['data']['ad_list']

if countrycode == None and currency == None:
if not payment_method == None:
url += payment_method + '/'
url += '.json'
r = requests.get(url).json
ad_list = r['data']['ad_list']

elif not countrycode == None and not currency == None:
ad_list = [item for item in currency_ads if item in country_ads]

else:
ad_list = country_ads + currency_ads

return {'success': 1, 'ad_list': ad_list}

def _geocode(location):
gmaps_url = 'https://maps.google.com/maps/api/geocode/json?'
r = requests.get(gmaps_url,
params={'address': location,
'sensor': 'false'}).json()
if r['status'] == 'OK':
geocoded = r['results'][0]['geometry']['location']
return {'success': 1, 'lat': geocoded['lat'], 'lng': geocoded['lng']}
else:
return {'success': 0}


def test():
pass

def main():
test()

if __name__ == '__main__':
main()
Binary file added lb/public.pyc
Binary file not shown.
142 changes: 91 additions & 51 deletions refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import urllib2
import simplejson
import os
from lb import public as locals

icon_mapping = {
'aeroway:aerodrome': 'transport_airport',
Expand Down Expand Up @@ -153,6 +154,7 @@
'tourism:viewpoint': 'tourist_view_point',
'tourism:zoo': 'tourist_zoo',
'traffic_calming:yes': 'transport_speedbump',
'local_bitcoins:local_bitcoins': 'local_bitcoins',
}

def determine_icon(tags):
Expand All @@ -161,14 +163,71 @@ def determine_icon(tags):
k,v = kv.split(':')
t = tags.get(k)
if not t:
continue
continue
t = t.split(';')[0]
if t == v:
icon = icon_mapping[kv]
break
icon = icon.replace('-', '_')
return icon


def write_elements(f, e):
lat = e.get('lat', None)
lon = e.get('lon', None)
typ = e['type']
tags = e.get('tags', {})
for k in tags.keys():
if tags[k]:
tags[k] = cgi.escape(tags[k]).replace('"', '\\"')
ide = e['id']

if typ == 'node':
nodes[ide] = (lat, lon)
if tags.get('payment:bitcoin') != 'yes': # nodes that are part of way (i.e. not accepting bitcoin)
return None

if typ == 'way':
lat, lon = nodes[e['nodes'][0]] # extract coordinate of first node

if not lat or not lon:
return None

if 'name' in tags:
name = tags['name']
else:
name = '%s %s' % (typ, ide)

icon = determine_icon(tags)
popup = '<b>%s</b> <a href=\\"http://openstreetmap.org/browse/%s/%s\\" target=\\"_blank\\">*</a><hr/>' % (name, typ, ide)
if 'addr:street' in tags:
popup += '%s %s<br/>' % (tags.get('addr:street', ''), tags.get('addr:housenumber', ''))
if 'addr:city' in tags:
popup += '%s %s<br/>' % (tags.get('addr:postcode', ''), tags.get('addr:city', ''))
if 'addr:country' in tags:
popup += '%s<br/>' % (tags.get('addr:country', ''))
popup += '<hr/>'
if 'contact:website' in tags:
w = tags['contact:website']
if not w.startswith('http'):
w = 'http://' + w
popup += 'website: <a href=\\"%s\\" target=\\"_blank\\">%s</a><br/>' % (w, w)
elif 'website' in tags:
w = tags['website']
if not w.startswith('http'):
w = 'http://' + w
popup += 'website: <a href=\\"%s\\" target=\\"_blank\\">%s</a><br/>' % (w, w)
if 'contact:email' in tags:
popup += 'email: <a href=\\"mailto:%s\\" target=\\"_blank\\">%s</a><br/>' % (tags['contact:email'], tags['contact:email'])
elif 'email' in tags:
popup += 'email: <a href=\\"mailto:%s\\" target=\\"_blank\\">%s</a><br/>' % (tags['email'], tags['email'])
if 'contact:phone' in tags:
popup += 'phone: %s<br/>' % (tags['contact:phone'])
elif 'phone' in tags:
popup += 'phone: %s<br/>' % (tags['phone'])
f.write(' L.marker([%s, %s], {"title": "%s", icon: icon_%s}).bindPopup("%s").addTo(markers);\n' % (lat, lon, name.encode('utf-8'), icon, popup.encode('utf-8')))


scriptdir = os.path.dirname(os.path.abspath(__file__))

f = urllib2.urlopen('http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];(node["payment:bitcoin"=yes];way["payment:bitcoin"=yes];>;);out;')
Expand All @@ -180,60 +239,41 @@ def determine_icon(tags):

with open(scriptdir + '/coinmap-data.js', 'w') as f:
f.write('function coinmap_populate(markers) {\n')
# overpass
for e in json['elements']:
lat = e.get('lat', None)
lon = e.get('lon', None)
typ = e['type']
tags = e.get('tags', {})
for k in tags.keys():
tags[k] = cgi.escape(tags[k]).replace('"', '\\"')
ide = e['id']
place = write_elements(f, e)
if place:
cnt += 1

if typ == 'node':
nodes[ide] = (lat,lon)
if tags.get('payment:bitcoin') != 'yes': # nodes that are part of way (i.e. not accepting bitcoin)
continue
# Bitcoin Locals
elements = []

if typ == 'way':
lat, lon = nodes[e['nodes'][0]] # extract coordinate of first node
country_codes = locals.countrycodes()
for country in country_codes:
bl_data = locals.get_local_sell_ads(country)
for entry in bl_data[:2]:
e = entry['data']
j = {
'id': e.get('profile', {}).get('id', '-1'),
'lat': e.get('lat', None),
'lon': e.get('lon', None),
'type': 'node',
'tags': {
'payment:bitcoin': 'yes',
'local_bitcoins': 'local_bitcoins',
'addr:city': e.get('city', ''),
'add:country': e.get('countrycode', ''),
'contact:website': 'https://localbitcoins.com/accounts/profile/'
+ e.get('profile', {}).get('username', '')
},
'website': 'http://localbitcoins.com'
}
elements.append(j)

if not lat or not lon:
continue
for e in elements:
place = write_elements(f, e)
if place:
cnt += 1

cnt += 1

if 'name' in tags:
name = tags['name']
else:
name = '%s %s' % (typ, ide)

icon = determine_icon(tags)
popup = '<b>%s</b> <a href=\\"http://openstreetmap.org/browse/%s/%s\\" target=\\"_blank\\">*</a><hr/>' % (name, typ, ide)
if 'addr:street' in tags:
popup += '%s %s<br/>' % (tags.get('addr:street', ''), tags.get('addr:housenumber', ''))
if 'addr:city' in tags:
popup += '%s %s<br/>' % (tags.get('addr:postcode', ''), tags.get('addr:city', ''))
if 'addr:country' in tags:
popup += '%s<br/>' % (tags.get('addr:country', ''))
popup += '<hr/>'
if 'contact:website' in tags:
w = tags['contact:website']
if not w.startswith('http'):
w = 'http://' + w
popup += 'website: <a href=\\"%s\\" target=\\"_blank\\">%s</a><br/>' % (w, w)
elif 'website' in tags:
w = tags['website']
if not w.startswith('http'):
w = 'http://' + w
popup += 'website: <a href=\\"%s\\" target=\\"_blank\\">%s</a><br/>' % (w, w)
if 'contact:email' in tags:
popup += 'email: <a href=\\"mailto:%s\\" target=\\"_blank\\">%s</a><br/>' % (tags['contact:email'], tags['contact:email'])
elif 'email' in tags:
popup += 'email: <a href=\\"mailto:%s\\" target=\\"_blank\\">%s</a><br/>' % (tags['email'], tags['email'])
if 'contact:phone' in tags:
popup += 'phone: %s<br/>' % (tags['contact:phone'])
elif 'phone' in tags:
popup += 'phone: %s<br/>' % (tags['phone'])
f.write(' L.marker([%s, %s], {"title": "%s", icon: icon_%s}).bindPopup("%s").addTo(markers);\n' % (lat, lon, name.encode('utf-8'), icon, popup.encode('utf-8')))
f.write(' document.getElementById("count").innerHTML = "<b>%d</b>";\n' % cnt);
f.write('}\n')

0 comments on commit bb78890

Please sign in to comment.