API

매일 공부(ML)·2022년 2월 26일
0

GeoJSON API

import urllib.request, urllib.parse, urllib.error
import json

serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
    address = input('Enter location: ')
    if len(address) < 1: break

    url = serviceurl + urllib.parse.urlencode({'address': address})

    print('Retrieving', url)
    uh = urllib.request.urlopen(url)
    data = uh.read().decode()
    print('Retrieved', len(data), 'characters')

    try:
        js = json.loads(data)
    except:
        js = None

    if not js or 'status' not in js or js['status'] != 'OK':
        print('==== Failure To Retrieve ====')
        print(data)
        continue

    lat = js["results"][0]["geometry"]["location"]["lat"]
    lng = js["results"][0]["geometry"]["location"]["lng"]
    print('lat', lat, 'lng', lng)
    location = js['results'][0]['formatted_address']
    print(location)

# Enter location: Korea, Seoul
# Retrieving http://maps.googleapis.com/maps/api/geocode/json?address=Korea%2C+Seoul
# Retrieved 1517 characters
# lat 37.566535 lng 126.9779692
# Seoul, South Korea

트위터 API

트위터 API

트위터에서 개인 키를 발급받으면 트위터 API를 활용할 수 있습니다.

hidden.py 파일의 return 부분에 발급받은 키(4종류)를 입력하면 트위터 API를 활용할 수 있게 됩니다.

import twurl
import json

TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'

While True:
    print('')
    acct = input('Enter Twitter Account:')
    if (len(acct) < 1): break
    url = twurl.augment(TWITTER_URL,
                        {'screen_name': acct, 'count': '5'})
# Keep this file separate

# https://apps.twitter.com/
# Create new App and get the four strings

def oauth():
    return {"consumer_key": "h7Lu...Ng",
            "consumer_secret": "dNKenAC3New...mmn7Q",
            "token_key": "10185562-eibxCp9n2...P4GEQQOSGI",
            "token_secret": "H0ycCFemmC4wyf1...qoIpBo"}
profile
성장을 도울 아카이빙 블로그

0개의 댓글