<commit_before>#!/usr/bin/python
# coding: utf8
 
############################################################
# Author <NAME> <<EMAIL>>
# Copyright (c) 2013-2014 Beebotte <<EMAIL>>
# This program is published under the MIT License 
# Check http://opensource.org/licenses/MIT for details.
#
# This code uses the Beebotte API, you must have an account.
# You can register here: http://beebotte.com/register
#
# This program computes the position of the ISS in real-time
# and publishes it to Beebotte.
# 
# Use the pip package manager to install dependencies:
# $ pip install pyephem
# $ pip install beebotte
############################################################

import time
from beebotte import *
import ephem
import datetime
import urllib2
from math import degrees

### URL where we will fetch TLE data
url = "http://www.celestrak.com/NORAD/elements/stations.txt"

### This is what TLE looks like. It will be updated every hour
line1 = "ISS (ZARYA)"
line2 = "1 25544U 98067A   14358.52537182  .00021997  00000-0  35639-3 0  6213"
line3 = "2 25544  51.6478 235.6040 0007241 168.1261 263.8740 15.52686091920860"
 
### Replace CHENNL_TOKEN with that of your channel's (this code assumes the channel name is "ISS")
CHANNEL_TOKEN = None
bbt = BBT(token = CHANNEL_TOKEN)
### Otherwise, use your Access and Secret keys to connect to Beebotte
### Replace ACCESS_KEY and SECRET_KEY with those of your account
# ACCESS_KEY = None
# SECRET_KEY = None
# bbt = BBT(ACCESS_KEY, SECRET_KEY)


### Change channel name and resource name as suits you
iss_position_resource = Resource(bbt, 'ISS', 'position')
iss = None
count = 0

def update_tle():
  global iss
  ### Fetch and extract ISS TLE data
  req = urllib2.Request(url)
  response = urllib2.urlopen(req)
  data = response.read()

  tle = data.split('\n')[0:3]
  line1 = tle[0]
  line2 = tle[1]
  line3 = tle[2]
  iss = ephem.readtle(line1, line2, line3)

def run():
  global count
  update_tle()
  while True:
        ### update the TLE data once per hour
        if count > 3600:
          update_tle()
          count = 0
        count += 1
        try:
          ### compute the ISS position
          now = datetime.datetime.utcnow()
          iss.compute(now)
          print('longitude: %f - latitude: %f' % (degrees(iss.sublong), degrees(iss.sublat)))
          ### Send temperature to Beebotte
          iss_position_resource.publish({
            "timestamp": round(time.time()), 
            ### transform longitude and latitude to degrees
            "position": { 
              "long": degrees(iss.sublong), 
              "lat": degrees(iss.sublat) 
            } 
          })

        except Exception as inst:
          print type(inst)     ### the exception instance
          print inst.args      ### arguments stored in .args
          print inst           ###
 
        ### sleep some time
        time.sleep( 1 )
 
run()

<commit_msg>handle request exception<commit_after>#!/usr/bin/python
# coding: utf8
 
############################################################
# Author <NAME> <<EMAIL>>
# Copyright (c) 2013-2014 Beebotte <<EMAIL>>
# This program is published under the MIT License 
# Check http://opensource.org/licenses/MIT for details.
#
# This code uses the Beebotte API, you must have an account.
# You can register here: http://beebotte.com/register
#
# This program computes the position of the ISS in real-time
# and publishes it to Beebotte.
# 
# Use the pip package manager to install dependencies:
# $ pip install pyephem
# $ pip install beebotte
############################################################

import time
from beebotte import *
import ephem
import datetime
import urllib2
from math import degrees

### URL where we will fetch TLE data
url = "http://www.celestrak.com/NORAD/elements/stations.txt"

### This is what TLE looks like. It will be updated every hour
line1 = "ISS (ZARYA)"
line2 = "1 25544U 98067A   16070.60802946  .00010558  00000-0  16731-3 0  9999"
line3 = "2 25544  51.6423 189.6478 0001642 260.2328 233.0609 15.53995147989640"
 
### Replace CHENNL_TOKEN with that of your channel's (this code assumes the channel name is "ISS")
CHANNEL_TOKEN = None
bbt = BBT(token = CHANNEL_TOKEN)
### Otherwise, use your Access and Secret keys to connect to Beebotte
### Replace ACCESS_KEY and SECRET_KEY with those of your account
# ACCESS_KEY = None
# SECRET_KEY = None
# bbt = BBT(ACCESS_KEY, SECRET_KEY)


### Change channel name and resource name as suits you
iss_position_resource = Resource(bbt, 'ISS', 'position')
iss = None
count = 0

def update_tle():
  global iss

  try:
    ### Fetch and extract ISS TLE data
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    data = response.read()

    tle = data.split('\n')[0:3]
    if len(tle) >= 3:
      line1 = tle[0]
      line2 = tle[1]
      line3 = tle[2]

  except Exception as inst:
    print type(inst)     ### the exception instance
    print inst.args      ### arguments stored in .args
    print inst           ###

  iss = ephem.readtle(line1, line2, line3)

def run():
  global count
  update_tle()

  while True:

    ### update the TLE data once per hour
    if count > 3600:
      update_tle()
      count = 0
    count += 1

    try:
      ### compute the ISS position
      now = datetime.datetime.utcnow()
      iss.compute(now)
      print('longitude: %f - latitude: %f' % (degrees(iss.sublong), degrees(iss.sublat)))
      ### Send temperature to Beebotte
      iss_position_resource.publish({
        "timestamp": round(time.time()), 
        ### transform longitude and latitude to degrees
        "position": { 
          "long": degrees(iss.sublong), 
          "lat": degrees(iss.sublat) 
        } 
      })

    except Exception as inst:
      print type(inst)     ### the exception instance
      print inst.args      ### arguments stored in .args
      print inst           ###

    ### sleep some time
    time.sleep( 1 )
 
run()


{'fraction_non_alphanumeric': 0.1414379525172588, 'fraction_numerical': 0.0469776056575181, 'mean_word_length': 2.9865771812080535, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5352922', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1808, 'n_words': 613}