0

I am trying to create a job using Gnip Historical Powertrack API.

I am getting issue with the urllib..

import urllib2  
import base64  
import json  
UN = '' # YOUR GNIP ACCOUNT EMAIL ID  
PWD = ''  
account = '' # YOUR GNIP ACCOUNT USER NAME  
def get_json(data):  
    return json.loads(data.strip())  
def post():  
    url = 'https://historical.gnip.com/accounts/' + account + '/jobs.json'  
    publisher = "twitter"  
    streamType = "track"  
    dataFormat = "activity-streams"  
    fromDate = "201510140630"  
    toDate = "201510140631"  
    jobTitle = "job30"  
    rules = '[{"value":"","tag":""}]'  
    jobString = '{"publisher":"' + publisher + '","streamType":"' + streamType + '","dataFormat":"' + dataFormat + '","fromDate":"' + fromDate + '","toDate":"' + toDate + '","title":"' + jobTitle + '","rules":' + rules + '}'  
    base64string = base64.encodestring('%s:%s' % (UN, PWD)).replace('\n', '')  
    req = urllib2.Request(url=url, data=jobString)  
    req.add_header('Content-type', 'application/json')  
    req.add_header("Authorization", "Basic %s" % base64string)  

    proxy = urllib2.ProxyHandler({'http': 'http://proxy:8080', 'https': 'https://proxy:8080'})  
    opener = urllib2.build_opener(proxy)  
    urllib2.install_opener(opener)  
    try:  
        response = urllib2.urlopen(req)  
        the_page = response.read()  
        the_page = get_json(the_page)  
        print 'Job has been created.'  
        print 'Job UUID : ' + the_page['jobURL'].split("/")[-1].split(".")[0]  
    except urllib2.HTTPError as e:  
        print e.read()  

if __name__=='__main__':  
    post()  

this is the error I am getting :

Traceback (most recent call last):
  File "gnip1.py", line 37, in <module>
    post()  
  File "gnip1.py", line 28, in post
    response = urllib2.urlopen(req)  
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 1240, in https_open
    context=self._context)
  File "/home/soundarya/anaconda-new-1/lib/python2.7/urllib2.py", line 1197, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>

I even tried through the curl command:

When I tried running the below one in terminal, I am getting error - ServiceUsername is not valid.

curl -v -X POST -uname -d '{"title": "HPT_test_job","publisher": "Twitter","streamType":"track","dataFormat":"activity-streams","fromDate":"201401010000","toDate":"201401020000 ","rules":[{"value": "twitter_lang:en (Hillary Clinton OR Donald)","tag": "2014_01_01_snow"}]}' 'https://historical.gnip.com/accounts/account_name/jobs.json'

This is the exact output msg:

Error retrieving Job status: {u'serviceUsername': [u'is invalid']} -- Please verify your connection parameters and network connection *

Soundarya Thiagarajan
  • 574
  • 2
  • 13
  • 31

2 Answers2

0

Try this.. see if it helps

import urllib2
from urllib2.request import urlopen 
u = urlopen ('http:// .........')
iamDRED
  • 1
  • 3
0

If you are using python 3.5 you should use the library urllib.request which is the newer version of urllib2. Notice however that this changes a few things in the code including print (which should be in parentheses) and the need to transform some of the string results into bytes. Here you can look at all the required changes in code adapted to python 3.5

chagag
  • 65
  • 5